001/*
002 * Copyright (c) 2016 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2016-04-21 05:38:50 +0000 (Thu, 21 Apr 2016) $' 
007 * '$Revision: 34476 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.gis.actor.bbox;
030
031import java.io.File;
032
033import org.geotools.geometry.jts.ReferencedEnvelope;
034import org.geotools.referencing.CRS;
035import org.geotools.referencing.crs.DefaultGeographicCRS;
036import org.kepler.gis.actor.GetBoundingBox;
037import org.kepler.gis.data.GISToken;
038import org.kepler.gis.util.RasterUtilities;
039import org.kepler.gis.util.RasterUtilities.RasterInfo;
040import org.opengis.referencing.FactoryException;
041import org.opengis.referencing.crs.CoordinateReferenceSystem;
042import org.opengis.referencing.operation.TransformException;
043
044import com.vividsolutions.jts.geom.Envelope;
045
046import ptolemy.actor.lib.Transformer;
047import ptolemy.actor.parameters.PortParameter;
048import ptolemy.data.StringToken;
049import ptolemy.data.Token;
050import ptolemy.data.type.BaseType;
051import ptolemy.kernel.CompositeEntity;
052import ptolemy.kernel.util.Attribute;
053import ptolemy.kernel.util.IllegalActionException;
054import ptolemy.kernel.util.NameDuplicationException;
055
056/** Find a GIS file in a directory whose bounding box contains
057 *  the input GIS data set.
058 * 
059 *  @author Daniel Crawl
060 *  @version $Id: FindCoveringFile.java 34476 2016-04-21 05:38:50Z crawl $ 
061 */
062public class FindCoveringFile extends Transformer {
063
064    public FindCoveringFile(CompositeEntity container, String name)
065            throws IllegalActionException, NameDuplicationException {
066        
067        super(container, name);
068        
069        input.setTypeAtMost(GISToken.GIS);
070        
071        directory = new PortParameter(this, "directory");
072        directory.setTypeEquals(BaseType.STRING);
073        directory.setStringMode(true);
074        directory.getPort().setTypeEquals(BaseType.STRING);
075        new Attribute(directory.getPort(), "_showName");
076        
077        
078        output.setTypeEquals(BaseType.STRING);
079        
080    }
081    
082    @Override
083    public void fire() throws IllegalActionException {
084     
085        super.fire();
086
087        directory.update();
088        Token token = directory.getToken();
089        if(token == null) {
090            throw new IllegalActionException(this, "Must specify directory.");
091        }
092        
093        String dirStr = ((StringToken)token).stringValue();
094        File dir = new File(dirStr);
095        if(!dir.isDirectory()) {
096            throw new IllegalActionException(this, "Directory " +
097                    dirStr + " does not exist.");
098        }
099        
100        String outName = null;
101        
102        token = input.get(0);
103        ReferencedEnvelope inputEnvelope = GetBoundingBox.getBoundingBox(token);
104        CoordinateReferenceSystem inputCRS = inputEnvelope.getCoordinateReferenceSystem();
105        if(inputCRS == null) {
106            System.err.println("WARNING: no CRS for input; assuming WGS84");
107            inputCRS = DefaultGeographicCRS.WGS84;
108        }
109        
110        //System.out.println("input bb = " + inputEnvelope);
111        
112        for(File file : dir.listFiles()) {
113            
114            final String fileName = file.getName();
115            if(file.isDirectory() || fileName.endsWith(".prj")) {
116                continue;
117            }
118            else if(fileName.endsWith(".lcp")) {
119                RasterInfo info = RasterUtilities.getInfo(file);
120                if(info != null) {
121                    ReferencedEnvelope fileEnvelope = info.getEnvelope();
122                    CoordinateReferenceSystem fileCRS = info.getCRS();
123                    if(!CRS.equalsIgnoreMetadata(fileCRS, inputCRS)) {
124                        try {
125                            fileEnvelope = fileEnvelope.transform(inputCRS, true);
126                        } catch (TransformException | FactoryException e) {
127                            throw new IllegalActionException(this, e,
128                                    "Error transforming CRS.");
129                        }
130                    }
131                    
132                    //System.out.println("checking envelope = " + fileEnvelope);
133                    
134                    if(fileEnvelope.contains((Envelope)inputEnvelope)) {
135                        outName = file.getAbsolutePath();
136                        break;
137                    }
138                }
139            } else {
140                System.err.println("WARNING: unknown type of file: " + file);
141            }
142        }
143        
144        if(outName == null) {
145            outName = "";
146        }
147        
148        output.broadcast(new StringToken(outName));
149        
150    }
151    
152    /** The directory to search. */
153    public PortParameter directory;
154
155}