Search in sources :

Example 6 with LogicalOperator

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

the class FilterReadingTest method testIn.

@Test
public void testIn() throws CQLException {
    final String cql = "att IN ( 15, 30, 'hello')";
    final Filter obj = CQL.parseFilter(cql);
    final LogicalOperator filter = (LogicalOperator) obj;
    assertEquals(FF.equal(FF.property("att"), FF.literal(15)), filter.getOperands().get(0));
    assertEquals(FF.equal(FF.property("att"), FF.literal(30)), filter.getOperands().get(1));
    assertEquals(FF.equal(FF.property("att"), FF.literal("hello")), filter.getOperands().get(2));
}
Also used : Filter(org.opengis.filter.Filter) LogicalOperator(org.opengis.filter.LogicalOperator) Test(org.junit.Test)

Example 7 with LogicalOperator

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

the class FilterToOGC100Converter method visit.

@Override
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 PropertyIsBetweenType bot = ogc_factory.createPropertyIsBetweenType();
        final LowerBoundaryType lbt = ogc_factory.createLowerBoundaryType();
        lbt.setExpression(extract(pib.getLowerBoundary()));
        final UpperBoundaryType ubt = ogc_factory.createUpperBoundaryType();
        ubt.setExpression(extract(pib.getUpperBoundary()));
        bot.setExpression(extract(pib.getExpression()));
        bot.setLowerBoundary(lbt);
        bot.setUpperBoundary(ubt);
        return ogc_factory.createPropertyIsBetween(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsEqualToType bot = new PropertyIsEqualToType();
        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 = new PropertyIsGreaterThanType();
        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 = new PropertyIsGreaterThanOrEqualToType();
        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 = new PropertyIsLessThanType();
        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 = new PropertyIsLessThanOrEqualToType();
        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.setEscape(String.valueOf(pis.getEscapeChar()));
        final LiteralType lt = ogc_factory.createLiteralType();
        lt.getContent().add(((Literal) expressions.get(1)).getValue());
        bot.setLiteral(lt);
        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 = new PropertyIsNotEqualToType();
        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();
        if (obj instanceof LiteralType) {
            bot.setLiteral((LiteralType) obj);
        } else if (obj instanceof PropertyNameType) {
            bot.setPropertyName((PropertyNameType) obj);
        } else {
            // should not be possible
            throw new IllegalArgumentException("Invalid expression element : " + obj);
        }
        return ogc_factory.createPropertyIsNull(bot);
    } else if (type == LogicalOperatorName.AND) {
        final LogicalOperator and = (LogicalOperator) filter;
        final AndType lot = new AndType();
        for (final Filter f : (List<Filter>) and.getOperands()) {
            lot.getComparisonOpsOrSpatialOpsOrLogicOps().add(visit(f));
        }
        return ogc_factory.createAnd(lot);
    } else if (type == LogicalOperatorName.OR) {
        final LogicalOperator or = (LogicalOperator) filter;
        final OrType lot = new OrType();
        for (final Filter f : (List<Filter>) or.getOperands()) {
            lot.getComparisonOpsOrSpatialOpsOrLogicOps().add(visit(f));
        }
        return ogc_factory.createOr(lot);
    } else if (type == LogicalOperatorName.NOT) {
        final LogicalOperator not = (LogicalOperator) filter;
        final NotType lot = new NotType();
        JAXBElement<?> sf = visit((Filter) not.getOperands().get(0));
        if (sf.getValue() instanceof ComparisonOpsType) {
            lot.setComparisonOps((JAXBElement<? extends ComparisonOpsType>) sf);
        } else if (sf.getValue() instanceof LogicOpsType) {
            lot.setLogicOps((JAXBElement<? extends LogicOpsType>) sf);
        } else if (sf.getValue() instanceof SpatialOpsType) {
            lot.setSpatialOps((JAXBElement<? extends SpatialOpsType>) sf);
        } else {
            // should not happen
            throw new IllegalArgumentException("invalid filter element : " + sf);
        }
        return ogc_factory.createNot(lot);
    } else if (type == SpatialOperatorName.BBOX) {
        final BBOX bbox = BBOX.wrap((BinarySpatialOperator) filter);
        final BBOXType bboxType = ogc_factory.createBBOXType();
        final Expression sourceExp1 = bbox.getOperand1();
        final JAXBElement<?> exp1 = extract(sourceExp1);
        final Expression sourceExp2 = bbox.getOperand2();
        JAXBElement<?> exp2 = extract(sourceExp2);
        final PropertyNameType pName;
        final BoxType boxType;
        if (exp1 != null && exp1.getValue() instanceof PropertyNameType) {
            pName = (PropertyNameType) exp1.getValue();
        } else if (exp2 != null && exp2.getValue() instanceof PropertyNameType) {
            pName = (PropertyNameType) exp2.getValue();
        } else
            throw new IllegalArgumentException("No property name found in given bbox filter");
        if (sourceExp1 instanceof Literal) {
            boxType = toBox((Literal) sourceExp1);
        } else if (sourceExp2 instanceof Literal) {
            boxType = toBox((Literal) sourceExp2);
        } else
            throw new IllegalArgumentException("No bbox found in given bbox filter");
        bboxType.setPropertyName(pName);
        bboxType.setBox(boxType);
        return ogc_factory.createBBOX(bboxType);
    }
    throw new IllegalArgumentException("Unknowed filter element : " + filter + " class :" + filter.getClass());
}
Also used : PropertyIsLessThanType(org.geotoolkit.ogc.xml.v100.PropertyIsLessThanType) AndType(org.geotoolkit.ogc.xml.v100.AndType) PropertyIsLessThanOrEqualToType(org.geotoolkit.ogc.xml.v100.PropertyIsLessThanOrEqualToType) BoxType(org.geotoolkit.gml.xml.v212.BoxType) PropertyIsNullType(org.geotoolkit.ogc.xml.v100.PropertyIsNullType) NotType(org.geotoolkit.ogc.xml.v100.NotType) SpatialOpsType(org.geotoolkit.ogc.xml.v100.SpatialOpsType) Literal(org.opengis.filter.Literal) CodeList(org.opengis.util.CodeList) List(java.util.List) PropertyIsBetweenType(org.geotoolkit.ogc.xml.v100.PropertyIsBetweenType) BetweenComparisonOperator(org.opengis.filter.BetweenComparisonOperator) ValueReference(org.opengis.filter.ValueReference) PropertyNameType(org.geotoolkit.ogc.xml.v100.PropertyNameType) OrType(org.geotoolkit.ogc.xml.v100.OrType) PropertyIsGreaterThanType(org.geotoolkit.ogc.xml.v100.PropertyIsGreaterThanType) LogicalOperator(org.opengis.filter.LogicalOperator) ComparisonOpsType(org.geotoolkit.ogc.xml.v100.ComparisonOpsType) LiteralType(org.geotoolkit.ogc.xml.v100.LiteralType) JAXBElement(javax.xml.bind.JAXBElement) PropertyIsGreaterThanOrEqualToType(org.geotoolkit.ogc.xml.v100.PropertyIsGreaterThanOrEqualToType) NullOperator(org.opengis.filter.NullOperator) LogicOpsType(org.geotoolkit.ogc.xml.v100.LogicOpsType) UpperBoundaryType(org.geotoolkit.ogc.xml.v100.UpperBoundaryType) PropertyIsNotEqualToType(org.geotoolkit.ogc.xml.v100.PropertyIsNotEqualToType) PropertyIsEqualToType(org.geotoolkit.ogc.xml.v100.PropertyIsEqualToType) BBOXType(org.geotoolkit.ogc.xml.v100.BBOXType) Expression(org.opengis.filter.Expression) Filter(org.opengis.filter.Filter) LowerBoundaryType(org.geotoolkit.ogc.xml.v100.LowerBoundaryType) LikeOperator(org.opengis.filter.LikeOperator) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator) PropertyIsLikeType(org.geotoolkit.ogc.xml.v100.PropertyIsLikeType)

