001/* 002 * Copyright (c) 2014-2015 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2015-08-24 22:45:41 +0000 (Mon, 24 Aug 2015) $' 007 * '$Revision: 33631 $' 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.io.File; 032import java.io.IOException; 033 034import org.geotools.data.simple.SimpleFeatureCollection; 035import org.geotools.feature.DefaultFeatureCollection; 036import org.geotools.feature.simple.SimpleFeatureTypeBuilder; 037//import org.geotools.process.vector.UnionFeatureCollection; 038import org.kepler.gis.data.UnionFeatureCollection; 039import org.kepler.gis.data.VectorToken; 040import org.kepler.gis.util.VectorUtilities; 041import org.opengis.feature.simple.SimpleFeatureType; 042 043import ptolemy.actor.TypedAtomicActor; 044import ptolemy.actor.TypedIOPort; 045import ptolemy.data.ArrayToken; 046import ptolemy.data.StringToken; 047import ptolemy.data.Token; 048import ptolemy.data.type.ArrayType; 049import ptolemy.data.type.BaseType; 050import ptolemy.kernel.CompositeEntity; 051import ptolemy.kernel.util.IllegalActionException; 052import ptolemy.kernel.util.NameDuplicationException; 053 054/** An actor that combines a set of vector/features files into 055 * a single data set. 056 * 057 * @author Daniel Crawl 058 * @version $Id: VectorCombineFiles.java 33631 2015-08-24 22:45:41Z crawl $ 059 */ 060public class VectorCombineFiles extends TypedAtomicActor { 061 062 public VectorCombineFiles(CompositeEntity container, String name) 063 throws IllegalActionException, NameDuplicationException { 064 065 super(container, name); 066 067 input = new TypedIOPort(this, "input", true, false); 068 input.setTypeEquals(new ArrayType(BaseType.STRING)); 069 070 output = new TypedIOPort(this, "output", false, true); 071 output.setTypeEquals(VectorToken.VECTOR); 072 } 073 074 @Override 075 public void fire() throws IllegalActionException { 076 077 super.fire(); 078 079 ArrayToken arrayToken = (ArrayToken) input.get(0); 080 081 if(arrayToken.length() == 0) { 082 throw new IllegalActionException(this, 083 "Input array of vectors is empty."); 084 } 085 086 //System.out.println("input array size is " + arrayToken.length()); 087 088 SimpleFeatureCollection merged = null; 089 UnionFeatureCollection unionProcess = new UnionFeatureCollection(); 090 091 //int count = 0; 092 093 for(Token token : arrayToken.arrayValue()) { 094 String fileStr = ((StringToken)token).stringValue(); 095 File file = new File(fileStr); 096 if(!file.exists()) { 097 //throw new IllegalActionException(this, "Input does not exist: " + 098 // file); 099 System.err.println("WARNING: input does not exist: " + file); 100 continue; 101 } 102 103 VectorToken vectorToken; 104 try { 105 vectorToken = VectorUtilities.readFile(new File(fileStr)); 106 } catch (IOException e) { 107 throw new IllegalActionException(this, e, 108 "Error reading vector file " + fileStr); 109 } 110 111 SimpleFeatureCollection features = vectorToken.getVectors(); 112 113 if(features.size() == 0) { 114 System.out.println("WARNING: no features in input: " + file); 115 continue; 116 } 117 118 //System.out.println("src features size " + features.size() + " for " + fileStr); 119 //count += features.size(); 120 121 if(merged == null) { 122 merged = features; 123 } else { 124 SimpleFeatureCollection combined; 125 try { 126 combined = unionProcess.execute(features, merged); 127 } catch (ClassNotFoundException e) { 128 throw new IllegalActionException(this, e, 129 "Error combined vector data."); 130 } 131 merged = combined; 132 //System.out.println("new merged size = " + merged.size()); 133 } 134 } 135 136 // see if any of the input files could be found. 137 if(merged == null) { 138 //throw new IllegalActionException(this, 139 System.err.println( 140 "ERROR: no input files existed."); 141 SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder(); 142 builder.setName("empty"); 143 SimpleFeatureType schema = builder.buildFeatureType(); 144 merged = new DefaultFeatureCollection("empty merged", schema); 145 } 146 147 output.broadcast(new VectorToken(merged)); 148 149 /* 150 System.out.println("merged size = " + merged.size() + " count = " + count); 151 count = 0; 152 try(SimpleFeatureIterator iterator = merged.features();) { 153 while(iterator.hasNext()) { 154 iterator.next(); 155 count++; 156 } 157 } 158 System.out.println("iter size = " + count); 159 */ 160 161 } 162 163 @Override 164 public void wrapup() throws IllegalActionException { 165 super.wrapup(); 166 VectorUtilities.closeDataStores(); 167 } 168 169 /** An array of vector/feature file names to read. */ 170 public TypedIOPort input; 171 172 /** A feature data set. */ 173 public TypedIOPort output; 174 175}