SimpleFeatureSource featureSource = store.getFeatureSource(); CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS(); CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true); Query query = new Query(); 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());
private Query reprojectFilter(Query query) throws IOException { SimpleFeatureType nativeFeatureType = source.getSchema(); final GeometryDescriptor geom = nativeFeatureType.getGeometryDescriptor(); Filter filter = query.getFilter() != null ? query.getFilter() : Filter.INCLUDE; Filter defaultedFilter = (Filter) filter.accept(defaultCRSVisitor, null); Filter reprojectedFilter = (Filter) defaultedFilter.accept(reprojectingVisitor, null); Query reprojectedQuery = new Query(query); reprojectedQuery.setFilter(reprojectedFilter); return reprojectedQuery;
public int getCount(Query query) throws IOException { return delegate.getCount(query); }
SimpleFeatureSource featureSource = store.getFeatureSource(); CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS(); CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true); Query query = new Query(); query.setCoordinateSystem(sourceCRS); query.setCoordinateSystemReproject(WGS84); SimpleFeatureCollection featureCollection = featureSource.getFeatures(query); SimpleFeatureIterator it = featureCollection.features(); SimpleFeature protoFt = it.next(); if (propertyFields == null) { propertyFields = new ArrayList<String>(); for (Property p : protoFt.getProperties()) { propertyFields.add(p.getName().toString()); it = featureCollection.features(); ft.setId(Integer.toString(i)); } else { ft.setId(feature.getProperty(originIDField).getValue().toString()); LOG.debug("Property {} of feature {} could not be interpreted as int, skipping", prop.getName().toString(), ft.getId()); continue;
SortBy[] sortBy = query.getSortBy(); Integer offset = null, maxFeatures = null; if (sortBy != null && sortBy != SortBy.UNSORTED) { if (!source.getQueryCapabilities().supportsSorting(sortBy)) { query.setSortBy(null); offset = query.getStartIndex(); maxFeatures = query.getMaxFeatures() == Integer.MAX_VALUE ? null : query.getMaxFeatures(); if (query.getStartIndex() != null) { if (!source.getQueryCapabilities().isOffsetSupported()) { offset = query.getStartIndex(); maxFeatures = try { SimpleFeatureCollection fc = source.getFeatures(newQuery);
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()); }
assertFeatureTypesEqual(td.roadType, road.getSchema()); assertSame(dataStore, road.getDataStore()); int count = road.getCount(Query.ALL); assertTrue((count == 3) || (count == -1)); ReferencedEnvelope bounds = road.getBounds(Query.ALL); assertTrue((bounds == null) || areReferencedEnvelopesEqual(bounds, td.roadBounds)); SimpleFeatureCollection all = road.getFeatures(); assertEquals(3, all.size()); assertTrue(areReferencedEnvelopesEqual(td.roadBounds, all.getBounds())); assertTrue(areReferencedEnvelopesEqual(td.roadBounds, all.getBounds())); SimpleFeatureCollection some = road.getFeatures(td.rd12Filter); assertEquals(2, some.size()); assertTrue(areReferencedEnvelopesEqual(e, some.getBounds())); assertEquals(some.getSchema(), road.getSchema()); Query query = new Query(tname("road"), td.rd12Filter, new String[] {aname("name")}); SimpleFeatureCollection half = road.getFeatures(query); assertEquals(2, half.size()); assertEquals(1, half.getSchema().getAttributeCount());
@Test public void testShapeFileWriter() throws IOException{ String inFile = "src/test/resources/" + utils.getInputDirectory() + "test.shp"; String outFile = utils.getOutputDirectory() + "/test.shp"; SimpleFeatureSource s = ShapeFileReader.readDataFile(inFile); SimpleFeatureCollection fts = s.getFeatures(); SimpleFeatureIterator it = fts.features(); SimpleFeature ft = it.next(); Geometry g = (Geometry) ft.getDefaultGeometry(); List<SimpleFeature> fc = new ArrayList<>(); fc.add(ft); ShapeFileWriter.writeGeometries(fc, outFile); SimpleFeatureSource s1 = ShapeFileReader.readDataFile(outFile); SimpleFeatureCollection fts1 = s1.getFeatures(); SimpleFeatureIterator it1 = fts1.features(); SimpleFeature ft1 = it1.next(); Geometry g1 = (Geometry) ft1.getDefaultGeometry(); Assert.assertEquals(g.getCoordinates().length, g1.getCoordinates().length); }
@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()); }
void assertFeatures(SimpleFeatureSource fs2, Filter filter, String... ids) throws IOException { Set<String> idSet = new HashSet<String>(Arrays.asList(ids)); int count = 0; try (SimpleFeatureIterator fi = fs.getFeatures(filter).features()) { while (fi.hasNext()) { SimpleFeature sf = fi.next(); assertTrue("Found unexpected id " + sf.getID(), idSet.contains(sf.getID())); count++; } } if (count != idSet.size()) { fail("Expected to find " + idSet.size() + " features, but was " + count); } }
@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)); }
GeometryType geometryType = (GeometryType) featureSource.getSchema().getDescriptor(sourceIndex).getType(); GeometryConverter converter = new GeometryConverter(geometryType); SimpleFeatureIterator it = featureSource.getFeatures().features(); while(it.hasNext()) { SimpleFeature feature = it.next(); ResourceId sourceId = ResourceId.valueOf(feature.getID()); ResourceId targetId = idMap.get(sourceId); Geometry geometry = converter.toWgs84(feature.getAttribute(sourceIndex)); System.out.print("Updating geometry for " + targetId + " [" + geometry.getGeometryType() + "] ... ");
/** * Test the first page of one feature per page. * * @throws IOException */ @Test public void oneFeatureFirstPage() throws IOException { Query query = new Query(); query.setMaxFeatures(1); query.setStartIndex(0); SimpleFeatureSource fs = store.getFeatureSource(TYPENAME); SimpleFeature[] features = (SimpleFeature[]) fs.getFeatures(query).toArray(); Assert.assertEquals(1, fs.getCount(query)); Assert.assertEquals("mock.1", features[0].getID()); }
@Test public void testGetFeatureSourceProvidedNamespace() throws Exception { String namespace = "http://www.geogig.org/test"; dataStore.setNamespaceURI(namespace); insertAndAdd(lines1); commit(); SimpleFeatureSource lines; lines = dataStore.getFeatureSource(linesName); Name expectedName = new NameImpl(namespace, linesName); assertEquals(expectedName, lines.getName()); SimpleFeatureType schema = lines.getSchema(); assertEquals(expectedName, schema.getName()); SimpleFeatureCollection features = lines.getFeatures(); assertEquals(expectedName, features.getSchema().getName()); try (SimpleFeatureIterator it = features.features()) { SimpleFeature feature = it.next(); assertEquals(expectedName, feature.getType().getName()); } }
@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")); }
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"));
public DataStoreAdaptor(SimpleFeatureSource source) { this.source = source; this.schema = source.getSchema(); this.name = schema.getName(); this.typeName = name.getLocalPart(); }
private Shapefile load() { if (state.compareAndSet(NOT_LOADED, LOADED)) { try { final List<PreparedGeometry> geometries = new ArrayList<>(); for (String typeName : datastore.getTypeNames()) { SimpleFeatureSource source = datastore.getFeatureSource(typeName); crs = source.getBounds().getCoordinateReferenceSystem(); final SimpleFeatureCollection features = source.getFeatures(); SimpleFeatureIterator it = features.features(); while (it.hasNext()) { SimpleFeature feature = it.next(); Geometry g = (Geometry) feature.getDefaultGeometry(); if (bufferDistance > 0) g = g.buffer(bufferDistance); geometries.add(PreparedGeometryFactory.prepare(g)); this.features.add(feature); } it.close(); } this.geometries = geometries; } catch (IOException e) { throw new RuntimeException(e); } } else if (state.get() == CLOSED) throw new RuntimeException("Shapefile is closed and can't be accessed"); return this; }
SimpleFeatureCollection fc = featureSource.getFeatures(localFilter); SimpleFeatureIterator fi = null; try { fi = fc.features(); if (fi.hasNext()) { SimpleFeature sf = fi.next(); result = (Geometry) sf.getDefaultGeometry(); if (fi.hasNext()) { throw new IOException( "The filter " + localFilter + " matched more than one footprint record, in particular, it matched " + fc.size() + ", the first match is: " + sf);
@Test public void testSimpleRename() throws IOException { SimpleFeatureSource fs = store.getFeatureSource(BUILDINGS.getLocalPart()); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init(fs.getSchema()); tb.setName("houses"); SimpleFeatureType target = tb.buildFeatureType(); SimpleFeatureSource retyped = RetypingFeatureSource.getRetypingSource(fs, target); assertTrue(retyped instanceof SimpleFeatureLocking); assertEquals(target, retyped.getSchema()); assertEquals(target, ((DataStore) retyped.getDataStore()).getSchema("houses")); assertEquals(target, retyped.getFeatures().getSchema()); SimpleFeatureIterator it = retyped.getFeatures().features(); SimpleFeature f = it.next(); it.close(); assertEquals(target, f.getType()); }