use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method testGetCount.
@Test
public void testGetCount() throws Exception {
assertTrue(copyShapefiles(STREAM).canRead());
// The following test seems to fail in the URL point into the JAR file.
ShapefileFeatureStore store = (ShapefileFeatureStore) new ShapefileProvider().createDataStore(TestData.url(AbstractTestCaseSupport.class, STREAM).toURI());
int count = 0;
try (FeatureReader reader = store.getFeatureReader(new Query(store.getNames().iterator().next()))) {
while (reader.hasNext()) {
count++;
reader.next();
}
assertEquals(count, store.getCount(new Query(store.getNames().iterator().next())));
}
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method testWriteShapefileWithNoRecords.
@Test
public void testWriteShapefileWithNoRecords() throws Exception {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder().setName("whatever");
ftb.addAttribute(Polygon.class).setName("a").addRole(AttributeRole.DEFAULT_GEOMETRY);
ftb.addAttribute(String.class).setName("b");
FeatureType featureType = ftb.build();
File tempFile = getTempFile();
ShapefileFeatureStore shapefileFeatureStore = new ShapefileFeatureStore(tempFile.toURI());
shapefileFeatureStore.createFeatureType(featureType);
FeatureWriter featureWriter = shapefileFeatureStore.getFeatureWriter(new Query(shapefileFeatureStore.getName()));
// don't add any features to the data store....
// this should open a shapefile with no records. Not sure about the
// semantics of this,
// but it's meant to be used in the context of a FeatureCollection
// iteration,
// where the FeatureCollection<SimpleFeatureType, SimpleFeature> has nothing in it.
featureWriter.close();
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method writeFeatures.
private void writeFeatures(final ShapefileFeatureStore s, final Collection<Feature> fc) throws Exception {
final FeatureType sft = fc.iterator().next().getType();
s.createFeatureType(sft);
try (FeatureWriter fw = s.getFeatureWriter(new Query(sft.getName()))) {
Iterator<Feature> it = fc.iterator();
while (it.hasNext()) {
Feature feature = it.next();
Feature newFeature = fw.next();
FeatureExt.copy(feature, newFeature, false);
fw.write();
}
}
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method testGetReaderOptimizations.
/**
* Checks if feature reading optimizations still allow to execute the
* queries or not
*/
@Test
public void testGetReaderOptimizations() throws Exception {
URL url = ShapeTestData.url(STATE_POP);
ShapefileFeatureStore s = new ShapefileFeatureStore(url.toURI());
// attributes other than geometry can be ignored here
Query builder = new Query();
builder.setTypeName(s.getNames().iterator().next());
builder.setSelection(Filter.include());
builder.setProperties(new String[] { "the_geom" });
Query query = builder;
FeatureReader reader = s.getFeatureReader(query);
assertEquals(1, reader.getFeatureType().getProperties(true).size());
assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
// here too, the filter is using the geometry only
GeometryFactory gc = org.geotoolkit.geometry.jts.JTS.getFactory();
LinearRing ring = gc.createLinearRing(new Coordinate[] { new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(10, 10), new Coordinate(0, 10), new Coordinate(0, 0) });
Polygon polygon = gc.createPolygon(ring, null);
JTSEnvelope2D bounds = new JTSEnvelope2D(polygon.getEnvelopeInternal(), null);
Filter gf = ff.bbox(ff.property("the_geom"), bounds);
builder = new Query();
builder.setTypeName(s.getNames().iterator().next());
builder.setSelection(gf);
builder.setProperties(new String[] { "the_geom" });
query = builder;
reader.close();
reader = s.getFeatureReader(query);
assertEquals(1, reader.getFeatureType().getProperties(true).size());
assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
reader.close();
// here not, we need state_name in the feature type, so open the dbf
// file please
Filter cf = ff.equal(ff.property("STATE_NAME"), ff.literal("Illinois"));
builder = new Query();
builder.setTypeName(s.getNames().iterator().next());
builder.setSelection(cf);
builder.setProperties(new String[] { "the_geom" });
query = builder;
reader = s.getFeatureReader(query);
assertEquals(1, reader.getFeatureType().getProperties(true).size());
assertEquals("the_geom", reader.getFeatureType().getProperties(true).iterator().next().getName().tip().toString());
reader.close();
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method testWriteReadBigNumbers.
@Test
public void testWriteReadBigNumbers() throws Exception {
// open feature type
final FeatureTypeBuilder ftb = new FeatureTypeBuilder().setName("junk");
ftb.addAttribute(Point.class).setName("a").addRole(AttributeRole.DEFAULT_GEOMETRY);
ftb.addAttribute(BigDecimal.class).setName("b");
ftb.addAttribute(BigInteger.class).setName("c");
FeatureType type = ftb.build();
Collection<Feature> features = new ArrayList<>();
BigInteger bigInteger = new BigInteger("1234567890123456789");
BigDecimal bigDecimal = new BigDecimal(bigInteger, 2);
final Feature feature = type.newInstance();
feature.setPropertyValue("a", org.geotoolkit.geometry.jts.JTS.getFactory().createPoint(new Coordinate(1, -1)));
feature.setPropertyValue("b", bigDecimal);
feature.setPropertyValue("c", bigInteger);
features.add(feature);
// store features
File tmpFile = getTempFile();
tmpFile.createNewFile();
ShapefileFeatureStore s = new ShapefileFeatureStore(tmpFile.toURI());
writeFeatures(s, features);
try (// read them back
FeatureReader reader = s.getFeatureReader(new Query(type.getName()))) {
Feature f = reader.next();
assertEquals("big decimal", bigDecimal.doubleValue(), ((Number) f.getPropertyValue("b")).doubleValue(), 0.00001);
assertEquals("big integer", bigInteger.longValue(), ((Number) f.getPropertyValue("c")).longValue(), 0.00001);
}
}
Aggregations