Search in sources :

Example 41 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class PostgresVersioningTest method testDistinctSchema.

@Test
public void testDistinctSchema() throws DataStoreException, VersioningException, FileNotFoundException, IOException {
    reload(true);
    List<Version> versions;
    Feature feature;
    ResourceId fid;
    Version v0;
    Version v1;
    Version v2;
    FeatureIterator ite;
    Query qb = new Query();
    final FeatureType refType = FTYPE_SIMPLE;
    // ------------------- initialize public2 schema --------------------
    // / creation 2eme table
    PostgresStore store2;
    final ParameterValueGroup params2 = params.clone();
    params2.parameter("schema").setValue("public2");
    store2 = (PostgresStore) DataStores.open(params2);
    // -------------- create schema in public2 schema --------------------
    try {
        ((DataStoreFactory) store2.getProvider()).create(params2);
    } catch (Exception ex) {
    // schema public2 already exist
    }
    for (GenericName n : store2.getNames()) {
        VersionControl vc = store2.getVersioning(n.toString());
        vc.dropVersioning();
        store2.deleteFeatureType(n.toString());
    }
    assertTrue(store2.getNames().isEmpty());
    // delete historisation functions, he must create them himself
    store2.dropHSFunctions();
    // -------------- create table in public schema --------------------
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    assertTrue(store2.getNames().isEmpty());
    // get version control
    final VersionControl vcP1 = store.getVersioning(refType.getName().toString());
    assertNotNull(vcP1);
    assertTrue(vcP1.isEditable());
    assertFalse(vcP1.isVersioned());
    // -------------------- start versioning in public schema ---------------
    vcP1.startVersioning();
    assertTrue(vcP1.isVersioned());
    versions = vcP1.list();
    assertTrue(versions.isEmpty());
    // --------------------- table creation in public2 schema ---------------
    store2.createFeatureType(refType);
    assertEquals(1, store2.getNames().size());
    // get version control
    final VersionControl vcP2 = store2.getVersioning(refType.getName().toString());
    assertNotNull(vcP2);
    assertTrue(vcP2.isEditable());
    assertFalse(vcP2.isVersioned());
    // -------------------- start versioning in public schema ---------------
    vcP2.startVersioning();
    assertTrue(vcP2.isVersioned());
    versions = vcP2.list();
    assertTrue(versions.isEmpty());
    /* insert, update and delete some elements in public schema and verify
         * public2 schema stay empty, to verify the 2th schema are actions distincts.*/
    // make an insert ///////////////////////////////////////////////////////
    final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
    feature = refType.newInstance();
    feature.setPropertyValue("id", "0");
    feature.setPropertyValue("boolean", Boolean.TRUE);
    feature.setPropertyValue("integer", 14);
    feature.setPropertyValue("point", firstPoint);
    feature.setPropertyValue("string", "someteststring");
    store.addFeatures(refType.getName().toString(), Collections.singleton(feature));
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // make an update ///////////////////////////////////////////////////////
    // get feature to update
    // get identifier
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
    final Map<String, Object> updates = new HashMap<>();
    updates.put("boolean", Boolean.FALSE);
    updates.put("integer", -3);
    updates.put("point", secondPoint);
    updates.put("string", "anothertextupdated");
    store.updateFeatures(refType.getName().toString(), fid, updates);
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // make a remove ///////////////////////////////////////////////////////
    store.removeFeatures(refType.getName().toString(), fid);
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    // get all versions organized in increase dates order.
    versions = vcP1.list();
    assertEquals(3, versions.size());
    v0 = versions.get(0);
    v1 = versions.get(1);
    v2 = versions.get(2);
    vcP1.revert(v1.getDate());
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
    vcP1.trim(v1.getDate());
    // ensure test table in public2 schema is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store2.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure history test table in public2 schema is empty
    assertTrue(vcP2.list().isEmpty());
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) VersioningException(org.geotoolkit.version.VersioningException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) GenericName(org.opengis.util.GenericName) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) DataStoreFactory(org.geotoolkit.storage.DataStoreFactory) Test(org.junit.Test)

Example 42 with ResourceId

use of org.opengis.filter.ResourceId 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));
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) Query(org.geotoolkit.storage.feature.query.Query) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) ResourceId(org.opengis.filter.ResourceId) Envelope(org.locationtech.jts.geom.Envelope) File(java.io.File) FilterFactory2(org.geotoolkit.filter.FilterFactory2) ShapefileProvider(org.geotoolkit.data.shapefile.ShapefileProvider) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 43 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class FidQueryTest method testModifyFeature.

