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; 041import com.vividsolutions.jts.geom.GeometryFactory; 042import com.vividsolutions.jts.geom.MultiLineString; 043 044import ptolemy.actor.lib.Transformer; 045import ptolemy.data.ArrayToken; 046import ptolemy.data.DoubleToken; 047import ptolemy.data.type.ArrayType; 048import ptolemy.data.type.BaseType; 049import ptolemy.kernel.CompositeEntity; 050import ptolemy.kernel.util.IllegalActionException; 051import ptolemy.kernel.util.NameDuplicationException; 052 053/** Calculate the area of a GIS data set. 054 * 055 * @author Daniel Crawl 056 * @version $Id: Area.java 34339 2015-12-16 18:21:36Z crawl $ 057 */ 058public class Area extends Transformer { 059 060 /** Create a new Area in a container with a specific name. */ 061 public Area(CompositeEntity container, String name) throws NameDuplicationException, IllegalActionException { 062 super(container, name); 063 064 input.setTypeEquals(VectorToken.VECTOR); 065 066 output.setTypeEquals(new ArrayType(BaseType.DOUBLE)); 067 } 068 069 @Override 070 public void fire() throws IllegalActionException { 071 072 super.fire(); 073 074 SimpleFeatureCollection features = ((VectorToken)input.get(0)).getVectors(); 075 076 List<DoubleToken> list = new ArrayList<DoubleToken>(features.size()); 077 078 AreaFunction area = new AreaFunction(); 079 GeometryFactory factory = new GeometryFactory(); 080 081 try(SimpleFeatureIterator iterator = features.features();) { 082 while(iterator.hasNext()) { 083 SimpleFeature feature = iterator.next(); 084 Object geometryObj = feature.getDefaultGeometry(); 085 if(!(geometryObj instanceof Geometry)) { 086 System.err.println("WARNING: is not instance of Geometry: " + geometryObj.getClass()); 087 } else { 088 Geometry geometry = (Geometry) geometryObj; 089 if(geometry instanceof MultiLineString) { 090 MultiLineString multiLineString = (MultiLineString) geometry; 091 geometry = factory.createPolygon(multiLineString.getCoordinates()); 092 } 093 094 double areaVal = area.getArea((Geometry)geometry); 095 list.add(new DoubleToken(areaVal)); 096 } 097 } 098 } 099 100 output.broadcast(new ArrayToken(list.toArray(new DoubleToken[list.size()]))); 101 } 102 103}