Search in sources :

Example 6 with FeatureReader

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

the class PostgresSimpleTypeTest method testUglyTableName.

/**
 * Test ugly named table
 */
@Test
public void testUglyTableName() throws Exception {
    reload(true);
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test'te#st$'test\"'test");
    ftb.addAttribute(String.class).setName("text");
    final FeatureType ft = ftb.build();
    store.createFeatureType(ft);
    final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
    final Feature record = resType.newInstance();
    record.setPropertyValue("text", "un'deux'trois'quatre");
    List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(record));
    assertEquals(1, addedIds.size());
    assertEquals(FF.resourceId("1"), addedIds.get(0));
    final Query query = new Query(resType.getName());
    final FeatureReader ite = store.getFeatureReader(query);
    boolean found = false;
    try {
        while (ite.hasNext()) {
            final Feature feature = ite.next();
            Object val = feature.getPropertyValue("text");
            assertEquals("un'deux'trois'quatre", val);
            found = true;
        }
    } finally {
        ite.close();
    }
    assertTrue(found);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) ResourceId(org.opengis.filter.ResourceId) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 7 with FeatureReader

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

the class ShapefileFeatureStore method getFeatureWriter.

/**
 * {@inheritDoc }
 */
@Override
public FeatureWriter getFeatureWriter(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;
    FeatureType type = getFeatureType(gquery.getTypeName());
    final ShapefileAttributeReader attReader = getAttributesReader(true, true, null);
    final FeatureIDReader idReader = new DefaultFeatureIDReader(type.getName().tip().toString());
    FeatureReader featureReader;
    try {
        featureReader = ShapefileFeatureReader.create(attReader, idReader, schema, gquery.getHints());
    } catch (Exception e) {
        featureReader = FeatureStreams.emptyReader(schema);
    }
    try {
        return FeatureStreams.filter(new ShapefileFeatureWriter(this, type.getName().tip().toString(), shpFiles, attReader, featureReader, dbfCharset), gquery.getSelection());
    } catch (Exception ex) {
        try {
            featureReader.close();
        } catch (Exception bis) {
            ex.addSuppressed(bis);
        }
        throw new DataStoreException(ex);
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.apache.sis.storage.Query) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 8 with FeatureReader

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

the class IndexedShapefileFeatureStore method getFeatureWriter.

/**
 * Create a FeatureWriter for the given type name.
 *
 * @param query The typeName of the FeatureType to write
 * @return A new FeatureWriter.
 * @throws DataStoreException If the typeName is not available or some other error occurs.
 */
@Override
public FeatureWriter getFeatureWriter(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;
    // will raise an error if it does not exist
    final FeatureType schema = getFeatureType(gquery.getTypeName());
    // we read all properties
    final IndexedShapefileAttributeReader attReader = getAttributesReader(getAttributes(schema, false), Filter.include(), true, null);
    try {
        final FeatureReader reader = createFeatureReader(attReader, schema, null);
        FeatureWriter writer = new IndexedShapefileFeatureWriter(schema.getName().tip().toString(), shpFiles, attReader, reader, this, dbfCharset);
        return FeatureStreams.filter(writer, gquery.getSelection());
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.apache.sis.storage.Query) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) IOException(java.io.IOException) ShapefileFeatureReader(org.geotoolkit.data.shapefile.ShapefileFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 9 with FeatureReader

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

the class ExtendedFeatureStore method getFeatureReader.

@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 String typeName = gquery.getTypeName();
    if (queries.get(this, typeName) != null) {
        final Query original = queries.get(this, typeName);
        final FeatureReader baseReader = wrapped.getFeatureReader(original);
        return FeatureStreams.subset(baseReader, gquery);
    }
    return wrapped.getFeatureReader(query);
}
Also used : Query(org.apache.sis.storage.Query) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) FeatureReader(org.geotoolkit.storage.feature.FeatureReader)

Example 10 with FeatureReader

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

the class DefaultJDBCFeatureStore method getQOMFeatureReader.

/**
 * Get reader with geotk query model.
 */
