Search in sources :

Example 21 with FeatureReader

use of org.geotoolkit.storage.feature.FeatureReader 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();
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Query(org.geotoolkit.storage.feature.query.Query) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) Coordinate(org.locationtech.jts.geom.Coordinate) Filter(org.opengis.filter.Filter) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) LinearRing(org.locationtech.jts.geom.LinearRing) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) URL(java.net.URL) Test(org.junit.Test)

Example 22 with FeatureReader

use of org.geotoolkit.storage.feature.FeatureReader 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);
    }
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) Coordinate(org.locationtech.jts.geom.Coordinate) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) File(java.io.File) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 23 with FeatureReader

use of org.geotoolkit.storage.feature.FeatureReader in project geotoolkit by Geomatys.

the class IndexedShapefileDataStoreTest method count.

private int count(final FeatureStore ds, final String typeName, final Filter filter) throws Exception {
    FeatureReader reader;
    reader = ds.getFeatureReader(Query.filtered(typeName, filter));
    int count = 0;
    try {
        while (reader.hasNext()) {
            reader.next();
            count++;
        }
    } finally {
        reader.close();
    }
    return count;
}
Also used : FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Point(org.locationtech.jts.geom.Point)

Example 24 with FeatureReader

use of org.geotoolkit.storage.feature.FeatureReader in project geotoolkit by Geomatys.

the class IndexedShapefileFeatureStore method getFeatureReader.

/**
 * Use the spatial index if available and adds a small optimization: if no
 * attributes are going to be read, don't uselessly open and read the dbf
 * file.
 */
@Override
public FeatureReader getFeatureReader(final Query query) throws DataStoreException {
    if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
        throw new UnsupportedQueryException();
    final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
    final FeatureType baseType = getFeatureType();
    final String queryTypeName = gquery.getTypeName();
    final String[] queryPropertyNames = gquery.getPropertyNames();
    final Hints queryHints = gquery.getHints();
    final double[] queryRes = gquery.getResolution();
    Filter queryFilter = gquery.getSelection();
    // check if we must read the 3d values
    final boolean read3D = true;
    // find the properties we will read and return --------------------------
    final AttributeType idAttribute = (AttributeType) baseType.getProperty(AttributeConvention.IDENTIFIER);
    Set<AttributeType> readProperties;
    Set<PropertyType> returnedProperties;
    if (queryPropertyNames == null) {
        // return all properties. Note : preserve order by using a linked set implementation
        readProperties = new LinkedHashSet<>(getAttributes(baseType, true));
        returnedProperties = new LinkedHashSet<>((Collection) baseType.getProperties(true));
    } else {
        // return only a subset of properties. Note : preserve order by using a linked set implementation
        readProperties = new LinkedHashSet<>(queryPropertyNames.length);
        returnedProperties = new LinkedHashSet<>(queryPropertyNames.length);
        for (String n : queryPropertyNames) {
            final PropertyType cdt = baseType.getProperty(n);
            if (cdt instanceof AttributeType) {
                readProperties.add((AttributeType) cdt);
            } else if (cdt instanceof AbstractOperation) {
                final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
                for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
            }
            returnedProperties.add(cdt);
        }
        // add filter properties
        final FilterAttributeExtractor fae = new FilterAttributeExtractor();
        fae.visit(queryFilter, null);
        final Set<GenericName> filterPropertyNames = fae.getAttributeNameSet();
        for (GenericName n : filterPropertyNames) {
            final PropertyType cdt = baseType.getProperty(n.toString());
            if (cdt instanceof AttributeType) {
                readProperties.add((AttributeType) cdt);
            } else if (cdt instanceof AbstractOperation) {
                final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
                for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
            }
        }
    }
    final Set<PropertyType> allProperties = new LinkedHashSet<>(returnedProperties);
    allProperties.addAll(readProperties);
    // create a reader ------------------------------------------------------
    final FeatureType readType;
    final FeatureReader reader;
    try {
        final GenericName[] readPropertyNames = new GenericName[allProperties.size()];
        int i = 0;
        for (PropertyType prop : allProperties) {
            readPropertyNames[i++] = prop.getName();
        }
        readType = FeatureTypeExt.createSubType(baseType, readPropertyNames);
        if (queryFilter.getOperatorType() == SpatialOperatorName.BBOX) {
            // in case we have a BBOX filter only, which is very commun, we can speed
            // the process by relying on the quadtree estimations
            final Envelope bbox = ExtractBoundsFilterVisitor.bbox(queryFilter, null);
            final boolean loose = (queryFilter instanceof LooseBBox);
            queryFilter = Filter.include();
            final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
            attsProperties.remove(idAttribute);
            reader = createFeatureReader(getBBoxAttributesReader(attsProperties, bbox, loose, queryHints, read3D, queryRes), readType, queryHints);
        } else if (queryFilter instanceof ResourceId && ((ResourceId) queryFilter).getIdentifier() == null) {
            // in case we have an empty id set (TODO: should never happen, maybe we should remove this case).
            return FeatureStreams.emptyReader(getFeatureType());
        } else {
            final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
            attsProperties.remove(idAttribute);
            reader = createFeatureReader(getAttributesReader(attsProperties, queryFilter, read3D, queryRes), readType, queryHints);
        }
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
    // handle remaining query parameters ------------------------------------
    final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query(queryTypeName);
    if (readProperties.equals(returnedProperties)) {
        qb.setProperties(queryPropertyNames);
    }
    qb.setSelection(queryFilter);
    qb.setHints(queryHints);
    qb.setSortBy(gquery.getSortBy());
    qb.setOffset(gquery.getOffset());
    gquery.getLimit().ifPresent(qb::setLimit);
    return FeatureStreams.subset(reader, qb);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) Hints(org.geotoolkit.factory.Hints) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) PropertyType(org.opengis.feature.PropertyType) Envelope(org.locationtech.jts.geom.Envelope) GenericName(org.opengis.util.GenericName) LooseBBox(org.geotoolkit.filter.binaryspatial.LooseBBox) AttributeType(org.opengis.feature.AttributeType) ShapefileFeatureReader(org.geotoolkit.data.shapefile.ShapefileFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) AbstractOperation(org.apache.sis.feature.AbstractOperation) FilterAttributeExtractor(org.geotoolkit.filter.visitor.FilterAttributeExtractor) Filter(org.opengis.filter.Filter) ResourceId(org.opengis.filter.ResourceId) CloseableCollection(org.geotoolkit.index.CloseableCollection)

Aggregations

FeatureReader (org.geotoolkit.storage.feature.FeatureReader)24 Test (org.junit.Test)11 FeatureType (org.opengis.feature.FeatureType)11 DataStoreException (org.apache.sis.storage.DataStoreException)10 Query (org.apache.sis.storage.Query)10 Feature (org.opengis.feature.Feature)9 Query (org.geotoolkit.storage.feature.query.Query)8 IOException (java.io.IOException)7 UnsupportedQueryException (org.apache.sis.storage.UnsupportedQueryException)7 Filter (org.opengis.filter.Filter)6 URL (java.net.URL)5 Hints (org.geotoolkit.factory.Hints)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 Point (org.locationtech.jts.geom.Point)5 ResourceId (org.opengis.filter.ResourceId)5 GenericName (org.opengis.util.GenericName)5 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)4 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)4 MalformedURLException (java.net.MalformedURLException)3 ArrayList (java.util.ArrayList)3