001/*
002 * Copyright (c) 2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-12-16 18:21:36 +0000 (Wed, 16 Dec 2015) $' 
007 * '$Revision: 34339 $'
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.calc;
030
031import java.util.ArrayList;
032import java.util.List;
033
034import org.geotools.data.simple.SimpleFeatureCollection;
035import org.geotools.data.simple.SimpleFeatureIterator;
036import org.geotools.filter.AreaFunction;
037import org.kepler.gis.data.VectorToken;
038import org.opengis.feature.simple.SimpleFeature;
039
040import com.vividsolutions.jts.geom.Geometry;
041
042import ptolemy.actor.lib.Transformer;
043import ptolemy.data.ArrayToken;
044import ptolemy.data.DoubleToken;
045import ptolemy.data.type.ArrayType;
046import ptolemy.data.type.BaseType;
047import ptolemy.kernel.CompositeEntity;
048import ptolemy.kernel.util.IllegalActionException;
049import ptolemy.kernel.util.NameDuplicationException;
050
051/** Calculate the perimeter of a GIS data set.
052 * 
053 *  @author Daniel Crawl
054 *  @version $Id: Perimeter.java 34339 2015-12-16 18:21:36Z crawl $
055 */
056public class Perimeter extends Transformer {
057
058    /** Create a new Perimeter in a container with a specific name. */
059    public Perimeter(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException {
060        super(container, name);
061        
062        input.setTypeEquals(VectorToken.VECTOR);
063        
064        output.setTypeEquals(new ArrayType(BaseType.DOUBLE));
065    }
066    
067    @Override
068    public void fire() throws IllegalActionException {
069        
070        super.fire();
071        
072        SimpleFeatureCollection features = ((VectorToken)input.get(0)).getVectors();
073        
074        List<DoubleToken> list = new ArrayList<DoubleToken>(features.size());
075        
076        AreaFunction area = new AreaFunction();
077        
078        try(SimpleFeatureIterator iterator = features.features();) {
079            while(iterator.hasNext()) {
080                SimpleFeature feature = iterator.next();
081                Object geometry = feature.getDefaultGeometry();
082                if(!(geometry instanceof Geometry)) {
083                    System.err.println("WARNING: is not instance of Geometry: " + geometry.getClass());
084                } else {
085                    double areaVal = area.getPerimeter((Geometry)geometry);
086                    list.add(new DoubleToken(areaVal));
087                }
088            }
089        }
090        
091        /*
092        SimpleFeatureType schema = features.getSchema();
093        if(schema != null) {
094            CoordinateReferenceSystem crs = schema.getCoordinateReferenceSystem();
095            if(crs != null) {
096                System.out.println("crs = " + crs.getName());
097                for(int i = 0; i < crs.getCoordinateSystem().getDimension(); i++) {
098                    Unit<?> unit = crs.getCoordinateSystem().getAxis(i).getUnit();
099                    System.out.println("units[" + i + "] = " + unit);
100                }
101            }
102        }
103        */
104
105        output.broadcast(new ArrayToken(list.toArray(new DoubleToken[list.size()])));
106    }
107
108}