Search in sources :

Example 11 with ResourceId

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

the class PostgresVersioningTest method testRevertVersioning.

@Test
public void testRevertVersioning() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Feature feature;
    ResourceId fid;
    Version v0;
    Version v1;
    Version v2;
    FeatureIterator ite;
    Query qb = new Query();
    // create table
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    // get version control
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    assertNotNull(vc);
    assertTrue(vc.isEditable());
    assertFalse(vc.isVersioned());
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    assertTrue(vc.isVersioned());
    versions = vc.list();
    assertTrue(versions.isEmpty());
    // 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));
    // we should have one version
    versions = vc.list();
    assertEquals(1, versions.size());
    // get identifier
    // ensure normal reading is correct without version----------------------
    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());
    }
    // make an update ///////////////////////////////////////////////////////
    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);
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // make a remove ///////////////////////////////////////////////////////
    store.removeFeatures(refType.getName().toString(), fid);
    // ensure test table is empty
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store.createSession(true).getFeatureCollection(qb).isEmpty());
    // get all versions organized in increase dates order.
    versions = vc.list();
    assertEquals(3, versions.size());
    v0 = versions.get(0);
    v1 = versions.get(1);
    v2 = versions.get(2);
    /* first revert between v1 date and v2 date (named middle date) to verify
         * re - insertion of feature in the original base and update v2 ending date become null.*/
    // =/2
    final Date middle = new Date((v1.getDate().getTime() + v2.getDate().getTime()) >> 1);
    vc.revert(middle);
    versions = vc.list();
    assertEquals(2, versions.size());
    // ensure version v2 does not exist
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v2.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure test table contain feature from version 1
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertFalse(store.createSession(true).getFeatureCollection(qb).isEmpty());
    Feature featV1;
    Feature feat;
    // feature from test base result.
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feat = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    // feature from version v1.
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        featV1 = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    assertTrue(feat.getProperty("boolean").equals(featV1.getProperty("boolean")));
    assertTrue(feat.getProperty("integer").equals(featV1.getProperty("integer")));
    assertTrue(feat.getProperty("point").equals(featV1.getProperty("point")));
    assertTrue(feat.getProperty("string").equals(featV1.getProperty("string")));
    /* second revert at v0 begin date to verify update roll back, and verify
         * feature update from history table into original base.*/
    vc.revert(v0.getDate());
    versions = vc.list();
    assertEquals(1, versions.size());
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    try {
        store.createSession(true).getFeatureCollection(qb).isEmpty();
        fail("should not find version");
    } catch (FeatureStoreRuntimeException ex) {
    // ok
    }
    // ensure test table contain feature from version 1
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertFalse(store.createSession(true).getFeatureCollection(qb).isEmpty());
    // feature from test base result.
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feat = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    // feature from version v1.
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v0.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        featV1 = ite.next();
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    assertTrue(feat.getProperty("boolean").equals(featV1.getProperty("boolean")));
    assertTrue(feat.getProperty("integer").equals(featV1.getProperty("integer")));
    assertTrue(feat.getProperty("point").equals(featV1.getProperty("point")));
    assertTrue(feat.getProperty("string").equals(featV1.getProperty("string")));
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) Date(java.util.Date) FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) Version(org.geotoolkit.version.Version) ResourceId(org.opengis.filter.ResourceId) Coordinate(org.locationtech.jts.geom.Coordinate) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) Test(org.junit.Test)

Example 12 with ResourceId

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

the class PostgresVersioningTest method testSimpleTypeVersioning.