Example 8 with LogicalOperator

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

the class FilterToOGC200Converter method visit.

public JAXBElement visit(Filter filter) {
    if (filter.equals(Filter.include()) || 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();
        bot.setExpression(extract(pib.getExpression()));
        bot.setLowerBoundary(lbt);
        bot.setUpperBoundary(ubt);
        return ogc_factory.createPropertyIsBetween(bot);
    } else if (type == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
        final BinaryComparisonOperator pit = (BinaryComparisonOperator) filter;
        final PropertyIsEqualToType bot = new PropertyIsEqualToType();
        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 = new PropertyIsGreaterThanType();
        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 = new PropertyIsGreaterThanOrEqualToType();
        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 = new PropertyIsLessThanType();
        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 = new PropertyIsLessThanOrEqualToType();
        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.setEscape(String.valueOf(pis.getEscapeChar()));
        final LiteralType lt = ogc_factory.createLiteralType();
        lt.setContent(((Literal) expressions.get(1)).getValue());
        bot.getElements().add(ogc_factory.createLiteral(lt));
        final Expression expression = expressions.get(0);
        if (!(expression instanceof ValueReference)) {
            throw new IllegalArgumentException("LikeOperator can support ValueReference only, but was a " + expression);
        }
        bot.getElements().add(0, extract(expression));
        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 = new PropertyIsNotEqualToType();
        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();
        bot.setExpression(extract((Expression) pis.getExpressions().get(0)));
        return ogc_factory.createPropertyIsNull(bot);
    } else if (type == LogicalOperatorName.AND) {
        final LogicalOperator and = (LogicalOperator) filter;
        final List<JAXBElement> lot = new ArrayList<>();
        for (final Filter f : (List<Filter>) and.getOperands()) {
            final JAXBElement<?> ele = visit(f);
            if (ele != null && ele.getValue() instanceof LogicOpsType) {
                lot.add(ele);
            }
        }
        return ogc_factory.createAnd(new AndType(lot.toArray()));
    } else if (type == LogicalOperatorName.OR) {
        final LogicalOperator or = (LogicalOperator) filter;
        final List<JAXBElement> lot = new ArrayList<>();
        for (final Filter f : (List<Filter>) or.getOperands()) {
            final JAXBElement subFilter = visit(f);
            if (subFilter != null) {
                lot.add(subFilter);
            }
        }
        return ogc_factory.createOr(new OrType(lot.toArray()));
    } else if (type == LogicalOperatorName.NOT) {
        final LogicalOperator not = (LogicalOperator) filter;
        final JAXBElement<?> sf = visit((Filter) not.getOperands().get(0));
        // should not happen
        return ogc_factory.createNot(new NotType(sf));
    } 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("invalid 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);
                    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("invalid bbox element : " + filter);
            }
        } else {
            throw new IllegalArgumentException("invalid bbox element : " + filter);
        }
        final BBOXType bbtype = new BBOXType(property, minx, miny, maxx, maxy, srs);
        return ogc_factory.createBBOX(bbtype);
    } else if (filter instanceof ResourceId) {
        final ValueReference n = FF.property(AttributeConvention.IDENTIFIER);
        ResourceId idFilter = (ResourceId) filter;
        final String id = idFilter.getIdentifier();
        final ResourceIdType rId = ogc_factory.createResourceIdType();
        rId.setRid(id);
        return ogc_factory.createResourceId(rId);
    } else if (filter instanceof BinarySpatialOperator) {
        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 xml filter, " + "expression are not of the required type ");
        } else if (!(exp2 instanceof Literal)) {
            throw new IllegalArgumentException("Spatial operator should use a literal object containing the filtering geometry.");
        }
        final JAXBElement pnt = extract(exp1);
        final String pName;
        if (pnt.getValue() instanceof String) {
            pName = (String) pnt.getValue();
        } else {
            throw new IllegalArgumentException("Property name cannot be cast to string.");
        }
        final JAXBElement<?> geometryExpression;
        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 AbstractGeometry gmlGeom;
            try {
                gmlGeom = JTStoGeometry.toGML("3.2.1", jts);
            } catch (FactoryException ex) {
                throw new IllegalArgumentException(ex);
            }
            // TODO use gml method to return any JAXBElement
            if (gmlGeom instanceof PointType) {
                geometryExpression = gml_factory.createPoint((PointType) gmlGeom);
            } else if (gmlGeom instanceof CurveType) {
                geometryExpression = gml_factory.createCurve((CurveType) gmlGeom);
            } else if (gmlGeom instanceof LineStringType) {
                geometryExpression = gml_factory.createLineString((LineStringType) gmlGeom);
            } else if (gmlGeom instanceof PolygonType) {
                geometryExpression = gml_factory.createPolygon((PolygonType) gmlGeom);
            } else if (gmlGeom instanceof MultiSurfaceType) {
                geometryExpression = gml_factory.createMultiSurface((MultiSurfaceType) gmlGeom);
            } else if (gmlGeom instanceof MultiCurveType) {
                geometryExpression = gml_factory.createMultiCurve((MultiCurveType) gmlGeom);
            } else if (gmlGeom instanceof MultiPointType) {
                geometryExpression = gml_factory.createMultiPoint((MultiPointType) gmlGeom);
            } else if (gmlGeom instanceof MultiGeometryType) {
                geometryExpression = gml_factory.createMultiGeometry((MultiGeometryType) gmlGeom);
            } else if (gmlGeom instanceof SurfaceType) {
                geometryExpression = gml_factory.createPolyhedralSurface((SurfaceType) gmlGeom);
            } else {
                throw new IllegalArgumentException("Unexpected Geometry type:" + gmlGeom.getClass().getName());
            }
        } else if (geom instanceof org.opengis.geometry.Geometry) {
            throw new UnsupportedOperationException("No valid ISO implementation avaiable for now.");
        } else if (geom instanceof org.opengis.geometry.Envelope) {
            final org.opengis.geometry.Envelope genv = (org.opengis.geometry.Envelope) geom;
            EnvelopeType ee = gml_factory.createEnvelopeType();
            ee.setSrsDimension(genv.getDimension());
            if (genv.getCoordinateReferenceSystem() != null) {
                String urn;
                try {
                    urn = IdentifiedObjects.lookupURN(genv.getCoordinateReferenceSystem(), null);
                } catch (FactoryException ex) {
                    Logger.getLogger("org.geotoolkit.sld.xml").log(Level.WARNING, null, ex);
                    urn = null;
                }
                if (urn == null) {
                    urn = IdentifiedObjects.getIdentifierOrName(genv.getCoordinateReferenceSystem());
                }
                if (urn != null) {
                    ee.setSrsName(urn);
                }
            }
            ee.setLowerCorner(new DirectPositionType(genv.getLowerCorner(), false));
            ee.setUpperCorner(new DirectPositionType(genv.getUpperCorner(), false));
            geometryExpression = gml_factory.createEnvelope(ee);
        } else {
            throw new IllegalArgumentException("Type is neither geometric nor envelope.");
        }
        if (type == DistanceOperatorName.BEYOND) {
            throw new UnsupportedOperationException();
        } else if (type == SpatialOperatorName.CONTAINS) {
            return ogc_factory.createContains(new ContainsType(pName, geometryExpression));
        } else if (type == SpatialOperatorName.CROSSES) {
            ogc_factory.createCrosses(new CrossesType(pName, geometryExpression));
        } else if (type == DistanceOperatorName.WITHIN) {
            Quantity q = ((DistanceOperator) filter).getDistance();
            return ogc_factory.createDWithin(new DWithinType(pName, geometryExpression, q.getValue().doubleValue(), q.getUnit().toString()));
        } else if (type == SpatialOperatorName.DISJOINT) {
            return ogc_factory.createDisjoint(new DisjointType(pName, geometryExpression));
        } else if (type == SpatialOperatorName.EQUALS) {
            return ogc_factory.createEquals(new EqualsType(pName, geometryExpression));
        } else if (type == SpatialOperatorName.INTERSECTS) {
            return ogc_factory.createIntersects(new IntersectsType(pName, geometryExpression));
        } else if (type == SpatialOperatorName.OVERLAPS) {
            return ogc_factory.createOverlaps(new OverlapsType(pName, geometryExpression));
        } else if (type == SpatialOperatorName.TOUCHES) {
            return ogc_factory.createTouches(new TouchesType(pName, geometryExpression));
        } else if (type == SpatialOperatorName.WITHIN) {
            return ogc_factory.createWithin(new WithinType(pName, geometryExpression));
        }
        throw new IllegalArgumentException("Unknown filter element : " + filter + " class :" + filter.getClass());
    }
    throw new IllegalArgumentException("Unknown filter element : " + filter + " class :" + filter.getClass());
}
Also used : OverlapsType(org.geotoolkit.ogc.xml.v200.OverlapsType) FactoryException(org.opengis.util.FactoryException) MultiCurveType(org.geotoolkit.gml.xml.v321.MultiCurveType) ArrayList(java.util.ArrayList) MultiGeometryType(org.geotoolkit.gml.xml.v321.MultiGeometryType) CrossesType(org.geotoolkit.ogc.xml.v200.CrossesType) DWithinType(org.geotoolkit.ogc.xml.v200.DWithinType) MultiSurfaceType(org.geotoolkit.gml.xml.v321.MultiSurfaceType) NotType(org.geotoolkit.ogc.xml.v200.NotType) Literal(org.opengis.filter.Literal) DisjointType(org.geotoolkit.ogc.xml.v200.DisjointType) CodeList(org.opengis.util.CodeList) List(java.util.List) ArrayList(java.util.ArrayList) PropertyIsBetweenType(org.geotoolkit.ogc.xml.v200.PropertyIsBetweenType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) BetweenComparisonOperator(org.opengis.filter.BetweenComparisonOperator) ValueReference(org.opengis.filter.ValueReference) DWithinType(org.geotoolkit.ogc.xml.v200.DWithinType) WithinType(org.geotoolkit.ogc.xml.v200.WithinType) ResourceIdType(org.geotoolkit.ogc.xml.v200.ResourceIdType) PropertyIsGreaterThanType(org.geotoolkit.ogc.xml.v200.PropertyIsGreaterThanType) LogicalOperator(org.opengis.filter.LogicalOperator) PolygonType(org.geotoolkit.gml.xml.v321.PolygonType) JAXBElement(javax.xml.bind.JAXBElement) IntersectsType(org.geotoolkit.ogc.xml.v200.IntersectsType) LineStringType(org.geotoolkit.gml.xml.v321.LineStringType) MultiSurfaceType(org.geotoolkit.gml.xml.v321.MultiSurfaceType) SurfaceType(org.geotoolkit.gml.xml.v321.SurfaceType) PropertyIsGreaterThanOrEqualToType(org.geotoolkit.ogc.xml.v200.PropertyIsGreaterThanOrEqualToType) MultiPointType(org.geotoolkit.gml.xml.v321.MultiPointType) LogicOpsType(org.geotoolkit.ogc.xml.v200.LogicOpsType) UpperBoundaryType(org.geotoolkit.ogc.xml.v200.UpperBoundaryType) PropertyIsNotEqualToType(org.geotoolkit.ogc.xml.v200.PropertyIsNotEqualToType) ContainsType(org.geotoolkit.ogc.xml.v200.ContainsType) BBOXType(org.geotoolkit.ogc.xml.v200.BBOXType) LikeOperator(org.opengis.filter.LikeOperator) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator) PropertyIsLikeType(org.geotoolkit.ogc.xml.v200.PropertyIsLikeType) PropertyIsLessThanType(org.geotoolkit.ogc.xml.v200.PropertyIsLessThanType) AndType(org.geotoolkit.ogc.xml.v200.AndType) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) DirectPositionType(org.geotoolkit.gml.xml.v321.DirectPositionType) CurveType(org.geotoolkit.gml.xml.v321.CurveType) MultiCurveType(org.geotoolkit.gml.xml.v321.MultiCurveType) PropertyIsLessThanOrEqualToType(org.geotoolkit.ogc.xml.v200.PropertyIsLessThanOrEqualToType) Envelope(org.locationtech.jts.geom.Envelope) PropertyIsNullType(org.geotoolkit.ogc.xml.v200.PropertyIsNullType) EqualsType(org.geotoolkit.ogc.xml.v200.EqualsType) BinarySpatialOperator(org.opengis.filter.BinarySpatialOperator) OrType(org.geotoolkit.ogc.xml.v200.OrType) EnvelopeType(org.geotoolkit.gml.xml.v321.EnvelopeType) DistanceOperator(org.opengis.filter.DistanceOperator) Quantity(javax.measure.Quantity) LiteralType(org.geotoolkit.ogc.xml.v200.LiteralType) NullOperator(org.opengis.filter.NullOperator) FactoryException(org.opengis.util.FactoryException) JTStoGeometry(org.geotoolkit.gml.JTStoGeometry) Geometry(org.locationtech.jts.geom.Geometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) PropertyIsEqualToType(org.geotoolkit.ogc.xml.v200.PropertyIsEqualToType) Expression(org.opengis.filter.Expression) Filter(org.opengis.filter.Filter) ResourceId(org.opengis.filter.ResourceId) PointType(org.geotoolkit.gml.xml.v321.PointType) MultiPointType(org.geotoolkit.gml.xml.v321.MultiPointType) LowerBoundaryType(org.geotoolkit.ogc.xml.v200.LowerBoundaryType) TouchesType(org.geotoolkit.ogc.xml.v200.TouchesType)