private FeatureReader getQOMFeatureReader(final org.geotoolkit.storage.feature.query.Query query, Connection cnx) throws DataStoreException {
    final String dbSchemaName = getDatabaseSchema();
    final FeatureType type = dbmodel.getFeatureType(query.getTypeName());
    final String tableName = type.getName().tip().toString();
    TableMetaModel tableMeta = null;
    if (dbSchemaName == null) {
        // Try to handle empty schema name given at configuration
        for (final SchemaMetaModel scheme : dbmodel.getSchemaMetaModels()) {
            final TableMetaModel tableMetaTemp = scheme.getTable(tableName);
            if (tableMetaTemp != null) {
                tableMeta = tableMetaTemp;
                break;
            }
        }
    } else {
        tableMeta = dbmodel.getSchemaMetaModel(getDatabaseSchema()).getTable(tableName);
    }
    if (tableMeta == null) {
        throw new DataStoreException("Unable to get table " + tableName + " in the database.");
    }
    final FeatureType tableType = tableMeta.getType(TableMetaModel.View.ALLCOMPLEX).build();
    final PrimaryKey pkey = dbmodel.getPrimaryKey(query.getTypeName());
    // replace any PropertyEqualsTo in true ID filters
    Filter baseFilter = query.getSelection();
    baseFilter = (Filter) FIDFixVisitor.INSTANCE.visit(baseFilter);
    // split the filter between what can be send and must be handle by code
    final Filter[] divided = getDialect().splitFilter(baseFilter, tableType);
    Filter preFilter = divided[0];
    Filter postFilter = divided[1];
    // ensure spatial filters are in featuretype geometry crs
    preFilter = (Filter) new CRSAdaptorVisitor(tableType).visit(preFilter);
    // rebuild a new query with the same params, but just the pre-filter
    final org.geotoolkit.storage.feature.query.Query builder = new org.geotoolkit.storage.feature.query.Query();
    builder.copy(query);
    builder.setSelection(preFilter);
    if (query.getResolution() != null) {
        // attach resampling in hints; used later by postgis dialect
        builder.getHints().add(new Hints(RESAMPLING, query.getResolution()));
    }
    final org.geotoolkit.storage.feature.query.Query preQuery = builder;
    final FeatureType baseType = getFeatureType(query.getTypeName());
    // Build the feature type returned by this query. Also build an eventual extra feature type
    // containing the attributes we might need in order to evaluate the post filter
    final FeatureType queryFeatureType;
    final FeatureType returnedFeatureType;
    if (query.retrieveAllProperties()) {
        returnedFeatureType = queryFeatureType = (FeatureType) baseType;
    } else {
        // TODO BUG here, query with filter on a not returned geometry field crash
        returnedFeatureType = (FeatureType) FeatureTypeExt.createSubType(tableType, query.getPropertyNames());
        final FilterAttributeExtractor extractor = new FilterAttributeExtractor(tableType);
        extractor.visit(postFilter, null);
        final GenericName[] extraAttributes = extractor.getAttributeNames();
        final List<GenericName> allAttributes = new ArrayList<>();
        for (String str : query.getPropertyNames()) {
            allAttributes.add(type.getProperty(str).getName());
        }
        for (GenericName extraAttribute : extraAttributes) {
            if (!allAttributes.contains(extraAttribute)) {
                allAttributes.add(extraAttribute);
            }
        }
        // ensure we have the primarykeys
        pkLoop: for (ColumnMetaModel pkc : pkey.getColumns()) {
            final String pkcName = pkc.getName();
            for (GenericName n : allAttributes) {
                if (n.tip().toString().equals(pkcName)) {
                    continue pkLoop;
                }
            }
            // add the pk attribut
            allAttributes.add(baseType.getProperty(pkcName).getName());
        }
        final GenericName[] allAttributeArray = allAttributes.toArray(new GenericName[allAttributes.size()]);
        queryFeatureType = (FeatureType) FeatureTypeExt.createSubType(tableType, allAttributeArray);
    }
    final String sql;
    // we gave him the connection, he must not release it
    final boolean release = (cnx == null);
    if (cnx == null) {
        try {
            cnx = getDataSource().getConnection();
        } catch (SQLException ex) {
            throw new DataStoreException(ex.getMessage(), ex);
        }
    }
    FeatureReader reader;
    // so for now if we got one, we let the filter be evaluated in java and not with a sql query
    if (containsOperation(preQuery, baseType)) {
        try {
            sql = getQueryBuilder().selectSQL(baseType, new org.geotoolkit.storage.feature.query.Query(baseType.getName()));
            reader = new JDBCFeatureReader(this, sql, baseType, cnx, release, null);
            FeatureStreams.subset(reader, query);
        } catch (SQLException ex) {
            throw new DataStoreException(ex.getMessage(), ex);
        }
    } else {
        try {
            sql = getQueryBuilder().selectSQL(queryFeatureType, preQuery);
            reader = new JDBCFeatureReader(this, sql, queryFeatureType, cnx, release, null);
        } catch (SQLException ex) {
            throw new DataStoreException(ex.getMessage(), ex);
        }
    }
    // if post filter, wrap it
    if (postFilter != null && postFilter != Filter.include()) {
        reader = FeatureStreams.filter(reader, postFilter);
    }
    // if we need to constraint type
    if (!returnedFeatureType.equals(queryFeatureType)) {
        reader = FeatureStreams.decorate(reader, new ViewMapper(type, query.getPropertyNames()), query.getHints());
    }
    return reader;
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) SQLQuery(org.geotoolkit.storage.feature.query.SQLQuery) Hints(org.geotoolkit.factory.Hints) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) GenericName(org.opengis.util.GenericName) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) DataStoreException(org.apache.sis.storage.DataStoreException) ViewMapper(org.geotoolkit.feature.ViewMapper) FilterAttributeExtractor(org.geotoolkit.filter.visitor.FilterAttributeExtractor) Filter(org.opengis.filter.Filter) CRSAdaptorVisitor(org.geotoolkit.filter.visitor.CRSAdaptorVisitor)

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