@Test
public void testSimpleTypeVersioning() throws DataStoreException, VersioningException {
    reload(true);
    List<Version> versions;
    Version version;
    Feature feature;
    ResourceId fid;
    Version v1;
    Version v2;
    Version v3;
    FeatureIterator ite;
    Query qb = new Query();
    // create table
    final FeatureType refType = FTYPE_SIMPLE;
    store.createFeatureType(refType);
    assertEquals(1, store.getNames().size());
    assertNotNull(store.getQueryCapabilities());
    assertTrue(store.getQueryCapabilities().handleVersioning());
    // get version control
    final VersionControl vc = store.getVersioning(refType.getName().toString());
    assertNotNull(vc);
    assertTrue(vc.isEditable());
    assertFalse(vc.isVersioned());
    // //////////////////////////////////////////////////////////////////////
    // start versioning /////////////////////////////////////////////////////
    vc.startVersioning();
    assertTrue(vc.isVersioned());
    versions = vc.list();
    assertTrue(versions.isEmpty());
    // check the version table is not visible in the feature types
    store.refreshMetaModel();
    final Set<GenericName> names = store.getNames();
    assertEquals(1, names.size());
    // //////////////////////////////////////////////////////////////////////
    // 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));
    // we should have one version
    versions = vc.list();
    assertEquals(1, versions.size());
    version = versions.get(0);
    Date date = version.getDate();
    // ensure normal reading is correct without version----------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.TRUE, feature.getPropertyValue("boolean"));
        assertEquals(14, feature.getPropertyValue("integer"));
        assertEquals(firstPoint, feature.getPropertyValue("point"));
        assertEquals("someteststring", feature.getPropertyValue("string"));
        fid = FeatureExt.getId(feature);
    } finally {
        ite.close();
    }
    // ensure normal reading is correct with version-------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(version.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.TRUE, feature.getPropertyValue("boolean"));
        assertEquals(14, feature.getPropertyValue("integer"));
        assertEquals(firstPoint, feature.getPropertyValue("point"));
        assertEquals("someteststring", feature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
    try {
        // wait a bit just to have some space between version dates
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
        fail(ex.getMessage());
    }
    // //////////////////////////////////////////////////////////////////////
    // make an update ///////////////////////////////////////////////////////
    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);
    // we should have two versions
    versions = vc.list();
    assertEquals(2, versions.size());
    v1 = versions.get(0);
    v2 = versions.get(1);
    // should be ordered starting from the oldest
    assertTrue(v1.getDate().compareTo(v2.getDate()) < 0);
    // ensure normal reading is correct without version----------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.FALSE, feature.getPropertyValue("boolean"));
        assertEquals(-3, feature.getPropertyValue("integer"));
        assertEquals(secondPoint, feature.getPropertyValue("point"));
        assertEquals("anothertextupdated", feature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
    // ensure normal reading is correct with version-------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v2.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.FALSE, feature.getPropertyValue("boolean"));
        assertEquals(-3, feature.getPropertyValue("integer"));
        assertEquals(secondPoint, feature.getPropertyValue("point"));
        assertEquals("anothertextupdated", feature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
    // ensure reading a previous version works ------------------------------
    qb = new Query();
    ;
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.TRUE, feature.getPropertyValue("boolean"));
        assertEquals(14, feature.getPropertyValue("integer"));
        assertEquals(firstPoint, feature.getPropertyValue("point"));
        assertEquals("someteststring", feature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
    // ensure reading a previous version using not exact date----------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionDate(new Date(v1.getDate().getTime() + 400));
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.TRUE, feature.getPropertyValue("boolean"));
        assertEquals(14, feature.getPropertyValue("integer"));
        assertEquals(firstPoint, feature.getPropertyValue("point"));
        assertEquals("someteststring", feature.getPropertyValue("string"));
    } finally {
        ite.close();
    }
    // //////////////////////////////////////////////////////////////////////
    // delete record ////////////////////////////////////////////////////////
    store.removeFeatures(refType.getName().toString(), fid);
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertEquals(0, store.getCount(qb));
    // we should have three versions
    versions = vc.list();
    assertEquals(3, versions.size());
    v1 = versions.get(0);
    v2 = versions.get(1);
    v3 = versions.get(2);
    // should be ordered starting from the oldest
    assertTrue(v2.getDate().compareTo(v1.getDate()) > 0);
    assertTrue(v3.getDate().compareTo(v2.getDate()) > 0);
    // ensure we have nothing if no version set -----------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure we have nothing if latest version set -------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v3.getLabel());
    assertTrue(store.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure we have nothing with date after deletion ----------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionDate(new Date(v3.getDate().getTime() + 400));
    assertTrue(store.createSession(true).getFeatureCollection(qb).isEmpty());
    // ensure reading version 1 works ---------------------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v1.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.TRUE, feature.getProperty("boolean").getValue());
        assertEquals(14, feature.getProperty("integer").getValue());
        assertEquals(firstPoint, feature.getProperty("point").getValue());
        assertEquals("someteststring", feature.getProperty("string").getValue());
    } finally {
        ite.close();
    }
    // ensure reading version 2 works ---------------------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    qb.setVersionLabel(v2.getLabel());
    ite = store.createSession(true).getFeatureCollection(qb).iterator();
    try {
        feature = ite.next();
        assertEquals(Boolean.FALSE, feature.getProperty("boolean").getValue());
        assertEquals(-3, feature.getProperty("integer").getValue());
        assertEquals(secondPoint, feature.getProperty("point").getValue());
        assertEquals("anothertextupdated", feature.getProperty("string").getValue());
    } finally {
        ite.close();
    }
    // //////////////////////////////////////////////////////////////////////
    // drop versioning //////////////////////////////////////////////////////
    vc.dropVersioning();
    assertTrue(vc.isEditable());
    assertFalse(vc.isVersioned());
    versions = vc.list();
    assertTrue(versions.isEmpty());
    // ensure we have no record----------------------------------------------
    qb = new Query();
    qb.setTypeName(refType.getName());
    assertTrue(store.createSession(true).getFeatureCollection(qb).isEmpty());
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) Point(org.locationtech.jts.geom.Point) Feature(org.opengis.feature.Feature) VersionControl(org.geotoolkit.version.VersionControl) Date(java.util.Date) 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) Test(org.junit.Test)

