@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; }
final FilteringFeatureCollection<T, F> filtered = new FilteringFeatureCollection<T, F>(collection, writeQuery.getFilter()); if (filtered.size() < collection.size()) { String typeName = getSchema().getName().getLocalPart(); if (policy.response == Response.CHALLENGE) { throw SecureCatalogImpl.unauthorizedAccess(typeName); return storeDelegate.addFeatures(collection); } else { if (collection.getSchema() instanceof SimpleFeatureType && storeDelegate instanceof SimpleFeatureStore) {
/** * Retreives the bounds for a feature source. * * <p>If the feautre source can calculate the bounds directly, those bounds are returned. * Otherwise, the underlying feature collection is retreived and asked to calculate bounds. If * that fails, an empty envelope is returned. * * @param fs The feature source. * @return The bounds. * @throws IOException Execption calculating bounds on feature source. */ public static ReferencedEnvelope getBoundingBoxEnvelope( FeatureSource<? extends FeatureType, ? extends Feature> fs) throws IOException { ReferencedEnvelope ev = fs.getBounds(); if ((ev == null) || ev.isNull()) { try { ev = fs.getFeatures().getBounds(); } catch (Throwable t) { LOGGER.log( Level.FINE, "Could not compute the data bounding box. Returning an empty envelope", t); ev = new ReferencedEnvelope(fs.getSchema().getCoordinateReferenceSystem()); } } return ev; } }
@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 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 mapping multi-valued simple properties still works. * * @throws Exception */ @Test public void testMultiValuedSimpleProperties() throws Exception { FeatureIterator<Feature> iterator = ccFeatures.features(); while (iterator.hasNext()) { Feature next = iterator.next(); Collection<Property> names = next.getProperties("name"); // these are gml:name and gsml:name, so count twice if (next.getIdentifier().toString().equals("cc.1")) { // see ControlledConcept.properties where id = cc.1 assertEquals(6, names.size()); } else { // see ControlledConcept.properties where id = cc.2 assertEquals(2, names.size()); } } iterator.close(); }
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();
/** 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); } }
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) {}
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"); FeatureType targetFeatureType = createTargetFeatureType(featureCollection.getSchema()); Feature nearestFeature = null; double nearestDistance = 9e9; FeatureIterator<Feature> featureIterator = null; try { featureIterator = featureCollection.features(); while (featureIterator.hasNext()) { SimpleFeature f = (SimpleFeature) featureIterator.next(); if (f.getDefaultGeometryProperty().getValue() == null) continue; DistanceOp op = new DistanceOp(
/** * 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); } }
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());
/** * 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());
@Test /* * Test Bbox function with no property name specified. The default geometry should be used. */ public void testBBoxWithNoPropertyName() throws IOException { // default geometry exists // in theory, when property name is not specified, the filter should apply to all the // geometry attributes, // but it wouldn't work until the bug in GeometryFilterImpl is fixed // and our test data only have 1 geometry, so it doesn't test multiple geometries case BBOX filter = ff.bbox(ff.property(""), -1.1, 52.5, -1.1, 52.6, null); FeatureCollection<FeatureType, Feature> features = fSource.getFeatures(filter); assertEquals(2, size(features)); FeatureIterator<Feature> iterator = features.features(); try { Feature f = iterator.next(); assertEquals(f.getIdentifier().toString(), "mf1"); f = iterator.next(); assertEquals(f.getIdentifier().toString(), "mf3"); } finally { iterator.close(); } }
FeatureCollection features = (FeatureCollection) fSource.getFeatures(); FeatureIterator iterator = features.features(); while (iterator.hasNext()) { Feature next = iterator.next(); Collection<Property> children = next.getProperties("nestedFeature"); if (next.getIdentifier().toString().equals("cc.1")) { Object value = nestedFeature.getValue(); assertNotNull(value); value = ((Collection) value).iterator().next(); assertTrue(value instanceof FeatureImpl); Feature feature = (Feature) value; assertNotNull(feature.getProperty("someAttribute").getValue()); features = (FeatureCollection) fSource.getFeatures(); iterator = features.features(); while (iterator.hasNext()) { Feature next = iterator.next(); Collection<Property> children = next.getProperties("nestedFeature"); Object value = nestedFeature.getValue(); assertNotNull(value); value = ((Collection) value).iterator().next();
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"); createTargetFeatureType(featureCollection.getSchema()); 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;
@Test public void testGetFeatureReader() throws IOException { FeatureSource<FeatureType, Feature> access = dataStore.getFeatureSource(targetName); FeatureType type = access.getSchema(); assertEquals(targetType, type); FeatureCollection<FeatureType, Feature> reader = access.getFeatures(); assertNotNull(reader); FeatureIterator<Feature> features = reader.features(); assertTrue(features.hasNext()); Feature complexFeature = (Feature) features.next(); assertNotNull(complexFeature); assertEquals(targetType, complexFeature.getType()); features.close();
public void testGetFeatureId() throws Exception { FeatureSource fsView = dataStore.getFeatureSource("riverReducedPk"); assertFalse(fsView instanceof FeatureStore); assertEquals(1, fsView.getCount(Query.ALL)); try (FeatureIterator<SimpleFeature> it = fsView.getFeatures().features()) { assertTrue(it.hasNext()); SimpleFeature sf = it.next(); // check the primary key is build out of the fid attribute assertEquals("riverReducedPk.0", sf.getID()); } }