Example 9 with LogicalOperator

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

the class OGC100Test method testFilterLogicalOr.

@Test
public void testFilterLogicalOr() throws JAXBException {
    final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
    final Marshaller MARSHALLER = POOL.acquireMarshaller();
    // Read test
    Object obj = UNMARSHALLER.unmarshal(FILE_FIL_LOG_OR);
    assertNotNull(obj);
    JAXBElement<? extends FilterType> jaxfilter = (JAXBElement<? extends FilterType>) obj;
    assertNotNull(jaxfilter);
    Filter filter = TRANSFORMER_GT.visitFilter(jaxfilter.getValue());
    assertNotNull(filter);
    LogicalOperator prop = (LogicalOperator) filter;
    BinaryComparisonOperator leftop = (BinaryComparisonOperator) prop.getOperands().get(0);
    BinaryComparisonOperator rightop = (BinaryComparisonOperator) prop.getOperands().get(1);
    ValueReference left = (ValueReference) leftop.getOperand1();
    Literal right = (Literal) leftop.getOperand2();
    assertEquals(left.getXPath(), valueStr);
    assertEquals(((Number) right.apply(null)).floatValue(), 455f, DELTA);
    left = (ValueReference) rightop.getOperand1();
    right = (Literal) rightop.getOperand2();
    assertEquals(left.getXPath(), valueStr);
    assertEquals(((Number) right.apply(null)).floatValue(), 457f, DELTA);
    // write test
    FilterType ft = TRANSFORMER_OGC.apply(filter);
    assertNotNull(ft.getLogicOps());
    LogicOpsType cot = ft.getLogicOps().getValue();
    assertEquals(ft.getLogicOps().getName().getLocalPart(), OGCJAXBStatics.FILTER_LOGIC_OR);
    BinaryLogicOpType pibt = (BinaryLogicOpType) cot;
    BinaryComparisonOpType leftoptype = (BinaryComparisonOpType) pibt.getComparisonOpsOrSpatialOpsOrLogicOps().get(0).getValue();
    BinaryComparisonOpType rightoptype = (BinaryComparisonOpType) pibt.getComparisonOpsOrSpatialOpsOrLogicOps().get(1).getValue();
    PropertyNameType lf = (PropertyNameType) leftoptype.getExpression().get(0).getValue();
    LiteralType rg = (LiteralType) leftoptype.getExpression().get(1).getValue();
    assertEquals(valueStr, lf.getContent());
    numberEquals(455, rg.getContent().get(0));
    lf = (PropertyNameType) rightoptype.getExpression().get(0).getValue();
    rg = (LiteralType) rightoptype.getExpression().get(1).getValue();
    assertEquals(valueStr, lf.getContent());
    numberEquals(457, rg.getContent().get(0));
    MARSHALLER.marshal(ft.getLogicOps(), TEST_FILE_FIL_LOG_OR);
    POOL.recycle(MARSHALLER);
    POOL.recycle(UNMARSHALLER);
}
Also used : Marshaller(javax.xml.bind.Marshaller) LogicalOperator(org.opengis.filter.LogicalOperator) LiteralType(org.geotoolkit.ogc.xml.v100.LiteralType) JAXBElement(javax.xml.bind.JAXBElement) LogicOpsType(org.geotoolkit.ogc.xml.v100.LogicOpsType) FilterType(org.geotoolkit.ogc.xml.v100.FilterType) BinaryLogicOpType(org.geotoolkit.ogc.xml.v100.BinaryLogicOpType) Filter(org.opengis.filter.Filter) Literal(org.opengis.filter.Literal) Unmarshaller(javax.xml.bind.Unmarshaller) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator) BinaryComparisonOpType(org.geotoolkit.ogc.xml.v100.BinaryComparisonOpType) ValueReference(org.opengis.filter.ValueReference) PropertyNameType(org.geotoolkit.ogc.xml.v100.PropertyNameType) Test(org.junit.Test)

