001/*
002 * Copyright (c) 2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2016-08-28 01:01:21 +0000 (Sun, 28 Aug 2016) $' 
007 * '$Revision: 34521 $'
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.proj;
030
031import java.io.File;
032
033import org.geotools.data.simple.SimpleFeatureCollection;
034import org.geotools.geometry.jts.ReferencedEnvelope;
035import org.kepler.gis.actor.CRSActor;
036import org.kepler.gis.data.BoundingBoxToken;
037import org.kepler.gis.data.GISToken;
038import org.kepler.gis.data.RasterToken;
039import org.kepler.gis.data.VectorToken;
040import org.kepler.gis.util.RasterUtilities;
041import org.kepler.gis.util.VectorUtilities;
042import org.opengis.referencing.FactoryException;
043import org.opengis.referencing.operation.TransformException;
044
045import ptolemy.actor.TypedIOPort;
046import ptolemy.data.Token;
047import ptolemy.kernel.CompositeEntity;
048import ptolemy.kernel.util.IllegalActionException;
049import ptolemy.kernel.util.NameDuplicationException;
050
051/** Reproject a GIS data set.
052 * 
053 *  @author Daniel Crawl
054 *  @version $Id: Reproject.java 34521 2016-08-28 01:01:21Z crawl $
055 */
056public class Reproject extends CRSActor {
057
058    public Reproject(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException {
059        super(container, name);
060        
061        input = new TypedIOPort(this, "input", true, false);
062        input.setTypeAtMost(GISToken.GIS);
063        
064        output = new TypedIOPort(this, "output", false, true);
065        output.setTypeSameAs(input);        
066    }
067    
068    @Override
069    public void fire() throws IllegalActionException {
070        super.fire();
071        
072        Token token = input.get(0);
073        
074        Token outToken;
075                
076        if(token instanceof VectorToken) {
077            SimpleFeatureCollection features = ((VectorToken)token).getVectors();
078            SimpleFeatureCollection reprojectedFeatures = VectorUtilities.reproject(features, _crs);
079            outToken = new VectorToken(reprojectedFeatures);
080        } else if(token instanceof RasterToken) {
081            File rasterFile = ((RasterToken)token).rasterFile();
082            if(rasterFile != null) {                
083                File reprojectedRasterFile = RasterUtilities.reproject(rasterFile, _crs);
084                outToken = new RasterToken(reprojectedRasterFile);
085            } else {
086                throw new IllegalActionException(this, "Reprojecting raster coverages not yet supported.");
087            }
088        } else if(token instanceof BoundingBoxToken) {
089            ReferencedEnvelope envelope = ((BoundingBoxToken)token).boundingBoxValue();
090            ReferencedEnvelope newEnvelope;
091            try {
092                newEnvelope = envelope.transform(_crs, true);
093            } catch (TransformException | FactoryException e) {
094                throw new IllegalActionException(this, e,
095                    "Error reprojecting bounding box.");
096            }
097            outToken = new BoundingBoxToken(newEnvelope);
098        } else {
099            throw new IllegalActionException(this, "Unsupported GIS type: " + token.getType());
100        }
101        
102        output.broadcast(outToken);
103    }
104
105    /** Input GIS data set to reproject. */
106    public TypedIOPort input;
107    
108    /** Reprojected GIS data set. */
109    public TypedIOPort output;
110}