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) {}
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; }
/** 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); } }
query.setCoordinateSystemReproject(getMapContent().getCoordinateReferenceSystem()); FeatureSource featureSource = getLayer().getFeatureSource(); Collection<PropertyDescriptor> descriptors = featureSource.getSchema().getDescriptors(); FeatureCollection queryResult = featureSource.getFeatures(query); FeatureIterator iter = queryResult.features(); while (iter.hasNext()) { Feature f = iter.next(); result.newFeature(f.getIdentifier().getID()); for (PropertyDescriptor desc : descriptors) { Name name = desc.getName(); Object value = f.getProperty(name).getValue(); iter.close();
@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; }
private void grabGeometry(int mx, int my) { Geometry geo = mousePositionToGeometry(mx, my); Filter flt = map2D.createFilter(geo,map2D.getSelectionFilter(), map2D.getEditedMapLayer()); FeatureCollection<SimpleFeatureType, SimpleFeature> editgeoms = null; try { editgeoms = (FeatureCollection<SimpleFeatureType, SimpleFeature>) map2D.getEditedMapLayer().getFeatureSource().getFeatures(flt); } catch (IOException e) { e.printStackTrace(); } if (editgeoms != null) { FeatureIterator<SimpleFeature> fi = editgeoms.features(); if (fi.hasNext()) { SimpleFeature sf = fi.next(); Object obj = sf.getDefaultGeometry(); if (obj instanceof Geometry) { hasEditionGeometry = true; Geometry geom = (Geometry) obj; geom = FACILITIES_FACTORY.projectGeometry(geom, map2D.getEditedMapLayer().getFeatureSource().getSchema().getCoordinateReferenceSystem(), map2D.getRenderingStrategy().getContext().getCoordinateReferenceSystem()); geoms.add((Geometry) geom.clone()); editedFeatureID = sf.getID(); System.out.println(editedFeatureID); } } fi.close(); } }
@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()); }
DefaultFeatureCollection results = new DefaultFeatureCollection(); try { if (featureCollection == null || featureCollection.size() == 0) { LOGGER.info("No features provided in request"); return results; || featureCollection.getSchema().getDescriptor(fromMeasureAttb) == null) { throw new ProcessException( "The from_measure_attb parameter was not provided or not defined in schema"); || featureCollection.getSchema().getDescriptor(toMeasureAttb) == null) { throw new ProcessException("The to_measure_attb parameter was not provided"); try { featureIterator = featureCollection.features(); Feature feature = featureIterator.next(); Double featureFromMeasure = (Double) feature.getProperty(fromMeasureAttb).getValue(); Double featureToMeasure = (Double) feature.getProperty(toMeasureAttb).getValue(); LengthIndexedLine lengthIndexedLine = new LengthIndexedLine( (Geometry) feature.getDefaultGeometryProperty().getValue()); double featureLength = featureToMeasure - featureFromMeasure; double startOffset = measure - featureFromMeasure; results.add(createTargetFeature(feature, targetFeatureType, point)); } finally { if (featureIterator != null) featureIterator.close();
ProgressListener progressListener) throws IOException { int count = features.size(); float done = 0; GeometryCollector collector = new GeometryCollector(); try { fi = features.features(); while (fi.hasNext()) { Geometry g = (Geometry) fi.next().getDefaultGeometryProperty().getValue(); collector.add(g); fi.close();
CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS(); .getFeatures(query); features = featureSource.getFeatures(query); FeatureIterator<SimpleFeature> it2 = features.features(); while (it2.hasNext()) { SimpleFeature feature = it2.next(); if (featureSelector != null && ! featureSelector.convert(feature)) { continue; it2.close(); it2 = null; if (feature.getDefaultGeometry() == null) { log.warn("feature has no geometry: " + feature.getIdentifier()); continue; LineString geom = toLineString((Geometry) feature.getDefaultGeometry());
footprintStore.getFeatureSource(typeName); final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(); if (features == null) { if (LOGGER.isLoggable(Level.FINE)) { try { it = features.features(); if (!it.hasNext()) { throw new IllegalArgumentException( "The provided FeatureCollection<SimpleFeatureType, SimpleFeature> or empty, it's impossible to create an index!"); while (it.hasNext()) { final SimpleFeature feature = it.next(); final Geometry g = (Geometry) feature.getDefaultGeometry(); final String location = (String) feature.getAttribute("location"); footprintsMap.put(location, g); it.close(); it.close(); } catch (Throwable e) {
DefaultFeatureCollection results = new DefaultFeatureCollection(); try { if (featureCollection == null || featureCollection.size() == 0) { LOGGER.info("No features provided in request"); return results; GeometryDescriptor gd = featureCollection.getSchema().getGeometryDescriptor(); if (gd != null) { crs = gd.getCoordinateReferenceSystem(); || featureCollection.getSchema().getDescriptor(fromMeasureAttb) == null) { throw new ProcessException( "The from_measure_attb parameter was not provided or not defined in schema"); || featureCollection.getSchema().getDescriptor(toMeasureAttb) == null) { throw new ProcessException("The to_measure_attb parameter was not provided"); while (featureIterator.hasNext()) { SimpleFeature f = (SimpleFeature) featureIterator.next(); if (f.getDefaultGeometryProperty().getValue() == null) continue; DistanceOp op = new DistanceOp( point, (Geometry) f.getDefaultGeometryProperty().getValue()); Coordinate[] co = op.closestPoints(); double[] co0 = if (featureIterator != null) featureIterator.close();
@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")); } }
@Test public void testGetFeaturesFeatureSource() throws Exception { // check the schemas in feature source and feature collection SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); assertEquals(primitive, fs.getSchema()); SimpleFeatureCollection fc = fs.getFeatures(); assertEquals(primitive, fc.getSchema()); assertTrue(fc.size() > 0); // make sure the feature schema is good as well FeatureIterator<SimpleFeature> it = fc.features(); SimpleFeature sf = it.next(); it.close(); assertEquals(primitive, sf.getFeatureType()); // check the feature ids have been renamed as well assertTrue( "Feature id has not been renamed, it's still " + sf.getID(), sf.getID().startsWith(RENAMED)); // check mappings occurred assertEquals("description-f001", sf.getAttribute("description")); assertTrue( new WKTReader() .read("MULTIPOINT(39.73245 2.00342)") .equalsExact((Geometry) sf.getAttribute("pointProperty"))); assertEquals(Long.valueOf(155), sf.getAttribute("intProperty")); assertNull(sf.getAttribute("newProperty")); }
assertEquals(3, features.size()); FeatureIterator fi = features.features(); try { for (int i = 0; i < 3; i++) { assertTrue(fi.hasNext()); SimpleFeature f = (SimpleFeature) fi.next(); assertTrue(f.getDefaultGeometry() instanceof Point); Point point = (Point) f.getDefaultGeometry(); assertEquals(i / 1d, point.getX(), 0.1); assertEquals(i / 1d, point.getX(), 0.1); assertEquals(i, f.getAttribute("count")); fi.close();
try { if (crs == null) { GeometryDescriptor gd = featureCollection.getSchema().getGeometryDescriptor(); if (gd != null) { crs = gd.getCoordinateReferenceSystem(); FeatureType targetFeatureType = createTargetFeatureType(featureCollection.getSchema()); Unit fromUnit = SI.METRE; Unit toUnit = USCustomary.MILE; double nearestDistance = 9e9; double nearestBearing = 0; FeatureIterator featureIterator = featureCollection.features(); try { while (featureIterator.hasNext()) { SimpleFeature f = (SimpleFeature) featureIterator.next(); if (f.getDefaultGeometryProperty().getValue() == null) continue; DistanceOp op = new DistanceOp( point, (Geometry) f.getDefaultGeometryProperty().getValue()); Coordinate[] co = op.closestPoints(); double[] co0 = featureIterator.close();
/** * 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 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); } }
/** * Mapping specifies station_no --> wq_plus/@id. A FidFilter over wq_plus type should result in * a compare equals filter over the station_no attribute of wq_ir_results simple type. */ @Test public void testUnrollFidMappedToAttribute() throws Exception { String fid = "station_no.1"; Id fidFilter = ff.id(Collections.singleton(ff.featureId(fid))); Filter unrolled = (Filter) fidFilter.accept(visitor, null); assertNotNull(unrolled); FeatureCollection<SimpleFeatureType, SimpleFeature> results = mapping.getSource().getFeatures(unrolled); assertEquals(1, getCount(results)); FeatureIterator<SimpleFeature> features = results.features(); SimpleFeature unmappedFeature = (SimpleFeature) features.next(); features.close(); assertNotNull(unmappedFeature); Object object = unmappedFeature.getProperty("station_no").getValue(); assertEquals(fid, object); }
Query query = new Query(); if (reprojection != null) { if (sourceFeatures.getSchema().getGeometryDescriptor() != null && !this.isReprojectionCrsEqual( this.mappedSource.getSchema().getCoordinateReferenceSystem(), this.reprojection)) { query.setCoordinateSystemReproject(reprojection); matchingFeatures = this.mappedSource.getFeatures(query); try (FeatureIterator<? extends Feature> iterator = matchingFeatures.features()) { while (iterator.hasNext()) { features.add(iterator.next());