public Object[] toArray(Object[] arg0) { List list = new ArrayList(); Iterator it = collections.iterator(); while (it.hasNext()) { FeatureCollection col = (FeatureCollection) it.next(); FeatureIterator it2 = col.features(); while (it2.hasNext()) { list.add(it.next()); } it2.close(); } return list.toArray(arg0); }
File file = new File("mayshapefile.shp"); try { Map connect = new HashMap(); connect.put("url", file.toURL()); DataStore dataStore = DataStoreFinder.getDataStore(connect); String[] typeNames = dataStore.getTypeNames(); String typeName = typeNames[0]; System.out.println("Reading content " + typeName); FeatureSource featureSource = dataStore.getFeatureSource(typeName); FeatureCollection collection = featureSource.getFeatures(); FeatureIterator iterator = collection.features(); try { while (iterator.hasNext()) { Feature feature = iterator.next(); Geometry sourceGeometry = feature.getDefaultGeometry(); } } finally { iterator.close(); } } catch (Throwable e) {}
@Override public String resolve(double x, double y) { System.out.println("x="+x+", y="+y); FeatureIterator<Feature> iterator = collection.features(); while( iterator.hasNext() ){ SimpleFeature feature = (SimpleFeature) iterator.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate coord = new Coordinate(x, y); Point point = geometryFactory.createPoint(coord); //System.out.println("checking "+point.toString()); if(geom.contains(point)) { return feature.getAttribute(this.nameField).toString(); } } return null; }
@Test public void testFeautureSourceFidFilter() throws Exception { // grab the last feature in the collection (there are more than one) SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); // build a filter that will retrieve that feature only FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); final String fid = RENAMED + ".1107531701011"; Filter fidFilter = ff.id(Collections.singleton(ff.featureId(fid))); SimpleFeatureCollection fc = fs.getFeatures(new Query(RENAMED, fidFilter)); assertEquals(RENAMED, fc.getSchema().getName().getLocalPart()); assertEquals(1, fc.size()); FeatureIterator<SimpleFeature> it = fc.features(); assertTrue(it.hasNext()); SimpleFeature sf = it.next(); assertFalse(it.hasNext()); it.close(); assertEquals(fid, sf.getID()); }
public void testFeatureCollection() throws Exception { FeatureSource fs = dataStore.getFeatureSource(tname("empty")); FeatureCollection features = fs.getFeatures(); assertTrue(features.getBounds().isNull()); assertEquals(0, features.size()); try (FeatureIterator i = features.features()) { assertFalse(i.hasNext()); } } }
/** We have to cache all the features in the supplemental file, because * if we try to load them on the fly, GeoTools wigs out. */ private void ensureCached() { if (cache != null) { return; } cache = new HashMap<String, SimpleFeature>(); try { FeatureCollection<SimpleFeatureType, SimpleFeature> features = joinedSource .getFeatures(); FeatureIterator<SimpleFeature> it = features.features(); while (it.hasNext()) { SimpleFeature feature = it.next(); String joinedKeyValue = toHashableString(feature.getAttribute(joinedKey)); if (joinedKeyValue != null) { cache.put(joinedKeyValue, feature); } else { log.warn("Feature " + feature.getID() + " has null value for its joinedKey (" + joinedKey + ")"); } } it.close(); } catch (IOException e) { throw new RuntimeException("Could not cache values for joined shapefile", e); } }
/** * The function is run periodically by the update manager. * The extending class should provide the getNote method. It is not implemented here * as the requirements for different updaters can be vastly different dependent on the data source. */ @Override protected void runPolling() throws IOException{ LOG.info("Run WFS polling updater with hashcode: {}", this.hashCode()); notesForEdge = HashMultimap.create(); uniqueMatchers = new HashMap<>(); FeatureIterator<SimpleFeature> features = featureSource.getFeatures(query).features(); while ( features.hasNext()){ SimpleFeature feature = features.next(); if (feature.getDefaultGeometry() == null) continue; Alert alert = getNote(feature); if (alert == null) continue; Geometry geom = (Geometry) feature.getDefaultGeometry(); Geometry searchArea = geom.buffer(SEARCH_RADIUS_DEG); Collection<Edge> edges = graph.streetIndex.getEdgesForEnvelope(searchArea.getEnvelopeInternal()); for(Edge edge: edges){ if (edge instanceof StreetEdge && !searchArea.disjoint(edge.getGeometry())) { addNote(edge, alert, NOTE_MATCHER); } } } updaterManager.execute(new WFSGraphWriter()); }
@Test public void testFeautureSourceFidFilter() throws Exception { // grab the last feature in the collection (there are more than one) SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); // build a filter that will retrieve that feature only FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); final String fid = RENAMED + ".f001"; Filter fidFilter = ff.id(Collections.singleton(ff.featureId(fid))); SimpleFeatureCollection fc = fs.getFeatures(new Query(RENAMED, fidFilter)); assertEquals(RENAMED, fc.getSchema().getName().getLocalPart()); assertEquals(1, fc.size()); FeatureIterator<SimpleFeature> it = fc.features(); assertTrue(it.hasNext()); SimpleFeature sf = it.next(); assertFalse(it.hasNext()); it.close(); assertEquals(fid, sf.getID()); }
public boolean hasNext() { // is there a current iterator that has another element if ((iterator != null) && iterator.hasNext()) { return true; } // get the next iterator while (index < collections.size()) { // close current before we move to next if (iterator != null) { iterator.close(); } // grap next iterator = collections.get(index++).features(); if (iterator.hasNext()) { return true; } } // no more if (iterator != null) { // close the last iterator iterator.close(); } return false; }
public void addFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> collection) { if ((collection == null)) { throw new IllegalArgumentException("Provided FeatureCollection is empty"); } synchronized (entries) { try (FeatureIterator<SimpleFeature> iterator = collection.features()) { while (iterator.hasNext()) { addFeatureInternal(iterator.next()); } } } } /**
public <T> T[] toArray(T[] arg0) { List<T> list = new ArrayList<T>(); Iterator it = collections.iterator(); while (it.hasNext()) { FeatureCollection col = (FeatureCollection) it.next(); FeatureIterator it2 = col.features(); try { while (it2.hasNext()) { list.add((T) it.next()); } } finally { it2.close(); } } return list.toArray(arg0); }
protected Feature getFilteredNext() { while (delegate.hasNext() && count < maxFeatures) { Feature feature = delegate.next(); try { if (filter.evaluate(feature)) { return feature; } } catch (NullPointerException e) { // ignore this exception // this is to cater the case if the attribute has no value and // has been skipped in the delegate DataAccessMappingFeatureIterator } } return null; }
/** * Copies the provided features into a List. * * @param featureCollection * @return List of features copied into memory */ public static <F extends Feature> List<F> list(FeatureCollection<?, F> featureCollection) { final ArrayList<F> list = new ArrayList<F>(); FeatureIterator<F> iter = featureCollection.features(); try { while (iter.hasNext()) { F feature = iter.next(); list.add(feature); } } finally { iter.close(); } return list; } /**
Filter matchingIdFilter = null; try (FeatureIterator<Feature> it = fCollection.features()) { if (nestedIdExpression.equals(Expression.NIL)) { HashSet<FeatureId> featureIds = new HashSet<FeatureId>(); while (it.hasNext()) { Feature f = it.next(); matchingFeatures.add(f); if (isMultiple && f.getIdentifier() != null) { while (it.hasNext()) { Feature f = it.next(); matchingFeatures.add(f); if (isMultiple) { try (FeatureIterator<Feature> it = fCollection.features()) { matchingFeatures.clear(); while (it.hasNext()) { matchingFeatures.add(it.next());