Example 13 with ResourceId

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

the class DefaultStyleVisitor method visit.

@Override
public Object visit(final FeatureTypeStyle featureTypeStyle, Object data) {
    final ResourceId ids = featureTypeStyle.getFeatureInstanceIDs();
    if (ids != null) {
        visit(ids, data);
    }
    final List<? extends Rule> rules = featureTypeStyle.rules();
    if (rules != null) {
        for (Rule r : rules) {
            data = r.accept(this, data);
        }
    }
    return data;
}
Also used : ResourceId(org.opengis.filter.ResourceId) Rule(org.opengis.style.Rule)

Example 14 with ResourceId

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

the class FilterToOGC110Converter method visit.

public JAXBElement<?> visit(final Filter filter) {
    if (filter.equals(Filter.include())) {
        return null;
    }
    if (filter.equals(Filter.exclude())) {
        return null;
    }
    final CodeList<?> type = filter.getOperatorType();
    if (filter instanceof BetweenComparisonOperator) {
        final BetweenComparisonOperator pib = (BetweenComparisonOperator) filter;
        final LowerBoundaryType lbt = ogc_factory.createLowerBoundaryType();
        lbt.setExpression(extract(pib.getLowerBoundary()));
        final UpperBoundaryType ubt = ogc_factory.createUpperBoundaryType();
        ubt.setExpression(extract(pib.getUpperBoundary()));
        final PropertyIsBetweenType bot = new PropertyIsBetweenType(extract(pib.getExpression()), lbt, ubt);
        return ogc_factory.createPropertyIsBetween(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsEqualToType bot = ogc_factory.createPropertyIsEqualToType();
        bot.getExpression().add(extract(pit.getOperand1()));
        bot.getExpression().add(extract(pit.getOperand2()));
        return ogc_factory.createPropertyIsEqualTo(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_GREATER_THAN) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsGreaterThanType bot = ogc_factory.createPropertyIsGreaterThanType();
        bot.getExpression().add(extract(pit.getOperand1()));
        bot.getExpression().add(extract(pit.getOperand2()));
        return ogc_factory.createPropertyIsGreaterThan(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_GREATER_THAN_OR_EQUAL_TO) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsGreaterThanOrEqualToType bot = ogc_factory.createPropertyIsGreaterThanOrEqualToType();
        bot.getExpression().add(extract(pit.getOperand1()));
        bot.getExpression().add(extract(pit.getOperand2()));
        return ogc_factory.createPropertyIsGreaterThanOrEqualTo(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_LESS_THAN) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsLessThanType bot = ogc_factory.createPropertyIsLessThanType();
        bot.getExpression().add(extract(pit.getOperand1()));
        bot.getExpression().add(extract(pit.getOperand2()));
        return ogc_factory.createPropertyIsLessThan(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_LESS_THAN_OR_EQUAL_TO) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsLessThanOrEqualToType bot = ogc_factory.createPropertyIsLessThanOrEqualToType();
        bot.getExpression().add(extract(pit.getOperand1()));
        bot.getExpression().add(extract(pit.getOperand2()));
        return ogc_factory.createPropertyIsLessThanOrEqualTo(bot);
    } else if (filter instanceof LikeOperator) {
        final LikeOperator pis = (LikeOperator) filter;
        final List<Expression> expressions = filter.getExpressions();
        final PropertyIsLikeType bot = ogc_factory.createPropertyIsLikeType();
        bot.setEscapeChar(String.valueOf(pis.getEscapeChar()));
        final LiteralType lt = ogc_factory.createLiteralType();
        lt.setContent(((Literal) expressions.get(1)).getValue());
        bot.setLiteral(lt.getStringValue());
        final Expression expression = expressions.get(0);
        if (!(expression instanceof ValueReference)) {
            throw new IllegalArgumentException("LikeOperator can support ValueReference only, but was a " + expression);
        }
        final PropertyNameType pnt = (PropertyNameType) extract(expression).getValue();
        bot.setPropertyName(pnt);
        bot.setSingleChar(String.valueOf(pis.getSingleChar()));
        bot.setWildCard(String.valueOf(pis.getWildCard()));
        return ogc_factory.createPropertyIsLike(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_NOT_EQUAL_TO) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsNotEqualToType bot = ogc_factory.createPropertyIsNotEqualToType();
        bot.getExpression().add(extract(pit.getOperand1()));
        bot.getExpression().add(extract(pit.getOperand2()));
        return ogc_factory.createPropertyIsNotEqualTo(bot);
    } else if (filter instanceof NullOperator) {
        final NullOperator pis = (NullOperator) filter;
        final PropertyIsNullType bot = ogc_factory.createPropertyIsNullType();
        final Object obj = extract((Expression) pis.getExpressions().get(0)).getValue();
        bot.setPropertyName((PropertyNameType) obj);
        return ogc_factory.createPropertyIsNull(bot);
    } else if (type == LogicalOperatorName.AND) {
        final LogicalOperator and = (LogicalOperator) filter;
        final List<JAXBElement> children = new ArrayList<>();
        for (final Filter f : (List<Filter>) and.getOperands()) {
            final JAXBElement<? extends LogicOpsType> ele = (JAXBElement<? extends LogicOpsType>) visit(f);
            if (ele != null) {
                children.add(ele);
            }
        }
        return ogc_factory.createAnd(new AndType(children.toArray()));
    } else if (type == LogicalOperatorName.OR) {
        final LogicalOperator or = (LogicalOperator) filter;
        final List<JAXBElement> children = new ArrayList<>();
        for (final Filter f : (List<Filter>) or.getOperands()) {
            final JAXBElement<? extends LogicOpsType> ele = (JAXBElement<? extends LogicOpsType>) visit(f);
            if (ele != null) {
                children.add(ele);
            }
        }
        return ogc_factory.createOr(new OrType(children.toArray()));
    } else if (type == LogicalOperatorName.NOT) {
        final LogicalOperator not = (LogicalOperator) filter;
        JAXBElement<?> sf = visit((Filter) not.getOperands().get(0));
        return ogc_factory.createNot(new NotType(sf.getValue()));
    } else if (filter instanceof ResourceId) {
        throw new IllegalArgumentException("Not parsed yet : " + filter);
    } else if (type == SpatialOperatorName.BBOX) {
        final BBOX bbox = BBOX.wrap((BinarySpatialOperator) filter);
        final Expression left = bbox.getOperand1();
        final Expression right = bbox.getOperand2();
        final String property;
        final double minx;
        final double maxx;
        final double miny;
        final double maxy;
        String srs;
        if (left instanceof ValueReference) {
            property = ((ValueReference) left).getXPath();
            final Object objGeom = ((Literal) right).getValue();
            if (objGeom instanceof org.opengis.geometry.Envelope) {
                final org.opengis.geometry.Envelope env = (org.opengis.geometry.Envelope) objGeom;
                minx = env.getMinimum(0);
                maxx = env.getMaximum(0);
                miny = env.getMinimum(1);
                maxy = env.getMaximum(1);
                try {
                    srs = IdentifiedObjects.lookupURN(env.getCoordinateReferenceSystem(), null);
                    if (srs == null) {
                        srs = ReferencingUtilities.lookupIdentifier(env.getCoordinateReferenceSystem(), true);
                    }
                } catch (FactoryException ex) {
                    throw new IllegalArgumentException("invalid bbox element : " + filter + " " + ex.getMessage(), ex);
                }
            } else if (objGeom instanceof Geometry) {
                final Geometry geom = (Geometry) objGeom;
                final Envelope env = geom.getEnvelopeInternal();
                minx = env.getMinX();
                maxx = env.getMaxX();
                miny = env.getMinY();
                maxy = env.getMaxY();
                srs = SRIDGenerator.toSRS(geom.getSRID(), SRIDGenerator.Version.V1);
            } else {
                throw new IllegalArgumentException("invalide bbox element : " + filter);
            }
        } else if (right instanceof ValueReference) {
            property = ((ValueReference) right).getXPath();
            final Object objGeom = ((Literal) left).getValue();
            if (objGeom instanceof org.opengis.geometry.Envelope) {
                final org.opengis.geometry.Envelope env = (org.opengis.geometry.Envelope) objGeom;
                minx = env.getMinimum(0);
                maxx = env.getMaximum(0);
                miny = env.getMinimum(1);
                maxy = env.getMaximum(1);
                try {
                    srs = IdentifiedObjects.lookupURN(env.getCoordinateReferenceSystem(), null);
                } catch (FactoryException ex) {
                    throw new IllegalArgumentException("invalide bbox element : " + filter + " " + ex.getMessage(), ex);
                }
            } else if (objGeom instanceof Geometry) {
                final Geometry geom = (Geometry) objGeom;
                final Envelope env = geom.getEnvelopeInternal();
                minx = env.getMinX();
                maxx = env.getMaxX();
                miny = env.getMinY();
                maxy = env.getMaxY();
                srs = SRIDGenerator.toSRS(geom.getSRID(), SRIDGenerator.Version.V1);
            } else {
                throw new IllegalArgumentException("invalide bbox element : " + filter);
            }
        } else {
            throw new IllegalArgumentException("invalide bbox element : " + filter);
        }
        final BBOXType bbtype = new BBOXType(property, minx, miny, maxx, maxy, srs);
        return ogc_factory.createBBOX(bbtype);
    } else if (filter instanceof ResourceId) {
        // todo OGC filter can not handle ID when we are inside another filter type
        // so here we make a small tric to change an id filter in a serie of propertyequal filter
        // this is not really legal but we dont have the choice here
        // we should propose an evolution of ogc filter do consider id filter as a comparison filter
        final ValueReference n = FF.property(AttributeConvention.IDENTIFIER);
        String ident = ((ResourceId) filter).getIdentifier();
        return visit(FF.equal(n, FF.literal(ident)));
    } else if (filter instanceof SpatialOperator) {
        final BinarySpatialOperator spatialOp = (BinarySpatialOperator) filter;
        Expression exp1 = spatialOp.getOperand1();
        Expression exp2 = spatialOp.getOperand2();
        if (!(exp1 instanceof ValueReference)) {
            // flip order
            final Expression ex = exp1;
            exp1 = exp2;
            exp2 = ex;
        }
        if (!(exp1 instanceof ValueReference)) {
            throw new IllegalArgumentException("Filter can not be transformed in wml filter, " + "expression are not of the requiered type ");
        }
        final JAXBElement<PropertyNameType> pnt = (JAXBElement<PropertyNameType>) extract(exp1);
        final JAXBElement<EnvelopeType> jaxEnv;
        final JAXBElement<? extends AbstractGeometryType> jaxGeom;
        final Object geom = ((Literal) exp2).getValue();
        if (geom instanceof Geometry) {
            final Geometry jts = (Geometry) geom;
            final String srid = SRIDGenerator.toSRS(jts.getSRID(), SRIDGenerator.Version.V1);
            CoordinateReferenceSystem crs;
            try {
                crs = CRS.forCode(srid);
            } catch (Exception ex) {
                Logger.getLogger("org.geotoolkit.sld.xml").log(Level.WARNING, null, ex);
                crs = null;
            }
            final AbstractGeometryType gt = GMLUtilities.getGMLFromISO(JTSUtils.toISO(jts, crs));
            // TODO use gml method to return any JAXBElement
            if (gt instanceof PointType) {
                jaxGeom = gml_factory.createPoint((PointType) gt);
            } else if (gt instanceof CurveType) {
                jaxGeom = gml_factory.createCurve((CurveType) gt);
            } else if (gt instanceof LineStringType) {
                jaxGeom = gml_factory.createLineString((LineStringType) gt);
            } else if (gt instanceof PolygonType) {
                jaxGeom = gml_factory.createPolygon((PolygonType) gt);
            } else if (gt instanceof MultiPolygonType) {
                jaxGeom = gml_factory.createMultiPolygon((MultiPolygonType) gt);
            } else if (gt instanceof MultiLineStringType) {
                jaxGeom = gml_factory.createMultiLineString((MultiLineStringType) gt);
            } else if (gt instanceof MultiCurveType) {
                jaxGeom = gml_factory.createMultiLineString((MultiLineStringType) gt);
            } else if (gt instanceof MultiPointType) {
                jaxGeom = gml_factory.createMultiLineString((MultiLineStringType) gt);
            } else if (gt instanceof MultiGeometryType) {
                jaxGeom = gml_factory.createMultiGeometry((MultiGeometryType) gt);
            } else if (gt instanceof PolyhedralSurfaceType) {
                jaxGeom = gml_factory.createPolyhedralSurface((PolyhedralSurfaceType) gt);
            } else if (gt != null) {
                throw new IllegalArgumentException("unexpected Geometry type:" + gt.getClass().getName());
            } else {
                jaxGeom = null;
            }
            jaxEnv = null;
        } else if (geom instanceof org.opengis.geometry.Geometry) {
            final AbstractGeometryType gt = GMLUtilities.getGMLFromISO((org.opengis.geometry.Geometry) geom);
            // TODO use gml method to return any JAXBElement
            if (gt instanceof PointType) {
                jaxGeom = gml_factory.createPoint((PointType) gt);
            } else if (gt instanceof CurveType) {
                jaxGeom = gml_factory.createCurve((CurveType) gt);
            } else if (gt instanceof LineStringType) {
                jaxGeom = gml_factory.createLineString((LineStringType) gt);
            } else if (gt instanceof PolygonType) {
                jaxGeom = gml_factory.createPolygon((PolygonType) gt);
            } else if (gt instanceof MultiPolygonType) {
                jaxGeom = gml_factory.createMultiPolygon((MultiPolygonType) gt);
            } else if (gt instanceof MultiLineStringType) {
                jaxGeom = gml_factory.createMultiLineString((MultiLineStringType) gt);
            } else if (gt instanceof MultiCurveType) {
                jaxGeom = gml_factory.createMultiLineString((MultiLineStringType) gt);
            } else if (gt instanceof MultiPointType) {
                jaxGeom = gml_factory.createMultiLineString((MultiLineStringType) gt);
            } else if (gt instanceof MultiGeometryType) {
                jaxGeom = gml_factory.createMultiGeometry((MultiGeometryType) gt);
            } else if (gt instanceof PolyhedralSurfaceType) {
                jaxGeom = gml_factory.createPolyhedralSurface((PolyhedralSurfaceType) gt);
            } else if (gt != null) {
                throw new IllegalArgumentException("unexpected Geometry type:" + gt.getClass().getName());
            } else {
                jaxGeom = null;
            }
            jaxEnv = null;
        } else if (geom instanceof org.opengis.geometry.Envelope) {
            final org.opengis.geometry.Envelope genv = (org.opengis.geometry.Envelope) geom;
            EnvelopeType ee = gml_factory.createEnvelopeType();
            try {
                ee.setSrsName(IdentifiedObjects.lookupURN(genv.getCoordinateReferenceSystem(), null));
            } catch (FactoryException ex) {
                Logger.getLogger("org.geotoolkit.sld.xml").log(Level.WARNING, null, ex);
            }
            ee.setLowerCorner(new DirectPositionType(genv.getLowerCorner()));
            ee.setUpperCorner(new DirectPositionType(genv.getUpperCorner()));
            jaxGeom = null;
            jaxEnv = gml_factory.createEnvelope(ee);
        } else {
            throw new IllegalArgumentException("Type is not geometric or envelope.");
        }
        if (type == DistanceOperatorName.BEYOND) {
            final BeyondType jaxelement = ogc_factory.createBeyondType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setPropertyName(pnt.getValue());
            return ogc_factory.createBeyond(jaxelement);
        } else if (type == SpatialOperatorName.CONTAINS) {
            final ContainsType jaxelement = ogc_factory.createContainsType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createContains(jaxelement);
        } else if (type == SpatialOperatorName.CROSSES) {
            final CrossesType jaxelement = ogc_factory.createCrossesType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createCrosses(jaxelement);
        } else if (type == DistanceOperatorName.WITHIN) {
            final DWithinType jaxelement = ogc_factory.createDWithinType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setPropertyName(pnt.getValue());
            return ogc_factory.createDWithin(jaxelement);
        } else if (type == SpatialOperatorName.DISJOINT) {
            final DisjointType jaxelement = ogc_factory.createDisjointType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createDisjoint(jaxelement);
        } else if (type == SpatialOperatorName.EQUALS) {
            final EqualsType jaxelement = ogc_factory.createEqualsType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createEquals(jaxelement);
        } else if (type == SpatialOperatorName.INTERSECTS) {
            final IntersectsType jaxelement = ogc_factory.createIntersectsType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createIntersects(jaxelement);
        } else if (type == SpatialOperatorName.OVERLAPS) {
            final OverlapsType jaxelement = new OverlapsType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createOverlaps(jaxelement);
        } else if (type == SpatialOperatorName.TOUCHES) {
            final TouchesType jaxelement = ogc_factory.createTouchesType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createTouches(jaxelement);
        } else if (type == SpatialOperatorName.WITHIN) {
            final WithinType jaxelement = ogc_factory.createWithinType();
            jaxelement.setAbstractGeometry(jaxGeom);
            jaxelement.setEnvelope(jaxEnv);
            jaxelement.setPropertyName(pnt);
            return ogc_factory.createWithin(jaxelement);
        } else {
            throw new IllegalArgumentException("Unknowed filter element : " + filter + " class :" + filter.getClass());
        }
    } else {
        throw new IllegalArgumentException("Unknowed filter element : " + filter + " class :" + filter.getClass());
    }
}
Also used : OverlapsType(org.geotoolkit.ogc.xml.v110.OverlapsType) FactoryException(org.opengis.util.FactoryException) MultiCurveType(org.geotoolkit.gml.xml.v311.MultiCurveType) BeyondType(org.geotoolkit.ogc.xml.v110.BeyondType) ArrayList(java.util.ArrayList) MultiGeometryType(org.geotoolkit.gml.xml.v311.MultiGeometryType) CrossesType(org.geotoolkit.ogc.xml.v110.CrossesType) DWithinType(org.geotoolkit.ogc.xml.v110.DWithinType) NotType(org.geotoolkit.ogc.xml.v110.NotType) Literal(org.opengis.filter.Literal) DisjointType(org.geotoolkit.ogc.xml.v110.DisjointType) MultiLineStringType(org.geotoolkit.gml.xml.v311.MultiLineStringType) CodeList(org.opengis.util.CodeList) List(java.util.List) ArrayList(java.util.ArrayList) PropertyIsBetweenType(org.geotoolkit.ogc.xml.v110.PropertyIsBetweenType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) BetweenComparisonOperator(org.opengis.filter.BetweenComparisonOperator) ValueReference(org.opengis.filter.ValueReference) PropertyNameType(org.geotoolkit.ogc.xml.v110.PropertyNameType) DWithinType(org.geotoolkit.ogc.xml.v110.DWithinType) WithinType(org.geotoolkit.ogc.xml.v110.WithinType) AbstractGeometryType(org.geotoolkit.gml.xml.v311.AbstractGeometryType) PropertyIsGreaterThanType(org.geotoolkit.ogc.xml.v110.PropertyIsGreaterThanType) LogicalOperator(org.opengis.filter.LogicalOperator) PolygonType(org.geotoolkit.gml.xml.v311.PolygonType) MultiPolygonType(org.geotoolkit.gml.xml.v311.MultiPolygonType) JAXBElement(javax.xml.bind.JAXBElement) IntersectsType(org.geotoolkit.ogc.xml.v110.IntersectsType) LineStringType(org.geotoolkit.gml.xml.v311.LineStringType) MultiLineStringType(org.geotoolkit.gml.xml.v311.MultiLineStringType) PropertyIsGreaterThanOrEqualToType(org.geotoolkit.ogc.xml.v110.PropertyIsGreaterThanOrEqualToType) MultiPointType(org.geotoolkit.gml.xml.v311.MultiPointType) LogicOpsType(org.geotoolkit.ogc.xml.v110.LogicOpsType) UpperBoundaryType(org.geotoolkit.ogc.xml.v110.UpperBoundaryType) PropertyIsNotEqualToType(org.geotoolkit.ogc.xml.v110.PropertyIsNotEqualToType) ContainsType(org.geotoolkit.ogc.xml.v110.ContainsType) BBOXType(org.geotoolkit.ogc.xml.v110.BBOXType) LikeOperator(org.opengis.filter.LikeOperator) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator) PolyhedralSurfaceType(org.geotoolkit.gml.xml.v311.PolyhedralSurfaceType) PropertyIsLikeType(org.geotoolkit.ogc.xml.v110.PropertyIsLikeType) PropertyIsLessThanType(org.geotoolkit.ogc.xml.v110.PropertyIsLessThanType) AndType(org.geotoolkit.ogc.xml.v110.AndType) DirectPositionType(org.geotoolkit.gml.xml.v311.DirectPositionType) CurveType(org.geotoolkit.gml.xml.v311.CurveType) MultiCurveType(org.geotoolkit.gml.xml.v311.MultiCurveType) PropertyIsLessThanOrEqualToType(org.geotoolkit.ogc.xml.v110.PropertyIsLessThanOrEqualToType) Envelope(org.locationtech.jts.geom.Envelope) PropertyIsNullType(org.geotoolkit.ogc.xml.v110.PropertyIsNullType) EqualsType(org.geotoolkit.ogc.xml.v110.EqualsType) BinarySpatialOperator(org.opengis.filter.BinarySpatialOperator) OrType(org.geotoolkit.ogc.xml.v110.OrType) EnvelopeType(org.geotoolkit.gml.xml.v311.EnvelopeType) MultiPolygonType(org.geotoolkit.gml.xml.v311.MultiPolygonType) LiteralType(org.geotoolkit.ogc.xml.v110.LiteralType) NullOperator(org.opengis.filter.NullOperator) FactoryException(org.opengis.util.FactoryException) Geometry(org.locationtech.jts.geom.Geometry) PropertyIsEqualToType(org.geotoolkit.ogc.xml.v110.PropertyIsEqualToType) Expression(org.opengis.filter.Expression) Filter(org.opengis.filter.Filter) ResourceId(org.opengis.filter.ResourceId) PointType(org.geotoolkit.gml.xml.v311.PointType) MultiPointType(org.geotoolkit.gml.xml.v311.MultiPointType) LowerBoundaryType(org.geotoolkit.ogc.xml.v110.LowerBoundaryType) BinarySpatialOperator(org.opengis.filter.BinarySpatialOperator) TouchesType(org.geotoolkit.ogc.xml.v110.TouchesType)