Example 10 with LogicalOperator

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

the class OGC100Test method testFilterLogicalAnd.

// //////////////////////////////////////////////////////////////////////////
// JAXB TEST MARSHELLING AND UNMARSHELLING FOR LOGIC FILTERS ///////////////
// //////////////////////////////////////////////////////////////////////////
@Test
public void testFilterLogicalAnd() throws JAXBException {
    final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
    final Marshaller MARSHALLER = POOL.acquireMarshaller();
    // Read test
    Object obj = UNMARSHALLER.unmarshal(FILE_FIL_LOG_AND);
    assertNotNull(obj);
    JAXBElement<? extends FilterType> jaxfilter = (JAXBElement<? extends FilterType>) obj;
    assertNotNull(jaxfilter);
    Filter filter = TRANSFORMER_GT.visitFilter(jaxfilter.getValue());
    assertNotNull(filter);
    LogicalOperator prop = (LogicalOperator) filter;
    BinaryComparisonOperator leftop = (BinaryComparisonOperator) prop.getOperands().get(0);
    BinaryComparisonOperator rightop = (BinaryComparisonOperator) prop.getOperands().get(1);
    ValueReference left = (ValueReference) leftop.getOperand1();
    Literal right = (Literal) leftop.getOperand2();
    assertEquals(left.getXPath(), valueStr);
    assertEquals(((Number) right.apply(null)).floatValue(), 455f, DELTA);
    left = (ValueReference) rightop.getOperand1();
    right = (Literal) rightop.getOperand2();
    assertEquals(left.getXPath(), valueStr);
    assertEquals(((Number) right.apply(null)).floatValue(), 457f, DELTA);
    // write test
    FilterType ft = TRANSFORMER_OGC.apply(filter);
    assertNotNull(ft.getLogicOps());
    LogicOpsType cot = ft.getLogicOps().getValue();
    assertEquals(ft.getLogicOps().getName().getLocalPart(), OGCJAXBStatics.FILTER_LOGIC_AND);
    BinaryLogicOpType pibt = (BinaryLogicOpType) cot;
    BinaryComparisonOpType leftoptype = (BinaryComparisonOpType) pibt.getComparisonOpsOrSpatialOpsOrLogicOps().get(0).getValue();
    BinaryComparisonOpType rightoptype = (BinaryComparisonOpType) pibt.getComparisonOpsOrSpatialOpsOrLogicOps().get(1).getValue();
    PropertyNameType lf = (PropertyNameType) leftoptype.getExpression().get(0).getValue();
    LiteralType rg = (LiteralType) leftoptype.getExpression().get(1).getValue();
    assertEquals(valueStr, lf.getContent());
    numberEquals(455, rg.getContent().get(0));
    lf = (PropertyNameType) rightoptype.getExpression().get(0).getValue();
    rg = (LiteralType) rightoptype.getExpression().get(1).getValue();
    assertEquals(valueStr, lf.getContent());
    numberEquals(457, rg.getContent().get(0));
    MARSHALLER.marshal(ft.getLogicOps(), TEST_FILE_FIL_LOG_AND);
    POOL.recycle(MARSHALLER);
    POOL.recycle(UNMARSHALLER);
}
Also used : Marshaller(javax.xml.bind.Marshaller) LogicalOperator(org.opengis.filter.LogicalOperator) LiteralType(org.geotoolkit.ogc.xml.v100.LiteralType) JAXBElement(javax.xml.bind.JAXBElement) LogicOpsType(org.geotoolkit.ogc.xml.v100.LogicOpsType) FilterType(org.geotoolkit.ogc.xml.v100.FilterType) BinaryLogicOpType(org.geotoolkit.ogc.xml.v100.BinaryLogicOpType) Filter(org.opengis.filter.Filter) Literal(org.opengis.filter.Literal) Unmarshaller(javax.xml.bind.Unmarshaller) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator) BinaryComparisonOpType(org.geotoolkit.ogc.xml.v100.BinaryComparisonOpType) ValueReference(org.opengis.filter.ValueReference) PropertyNameType(org.geotoolkit.ogc.xml.v100.PropertyNameType) Test(org.junit.Test)

