001/*
002 * Copyright (c) 2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-12-18 01:46:40 +0000 (Fri, 18 Dec 2015) $' 
007 * '$Revision: 34351 $'
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.conversions;
030
031import org.geotools.data.DataUtilities;
032import org.geotools.data.simple.SimpleFeatureCollection;
033import org.geotools.feature.SchemaException;
034import org.geotools.feature.simple.SimpleFeatureBuilder;
035import org.geotools.geometry.jts.GeometryBuilder;
036import org.geotools.geometry.jts.ReferencedEnvelope;
037import org.kepler.gis.data.BoundingBoxToken;
038import org.kepler.gis.data.VectorToken;
039import org.opengis.feature.simple.SimpleFeature;
040import org.opengis.feature.simple.SimpleFeatureType;
041import org.opengis.referencing.crs.CoordinateReferenceSystem;
042
043import com.vividsolutions.jts.geom.Polygon;
044
045import ptolemy.actor.lib.Transformer;
046import ptolemy.kernel.CompositeEntity;
047import ptolemy.kernel.util.IllegalActionException;
048import ptolemy.kernel.util.NameDuplicationException;
049
050/** Convert a bounding box to a vector as a polygon.
051 * 
052 *  @author Daniel Crawl
053 *  @version $Id: BoundingBoxToVector.java 34351 2015-12-18 01:46:40Z crawl $
054 */
055public class BoundingBoxToVector extends Transformer {
056
057    /** Create a new BoundingBoxToVector in a container with a specific name. */
058    public BoundingBoxToVector(CompositeEntity container, String name)
059            throws NameDuplicationException, IllegalActionException {
060        super(container, name);
061        
062        input.setTypeEquals(BoundingBoxToken.BOUNDING_BOX);
063        output.setTypeEquals(VectorToken.VECTOR);
064    }
065    
066    @Override
067    public void fire() throws IllegalActionException {
068     
069        super.fire();
070        
071        ReferencedEnvelope envelope = ((BoundingBoxToken)input.get(0)).boundingBoxValue();
072        
073        SimpleFeatureType type;
074        try {
075            type = DataUtilities.createType("polygon", "geom:Polygon,id:String");
076            CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();
077            if(crs != null) {
078                type = DataUtilities.createSubType(type, null, envelope.getCoordinateReferenceSystem());
079            }
080        } catch (SchemaException e) {
081            throw new IllegalActionException(this, e, "Error creating type for output vector.");
082        }
083        
084        GeometryBuilder builder = new GeometryBuilder();
085        Polygon polygon = builder.box(envelope.getMinX(), envelope.getMinY(),
086            envelope.getMaxX(), envelope.getMaxY());
087        SimpleFeature feature = SimpleFeatureBuilder.build(type, new Object[] {polygon, "1"}, "fid");
088        SimpleFeatureCollection collection = DataUtilities.collection(feature);
089        output.broadcast(new VectorToken(collection));
090    }
091}