Example 15 with ResourceId

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

the class FeatureStoreUtilities method write.

/**
 * Write the features from the given collection and return the list of generated FeatureID
 * send by the writer.
 *
 * @param writer, writer will not be closed
 * @return List of generated FeatureId. Can be empty if output data type has
 * no identifier property.
 * @throws FeatureStoreRuntimeException
 */
public static List<ResourceId> write(final FeatureWriter writer, final Collection<? extends Feature> collection) throws FeatureStoreRuntimeException {
    final List<ResourceId> ids = new ArrayList<>();
    boolean withId = false;
    // Check if there's identifiers to report.
    try {
        writer.getFeatureType().getProperty(AttributeConvention.IDENTIFIER);
        withId = true;
    } catch (PropertyNotFoundException e) {
        LOGGER.log(Level.FINE, "No identifier available at copy", e);
    }
    RuntimeException error = null;
    final Iterator<? extends Feature> ite = collection.iterator();
    try {
        while (ite.hasNext()) {
            final Feature f = ite.next();
            final Feature candidate = writer.next();
            FeatureExt.copy(f, candidate, false);
            writer.write();
            if (withId) {
                ids.add(FeatureExt.getId(candidate));
            }
        }
    } catch (RuntimeException e) {
        error = e;
        throw error;
    } finally {
        if (ite instanceof AutoCloseable) {
            try {
                ((AutoCloseable) ite).close();
            } catch (Exception ex) {
                if (error == null)
                    throw new FeatureStoreRuntimeException(ex);
                else
                    error.addSuppressed(ex);
            }
        }
    }
    return ids;
}
Also used : PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) ResourceId(org.opengis.filter.ResourceId) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) DataStoreException(org.apache.sis.storage.DataStoreException) BackingStoreException(org.apache.sis.util.collection.BackingStoreException) IOException(java.io.IOException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException)

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