use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class IndexedShapefileDataStoreTest method runWriteReadTest.
private void runWriteReadTest(final Geometry geom, final boolean d3) throws Exception {
// make features
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("Junk");
ftb.addAttribute(geom.getClass()).setName("a").setCRS(CommonCRS.WGS84.normalizedGeographic());
final FeatureType type = ftb.build();
final Collection<Feature> features = new ArrayList<>();
for (int i = 0, ii = 20; i < ii; i++) {
Feature feature = type.newInstance();
feature.setPropertyValue("a", geom.clone());
features.add(feature);
}
// set up file
File tmpFile = getTempFile();
tmpFile.delete();
// write features
IndexedShapefileFeatureStore s = new IndexedShapefileFeatureStore(tmpFile.toURI());
s.createFeatureType(type);
writeFeatures(s, features);
s.close();
// read features
s = new IndexedShapefileFeatureStore(tmpFile.toURI());
FeatureCollection fc = loadFeatures(s);
FeatureIterator fci = fc.iterator();
// verify
try {
while (fci.hasNext()) {
Feature f = fci.next();
Geometry fromShape = FeatureExt.getDefaultGeometryValue(f).filter(Geometry.class::isInstance).map(Geometry.class::cast).orElseThrow(() -> new IllegalArgumentException("No geometry found in feature " + f));
if (fromShape instanceof GeometryCollection) {
if (!(geom instanceof GeometryCollection)) {
fromShape = ((GeometryCollection) fromShape).getGeometryN(0);
}
}
try {
Coordinate[] c1 = geom.getCoordinates();
Coordinate[] c2 = fromShape.getCoordinates();
for (int cc = 0, ccc = c1.length; cc < ccc; cc++) {
if (d3) {
assertTrue(c1[cc].equals3D(c2[cc]));
} else {
assertTrue(c1[cc].equals2D(c2[cc]));
}
}
} catch (Throwable t) {
fail("Bogus : " + Arrays.asList(geom.getCoordinates()) + " : " + Arrays.asList(fromShape.getCoordinates()));
}
}
} finally {
fci.close();
s.close();
tmpFile.delete();
}
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class IndexedShapefileDataStoreTest method testUpdating.
/**
* Create a set of features, then remove every other one, updating the
* remaining. Test for removal and proper update after reloading...
*/
@Test
public void testUpdating() throws Throwable {
IndexedShapefileFeatureStore sds = createDataStore();
loadFeatures(sds);
FeatureWriter writer = null;
try {
writer = sds.getFeatureWriter(new Query(sds.getName()));
while (writer.hasNext()) {
Feature feat = writer.next();
Byte b = (Byte) feat.getPropertyValue("b");
if ((b.byteValue() % 2) == 0) {
writer.remove();
} else {
feat.setPropertyValue("b", new Byte((byte) -1));
}
}
} finally {
if (writer != null) {
writer.close();
}
}
FeatureCollection fc = loadFeatures(sds);
assertEquals(10, fc.size());
FeatureIterator i = fc.iterator();
for (; i.hasNext(); ) {
assertEquals(-1, ((Byte) i.next().getPropertyValue("b")).byteValue());
}
i.close();
sds.close();
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class ShapefileQuadTreeReadWriteTest method testGetBoundsQuery.
/**
* Test optimized getBounds(). Testing when filter is a bbox filter and a fidfilter
*
* @throws Exception
*/
@Test
// fails randomly, urgent need to write shapefile store in SIS
@Ignore
public void testGetBoundsQuery() throws Exception {
File file = copyShapefiles("shapes/streams.shp");
ShapefileProvider fac = new ShapefileProvider();
final ParameterValueGroup params = fac.getOpenParameters().createValue();
params.parameter(ShapefileProvider.LOCATION).setValue(file.toURI());
params.parameter(ShapefileProvider.CREATE_SPATIAL_INDEX.getName().toString()).setValue(Boolean.TRUE);
IndexedShapefileFeatureStore ds = (IndexedShapefileFeatureStore) fac.open(params);
FilterFactory2 ff = FilterUtilities.FF;
ResourceId filter = ff.resourceId("streams.84");
FeatureIterator iter = ds.getFeatureReader(Query.filtered(ds.getName().toString(), filter));
JTSEnvelope2D bounds;
try {
bounds = new JTSEnvelope2D(FeatureExt.getEnvelope(iter.next()));
} finally {
iter.close();
}
Query query = Query.filtered(ds.getNames().iterator().next().toString(), filter);
Envelope result = (Envelope) ds.getEnvelope(query);
assertTrue(result.equals(bounds));
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class FidQueryTest method assertFidsMatch.
private void assertFidsMatch() throws IOException, DataStoreException {
int i = 0;
for (Iterator<Entry<String, Feature>> iter = fids.entrySet().iterator(); iter.hasNext(); ) {
i++;
final Entry<String, Feature> entry = iter.next();
final String fid = (String) entry.getKey();
final Filter filter = fac.resourceId(fid);
final Query query = Query.filtered(name.toString(), filter);
final FeatureIterator features = ds.getFeatureReader(query);
try {
final Feature feature = features.next();
assertFalse(features.hasNext());
assertEquals(i + "th feature", entry.getValue(), feature);
} finally {
if (features != null)
features.close();
}
}
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class FidQueryTest method testFIDBBoxQuery.
@Test
public void testFIDBBoxQuery() throws Exception {
FeatureIterator features = ds.getFeatureReader(new Query(name));
Feature feature;
try {
feature = features.next();
feature = features.next();
feature = features.next();
} finally {
if (features != null)
features.close();
}
// FilterFactory factory = FilterFactoryFinder.createFilterFactory();
// BBoxExpression bb =
// factory.createBBoxExpression(feature.getBounds());
//
// GeometryFilter bboxFilter =
// factory.createGeometryFilter(FilterType.GEOMETRY_INTERSECTS);
// bboxFilter.addRightGeometry(bb);
//
// String geom = ds.getSchema().getDefaultGeometryValue().getLocalName();
//
// bboxFilter.addLeftGeometry(factory.createAttributeExpression(geom));
SpatialOperator bbox = fac.bbox(fac.property(""), FeatureExt.getEnvelope(feature));
features = ds.getFeatureReader(Query.filtered(name.toString(), bbox));
try {
while (features.hasNext()) {
Feature newFeature = features.next();
assertEquals(newFeature, fids.get(FeatureExt.getId(newFeature).getIdentifier()));
}
} finally {
if (features != null)
features.close();
}
}
Aggregations