001/* 002 * Copyright (c) 2014-2015 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2015-04-27 18:47:47 +0000 (Mon, 27 Apr 2015) $' 007 * '$Revision: 33374 $' 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; 030 031import java.util.ArrayList; 032 033import org.geotools.data.simple.SimpleFeatureCollection; 034import org.geotools.data.simple.SimpleFeatureIterator; 035import org.kepler.gis.data.VectorToken; 036import org.opengis.feature.simple.SimpleFeature; 037 038import ptolemy.actor.lib.Transformer; 039import ptolemy.actor.parameters.PortParameter; 040import ptolemy.data.ArrayToken; 041import ptolemy.data.DoubleToken; 042import ptolemy.data.IntToken; 043import ptolemy.data.StringToken; 044import ptolemy.data.Token; 045import ptolemy.data.type.ArrayType; 046import ptolemy.data.type.BaseType; 047import ptolemy.kernel.CompositeEntity; 048import ptolemy.kernel.util.IllegalActionException; 049import ptolemy.kernel.util.NameDuplicationException; 050import ptolemy.kernel.util.SingletonAttribute; 051 052/** An actor that extracts properties from a vector/feature data set. 053 * 054 * TODO make property be the output port to allow multiple properties 055 * but how to handle properties with "." or "-" in the name? 056 * 057 * @author Daniel Crawl 058 * @version $Id: VectorProperty.java 33374 2015-04-27 18:47:47Z crawl $ 059 */ 060public class VectorProperty extends Transformer { 061 062 public VectorProperty(CompositeEntity container, String name) 063 throws NameDuplicationException, IllegalActionException { 064 065 super(container, name); 066 067 input.setTypeEquals(VectorToken.VECTOR); 068 output.setTypeEquals(new ArrayType(BaseType.STRING)); 069 070 propertyName = new PortParameter(this, "name"); 071 propertyName.setStringMode(true); 072 propertyName.setTypeEquals(BaseType.STRING); 073 propertyName.getPort().setTypeEquals(BaseType.STRING); 074 new SingletonAttribute(propertyName.getPort(), "_showName"); 075 } 076 077 @Override 078 public void fire() throws IllegalActionException { 079 080 super.fire(); 081 082 propertyName.update(); 083 084 StringToken nameToken = (StringToken)propertyName.getToken(); 085 String nameStr = null; 086 if(nameToken != null) { 087 nameStr = nameToken.stringValue(); 088 } 089 090 if(nameStr == null || nameStr.trim().isEmpty()) { 091 throw new IllegalActionException(this, "Property name must not be empty."); 092 } 093 094 ArrayList<Token> array = new ArrayList<Token>(); 095 096 VectorToken vectorToken = (VectorToken) input.get(0); 097 SimpleFeatureCollection features = vectorToken.getVectors(); 098 099 try(SimpleFeatureIterator iterator = features.features();) { 100 while(iterator.hasNext()) { 101 SimpleFeature feature = iterator.next(); 102 Object value = feature.getAttribute(nameStr); 103 if(value != null) { 104 if(value instanceof Double) { 105 array.add(new DoubleToken(((Double)value).doubleValue())); 106 } else if(value instanceof Integer) { 107 array.add(new IntToken(((Integer)value).intValue())); 108 } else { 109 array.add(new StringToken(value.toString())); 110 } 111 } 112 } 113 } 114 115 if(array.isEmpty()) { 116 throw new IllegalActionException(this, 117 "Property " + nameStr + " was not found in features."); 118 } 119 120 output.broadcast(new ArrayToken(array.toArray(new Token[array.size()]))); 121 122 } 123 124 /** The name of the property to extract. */ 125 public PortParameter propertyName; 126}