Aggregations

Filter (org.opengis.filter.Filter)16 LogicalOperator (org.opengis.filter.LogicalOperator)16 ValueReference (org.opengis.filter.ValueReference)12 Test (org.junit.Test)11 BinaryComparisonOperator (org.opengis.filter.BinaryComparisonOperator)11 Literal (org.opengis.filter.Literal)11 JAXBElement (javax.xml.bind.JAXBElement)9 List (java.util.List)6 Marshaller (javax.xml.bind.Marshaller)6 Unmarshaller (javax.xml.bind.Unmarshaller)6 Expression (org.opengis.filter.Expression)5 ArrayList (java.util.ArrayList)4 LiteralType (org.geotoolkit.ogc.xml.v100.LiteralType)4 LogicOpsType (org.geotoolkit.ogc.xml.v100.LogicOpsType)4 PropertyNameType (org.geotoolkit.ogc.xml.v100.PropertyNameType)4 BinaryComparisonOpType (org.geotoolkit.ogc.xml.v100.BinaryComparisonOpType)3 FilterType (org.geotoolkit.ogc.xml.v100.FilterType)3 LiteralType (org.geotoolkit.ogc.xml.v110.LiteralType)3 LogicOpsType (org.geotoolkit.ogc.xml.v110.LogicOpsType)3 PropertyNameType (org.geotoolkit.ogc.xml.v110.PropertyNameType)3