@Test
public void testModifyFeature() throws Exception {
    final Feature feature = this.fids.values().iterator().next();
    final long newId = 237594123;
    final ResourceId createFidFilter = FeatureExt.getId(feature);
    final FeatureType schema = feature.getType();
    session.updateFeatures(schema.getName().toString(), createFidFilter, Collections.singletonMap("ID", newId));
    session.commit();
    final FeatureIterator features = ds.getFeatureReader(Query.filtered(name.toString(), createFidFilter));
    try {
        assertFalse(feature.equals(features.next()));
    } finally {
        if (features != null) {
            features.close();
        }
    }
    feature.setPropertyValue("ID", newId);
    this.assertFidsMatch();
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureType(org.opengis.feature.FeatureType) ResourceId(org.opengis.filter.ResourceId) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 44 with ResourceId

use of org.opengis.filter.ResourceId in project geotoolkit by Geomatys.

the class IndexedShapefileFeatureStore method getAttributesReader.

private IndexedShapefileAttributeReader getAttributesReader(final List<? extends AttributeType> properties, final Filter filter, final boolean read3D, final double[] resample) throws DataStoreException {
    final AccessManager locker = shpFiles.createLocker();
    CloseableCollection<ShpData> goodRecs = null;
    if (filter instanceof ResourceId && shpFiles.isWritable() && shpFiles.exists(FIX)) {
        final ResourceId fidFilter = (ResourceId) filter;
        final TreeSet<String> idsSet = new TreeSet<>();
        idsSet.add(fidFilter.getIdentifier());
        try {
            goodRecs = queryFidIndex(idsSet);
        } catch (IOException ex) {
            throw new DataStoreException(ex);
        }
    } else {
        // will be bbox.isNull() to start
        Envelope bbox = new JTSEnvelope2D();
        if (filter != null) {
            // Add additional bounds from the filter
            // will be null for Filter.EXCLUDES
            bbox = ExtractBoundsFilterVisitor.bbox(filter, null);
            if (bbox == null) {
                bbox = new JTSEnvelope2D();
            // we hit Filter.EXCLUDES consider returning an empty
            // reader?
            // (however should simplify the filter to detect ff.not(
            // fitler.EXCLUDE )
            }
        }
        if (!bbox.isNull() && this.useIndex) {
            try {
                goodRecs = this.queryQuadTree(locker, bbox);
            } catch (TreeException e) {
                throw new DataStoreException("Error querying index: " + e.getMessage());
            } catch (IOException e) {
                throw new DataStoreException("Error querying index: " + e.getMessage());
            }
        }
    }
    final boolean readDBF = !(properties.size() == 1 && Geometry.class.isAssignableFrom(properties.get(0).getValueClass()));
    final AttributeType[] atts = properties.toArray(new AttributeType[properties.size()]);
    try {
        return new IndexedShapefileAttributeReader(locker, atts, read3D, useMemoryMappedBuffer, resample, readDBF, dbfCharset, resample, goodRecs, ((goodRecs != null) ? goodRecs.iterator() : null));
    } catch (IOException ex) {
        throw new DataStoreException(ex);
    }
}
Also used : AccessManager(org.geotoolkit.data.shapefile.lock.AccessManager) ShpData(org.geotoolkit.data.shapefile.indexed.IndexDataReader.ShpData) DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) Envelope(org.locationtech.jts.geom.Envelope) Geometry(org.locationtech.jts.geom.Geometry) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) ResourceId(org.opengis.filter.ResourceId) AttributeType(org.opengis.feature.AttributeType) TreeException(org.geotoolkit.index.TreeException)

Example 45 with ResourceId

use of org.opengis.filter.ResourceId 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

ResourceId (org.opengis.filter.ResourceId)45 Feature (org.opengis.feature.Feature)26 Test (org.junit.Test)23 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)20 FeatureType (org.opengis.feature.FeatureType)20 Query (org.geotoolkit.storage.feature.query.Query)18 HashMap (java.util.HashMap)11 Coordinate (org.locationtech.jts.geom.Coordinate)11 Point (org.locationtech.jts.geom.Point)10 Filter (org.opengis.filter.Filter)10 DataStoreException (org.apache.sis.storage.DataStoreException)9 Session (org.geotoolkit.storage.feature.session.Session)9 Geometry (org.locationtech.jts.geom.Geometry)9 Date (java.util.Date)8 PropertyType (org.opengis.feature.PropertyType)8 ArrayList (java.util.ArrayList)7 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)7 Version (org.geotoolkit.version.Version)6 VersionControl (org.geotoolkit.version.VersionControl)6 IOException (java.io.IOException)5