static SimpleFeature retype(SimpleFeature source, SimpleFeatureBuilder builder) throws IllegalAttributeException { SimpleFeatureType target = builder.getFeatureType(); for (int i = 0; i < target.getAttributeCount(); i++) { AttributeDescriptor attributeType = target.getDescriptor(i); Object value = null; if (source.getFeatureType().getDescriptor(attributeType.getName()) != null) { value = source.getAttribute(attributeType.getName()); } builder.add(value); } FeatureId id = reTypeId(source.getIdentifier(), source.getFeatureType(), target); SimpleFeature retyped = builder.buildFeature(id.getID()); retyped.getUserData().putAll(source.getUserData()); return retyped; }
SimpleFeatureSource featureSource = store.getFeatureSource(); CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS(); CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true); query.setCoordinateSystem(sourceCRS); query.setCoordinateSystemReproject(WGS84); SimpleFeatureCollection featureCollection = featureSource.getFeatures(query); SimpleFeatureIterator it = featureCollection.features(); int i = 0; while (it.hasNext()) { SimpleFeature feature = it.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); Point point = null; if (geom instanceof Point) { label = Integer.toString(i); } else { label = feature.getAttribute(labelAttribute).toString(); Number n = (Number) feature.getAttribute(inputAttribute); input = n.doubleValue(); it.close(); } catch (Exception ex) { LOG.error("Error loading population from shapefile: {}", ex.getMessage());
@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")); }
/** * Recursively breaks down SimpleFeatures that may contain other features as attributes to * accumulate simple attribute values to a List * * @param attributeValues The List of attribute values * @param feature A SimpleFeature to harvest attributes */ private void accumulateAttributes(SimpleFeature feature) { for (int i = 0; i < feature.getAttributes().size(); i++) { Object attr = feature.getAttribute(i); if (attr instanceof SimpleFeature) { // go forth and harvest attrubutes accumulateAttributes((SimpleFeature) attr); } else { builder.add(attr); } } } }
public void write() throws IOException { try { SimpleFeatureType target = getFeatureType(); for (int i = 0; i < target.getAttributeCount(); i++) { AttributeDescriptor at = target.getDescriptor(i); Object value = retyped.getAttribute(i); current.setAttribute(at.getLocalName(), value); } delegate.write(); } catch (IllegalAttributeException e) { throw (IOException) new IOException("Error occurred while retyping feature").initCause(e); } } }
private void buildSpatialIndex(SimpleFeatureCollection features) { spatialIndex = new STRtree(); SimpleFeatureIterator featureIter = features.features(); try { while (featureIter.hasNext()) { SimpleFeature feature = featureIter.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); NeighborFeature nearFeature = new NeighborFeature(geometry, feature.getID()); spatialIndex.insert(geometry.getEnvelopeInternal(), nearFeature); } } finally { featureIter.close(); } }
public SimpleFeature next() throws NoSuchElementException { SimpleFeature f = delegate.next(); for (Object attribute : f.getAttributes()) { if ((attribute instanceof Geometry) && !(attribute instanceof Point)) { attribute = ((Geometry) attribute).getCentroid(); } fb.add(attribute); } return fb.buildFeature(f.getID()); } }
public void testSimplification() throws Exception { SimpleFeatureSource fs = dataStore.getFeatureSource(tname("road")); if (fs.getSupportedHints().contains(Hints.GEOMETRY_SIMPLIFICATION) == false) return; SimpleFeatureCollection fColl = fs.getFeatures(); Geometry original = null; try (SimpleFeatureIterator iterator = fColl.features()) { if (iterator.hasNext()) { original = (Geometry) iterator.next().getDefaultGeometry(); } } double width = original.getEnvelope().getEnvelopeInternal().getWidth(); Query query = new Query(); Hints hints = new Hints(Hints.GEOMETRY_SIMPLIFICATION, width / 2); query.setHints(hints); Geometry simplified = null; fColl = fs.getFeatures(query); try (SimpleFeatureIterator iterator = fColl.features()) { if (iterator.hasNext()) simplified = (Geometry) iterator.next().getDefaultGeometry(); } assertTrue(original.getNumPoints() >= simplified.getNumPoints()); }
public SimpleFeature next() throws NoSuchElementException { final SimpleFeature next = delegate.next(); // check all write protected attributes are null final SimpleFeatureType featureType = next.getFeatureType(); for (AttributeDescriptor att : featureType.getAttributeDescriptors()) { String name = att.getLocalName(); if (!writableAttributes.contains(name)) { Object value = next.getAttribute(name); if (value != null) { String typeName = getSchema().getName().getLocalPart(); if (response == Response.CHALLENGE) { throw SecureCatalogImpl.unauthorizedAccess(typeName); } else { throw new UnsupportedOperationException( "Trying to write on the write protected attribute " + name); } } } } return next; } }
@Test public void testModify() throws Exception { final Query queryAll = new Query(RENAMED); SimpleFeatureStore store; store = (SimpleFeatureStore) rts.getFeatureSource(RENAMED); SimpleFeature original = store.getFeatures(fidFilter).features().next(); String newAddress = ((String) original.getAttribute("ADDRESS")) + " xxx"; store.modifyFeatures( original.getFeatureType().getDescriptor("ADDRESS"), newAddress, fidFilter); SimpleFeature modified = store.getFeatures(fidFilter).features().next(); assertEquals(newAddress, modified.getAttribute("ADDRESS")); }
public void testGetFeatureStoreModifyFeatures1() throws IOException { SimpleFeatureStore road = (SimpleFeatureStore) data.getFeatureSource("road"); AttributeDescriptor descriptor = roadType.getDescriptor("name"); road.modifyFeatures(descriptor.getName(), "changed", rd1Filter); SimpleFeatureCollection results = road.getFeatures(rd1Filter); assertEquals("changed", results.features().next().getAttribute("name")); }
SimpleFeature reproject(SimpleFeature feature) throws IOException { Object[] attributes = new Object[schema.getAttributeCount()]; AttributeDescriptor type = schema.getDescriptor(i); Object object = feature.getAttribute(type.getName()); SimpleFeature f = SimpleFeatureBuilder.build(schema, attributes, feature.getID()); f.getUserData().putAll(feature.getUserData()); return f; } catch (IllegalAttributeException e) {
private void writeToShapefile( SimpleFeature f, FeatureWriter<SimpleFeatureType, SimpleFeature> writer) throws IOException { SimpleFeature fw = writer.next(); // we cannot trust attribute order, shapefile changes the location and name of the geometry for (AttributeDescriptor d : fw.getFeatureType().getAttributeDescriptors()) { fw.setAttribute(d.getLocalName(), f.getAttribute(d.getLocalName())); } fw.setDefaultGeometry(f.getDefaultGeometry()); writer.write(); }
tb.init(fs.getSchema()); tb.setName(BRIDGES.getLocalPart()); SimpleFeatureType target = tb.buildFeatureType(); assertEquals(target, retyped.getSchema()); DataStore rs = (DataStore) retyped.getDataStore(); assertEquals(1, rs.getTypeNames().length); assertEquals(BRIDGES.getLocalPart(), rs.getTypeNames()[0]); Filter fidFilter = ff.id(Collections.singleton(ff.featureId(fid))); SimpleFeatureIterator it = retyped.getFeatures(fidFilter).features(); assertTrue(it.hasNext()); SimpleFeature f = it.next(); assertFalse(it.hasNext()); it.close(); assertEquals("114", f.getAttribute("FID")); assertEquals("215 Main Street", f.getAttribute("ADDRESS"));
@Test public void testGetFeaturesFeatureSource() throws Exception { // check the schemas in feature source and feature collection SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); assertEquals(RENAMED, fs.getSchema().getName().getLocalPart()); SimpleFeatureCollection fc = fs.getFeatures(); assertEquals(RENAMED, fc.getSchema().getName().getLocalPart()); 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(RENAMED, sf.getFeatureType().getName().getLocalPart()); // 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)); }
@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()); }
protected Set<String> collectFeatureIds(SimpleFeatureCollection fc) { Set<String> identifiers = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); try (SimpleFeatureIterator fi = fc.features()) { while (fi.hasNext()) { SimpleFeature sf = fi.next(); identifiers.add(sf.getID()); } } return identifiers; } }
SimpleFeatureSource featureSource = store.getFeatureSource(); CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS(); CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true); query.setCoordinateSystem(sourceCRS); query.setCoordinateSystemReproject(WGS84); SimpleFeatureCollection featureCollection = featureSource.getFeatures(query); for (Property p : protoFt.getProperties()) { propertyFields.add(p.getName().toString()); it = featureCollection.features(); PointSet ret = new PointSet(featureCollection.size()); int i=0; while (it.hasNext()) { SimpleFeature feature = it.next(); Geometry geom = (Geometry) feature.getDefaultGeometry(); ft.setId(Integer.toString(i)); } else { ft.setId(feature.getProperty(originIDField).getValue().toString()); for(Property prop : feature.getProperties() ){ String propName = prop.getName().toString(); if (propertyFields.contains(propName)) {
private STRtree buildIndex(SimpleFeatureCollection points) { STRtree spatialIndex = new STRtree(); SimpleFeatureIterator featureIter = points.features(); try { while (featureIter.hasNext()) { SimpleFeature feature = featureIter.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); Event event = new Event(feature.getID(), geometry.getCoordinate()); spatialIndex.insert(new Envelope(geometry.getCoordinate()), event); } } finally { featureIter.close(); } return spatialIndex; }
void assertDatesMatch(Query query, String... dates) throws Exception { SimpleFeatureSource source = dataStore.getFeatureSource(tname("dates")); assertEquals(dates.length, source.getCount(query)); SimpleFeatureCollection features = source.getFeatures(query); try (SimpleFeatureIterator it = features.features()) { int i = 0; while (it.hasNext()) { SimpleFeature f = it.next(); Date expected = date(dates[i++]); assertEquals( Converters.convert(expected, Timestamp.class), f.getAttribute(aname("dt"))); } } }