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 testForce() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(MockData.BASIC_POLYGONS.getLocalPart()); assertEquals("EPSG:4269", fti.getSRS()); assertEquals(ProjectionPolicy.FORCE_DECLARED, fti.getProjectionPolicy()); FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures(); assertEquals(CRS.decode("EPSG:4269"), fc.getSchema().getCoordinateReferenceSystem()); FeatureIterator fi = fc.features(); Feature f = fi.next(); fi.close(); assertEquals(CRS.decode("EPSG:4269"), f.getType().getCoordinateReferenceSystem()); }
@Test public void testDefaultGeometry() throws IOException { FeatureTypeInfo featureType = getCatalog().getResourceByName("cdf", "Nulls", FeatureTypeInfo.class); GeometryDescriptor schemaDefaultGeometry = featureType.getFeatureType().getGeometryDescriptor(); FeatureIterator i = featureType.getFeatureSource(null, null).getFeatures().features(); GeometryDescriptor featureDefaultGeometry = i.next().getDefaultGeometryProperty().getDescriptor(); assertNotNull(schemaDefaultGeometry); assertNotNull(featureDefaultGeometry); assertEquals("pointProperty", schemaDefaultGeometry.getLocalName()); assertEquals(schemaDefaultGeometry, featureDefaultGeometry); } }
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 testLeaveNative() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(MockData.LINES.getLocalPart()); assertEquals("EPSG:3004", fti.getSRS()); assertEquals(ProjectionPolicy.NONE, fti.getProjectionPolicy()); FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures(); assertEquals(CRS.decode("EPSG:32615"), fc.getSchema().getCoordinateReferenceSystem()); FeatureIterator fi = fc.features(); Feature f = fi.next(); // test that the geometry was left in tact Geometry g = (Geometry) f.getDefaultGeometryProperty().getValue(); assertTrue(g.equalsExact(WKT.read("LINESTRING(500125 500025,500175 500075)"))); fi.close(); assertEquals(CRS.decode("EPSG:32615"), f.getType().getCoordinateReferenceSystem()); }
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()); } } } } /**
@Test public void testReproject() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(MockData.POLYGONS.getLocalPart()); assertEquals("EPSG:4326", fti.getSRS()); assertEquals(ProjectionPolicy.REPROJECT_TO_DECLARED, fti.getProjectionPolicy()); FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures(); assertEquals(CRS.decode("EPSG:4326"), fc.getSchema().getCoordinateReferenceSystem()); FeatureIterator fi = fc.features(); Feature f = fi.next(); // test that geometry was actually reprojected Geometry g = (Geometry) f.getDefaultGeometryProperty().getValue(); assertFalse( g.equalsExact( WKT.read( "POLYGON((500225 500025,500225 500075,500275 500050,500275 500025,500225 500025))"))); fi.close(); assertEquals(CRS.decode("EPSG:4326"), f.getType().getCoordinateReferenceSystem()); }
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); }
@Test public void testRetrieveTimeInstantGmlId() throws IOException { FilterFactory2 ff = new FilterFactoryImplNamespaceAware(namespaces); PropertyName gmlIdProperty = ff.property("om:resultTime/gml:TimeInstant/@gml:id"); try (FeatureIterator featureIt = obsSource.getFeatures().features()) { Feature f = featureIt.next(); String gmlId = (String) gmlIdProperty.evaluate(f); assertTrue(gmlId != null && !gmlId.trim().isEmpty()); } } }
/** * Obtain the first feature from the collection as an exemplar. * * @param featureCollection * @return first feature from the featureCollection */ public static <F extends Feature> F first(FeatureCollection<?, F> featureCollection) { if (featureCollection == null) { return null; } FeatureIterator<F> iter = featureCollection.features(); try { while (iter.hasNext()) { F feature = iter.next(); if (feature != null) { return feature; } } return null; // not found! } finally { iter.close(); } }
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());
@Test public void testQueryWithPropertyNames() throws Exception { // check the schemas in feature source and feature collection SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); Query q = new Query(RENAMED, Filter.INCLUDE, new String[] {"ADDRESS"}); FeatureCollection<SimpleFeatureType, SimpleFeature> fc = fs.getFeatures(q); assertEquals(1, fc.getSchema().getAttributeCount()); // make sure the feature schema is good as well FeatureIterator<SimpleFeature> it = fc.features(); SimpleFeature sf = it.next(); it.close(); assertEquals(1, sf.getAttributeCount()); assertNull(sf.getAttribute("FID")); assertNotNull(sf.getAttribute("ADDRESS")); } }
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 = ((FeatureCollection) collections.get(index++)).features(); if (iterator.hasNext()) { return true; } } // no more if (iterator != null) { // close the last iterator iterator.close(); } return false; }