Search in sources :

Example 1 with ValueReference

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

the class CategoryStyleBuilder method analyze.

public void analyze(final MapLayer layer) {
    Resource resource = layer.getData();
    if (!(resource instanceof FeatureSet)) {
        throw new IllegalArgumentException("Layer resource must be a FeatureSet");
    }
    this.layer = layer;
    fts.rules().clear();
    properties.clear();
    if (layer != null) {
        FeatureType schema;
        try {
            schema = ((FeatureSet) resource).getType();
        } catch (DataStoreException ex) {
            throw new FeatureStoreRuntimeException(ex.getMessage(), ex);
        }
        for (PropertyType desc : schema.getProperties(true)) {
            if (desc instanceof AttributeType) {
                Class<?> type = ((AttributeType) desc).getValueClass();
                if (!Geometry.class.isAssignableFrom(type)) {
                    properties.add(ff.property(desc.getName().tip().toString()));
                }
            }
        }
        // find the geometry class for template
        Class<?> geoClass = null;
        try {
            PropertyType geo = FeatureExt.getDefaultGeometry(schema);
            geoClass = Features.toAttribute(geo).map(AttributeType::getValueClass).orElse(null);
        } catch (PropertyNotFoundException ex) {
            LOGGER.log(Level.FINE, "No sis:geometry property found", ex);
        }
        if (geoClass == null) {
            return;
        }
        if (Polygon.class.isAssignableFrom(geoClass) || MultiPolygon.class.isAssignableFrom(geoClass)) {
            Stroke stroke = sf.stroke(Color.BLACK, 1);
            Fill fill = sf.fill(Color.BLUE);
            template = sf.polygonSymbolizer(stroke, fill, null);
            expectedType = PolygonSymbolizer.class;
        } else if (LineString.class.isAssignableFrom(geoClass) || MultiLineString.class.isAssignableFrom(geoClass)) {
            Stroke stroke = sf.stroke(Color.BLUE, 2);
            template = sf.lineSymbolizer(stroke, null);
            expectedType = LineSymbolizer.class;
        } else {
            Stroke stroke = sf.stroke(Color.BLACK, 1);
            Fill fill = sf.fill(Color.BLUE);
            List<GraphicalSymbol> symbols = new ArrayList<>();
            symbols.add(sf.mark(StyleConstants.MARK_CIRCLE, fill, stroke));
            Graphic gra = sf.graphic(symbols, ff.literal(1), ff.literal(12), ff.literal(0), sf.anchorPoint(), sf.displacement());
            template = sf.pointSymbolizer(gra, null);
            expectedType = PointSymbolizer.class;
        }
        // try to rebuild the previous analyze if it was one
        List<? extends FeatureTypeStyle> ftss = layer.getStyle().featureTypeStyles();
        if (ftss.size() == 1) {
            FeatureTypeStyle fts = ftss.get(0);
            // defensive copy avoid synchronization
            List<? extends Rule> candidateRules = new ArrayList<>(fts.rules());
            for (Rule r : candidateRules) {
                // defensive copy avoid synchronization
                List<? extends Symbolizer> candidateSymbols = new ArrayList<>(r.symbolizers());
                if (candidateSymbols.size() != 1)
                    break;
                Symbolizer symbol = candidateSymbols.get(0);
                if (expectedType.isInstance(symbol)) {
                    if (r.isElseFilter()) {
                        // it looks like it's a valid classification "other" rule
                        this.fts.rules().add((MutableRule) r);
                        template = symbol;
                        other = true;
                    } else {
                        Filter f = r.getFilter();
                        if (f != null && f.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
                            BinaryComparisonOperator equal = (BinaryComparisonOperator) f;
                            Expression exp1 = equal.getOperand1();
                            Expression exp2 = equal.getOperand2();
                            if (exp1 instanceof ValueReference && exp2 instanceof Literal) {
                                if (properties.contains(exp1)) {
                                    // it looks like it's a valid classification property rule
                                    this.fts.rules().add((MutableRule) r);
                                    template = symbol;
                                    currentProperty = (ValueReference) exp1;
                                } else {
                                    // property is not in the schema
                                    break;
                                }
                            } else if (exp2 instanceof ValueReference && exp1 instanceof Literal) {
                                if (properties.contains(exp2)) {
                                    // it looks like it's a valid classification property rule
                                    this.fts.rules().add((MutableRule) r);
                                    template = symbol;
                                    currentProperty = (ValueReference) exp2;
                                } else {
                                    // property is not in the schema
                                    break;
                                }
                            } else {
                                // mismatch analyze structure
                                break;
                            }
                        }
                    }
                } else {
                    break;
                }
            }
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Fill(org.opengis.style.Fill) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) AttributeType(org.opengis.feature.AttributeType) Literal(org.opengis.filter.Literal) List(java.util.List) ArrayList(java.util.ArrayList) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) ValueReference(org.opengis.filter.ValueReference) PointSymbolizer(org.opengis.style.PointSymbolizer) DataStoreException(org.apache.sis.storage.DataStoreException) Stroke(org.opengis.style.Stroke) Graphic(org.opengis.style.Graphic) Resource(org.apache.sis.storage.Resource) PointSymbolizer(org.opengis.style.PointSymbolizer) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) LineSymbolizer(org.opengis.style.LineSymbolizer) Symbolizer(org.opengis.style.Symbolizer) Geometry(org.locationtech.jts.geom.Geometry) MutableRule(org.geotoolkit.style.MutableRule) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Filter(org.opengis.filter.Filter) Expression(org.opengis.filter.Expression) LineSymbolizer(org.opengis.style.LineSymbolizer) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) FeatureSet(org.apache.sis.storage.FeatureSet) MutableFeatureTypeStyle(org.geotoolkit.style.MutableFeatureTypeStyle) FeatureTypeStyle(org.opengis.style.FeatureTypeStyle) Rule(org.opengis.style.Rule) MutableRule(org.geotoolkit.style.MutableRule) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator)

Example 2 with ValueReference

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

the class CategoryStyleBuilder method create.

public List<MutableRule> create() {
    // search the different values
    final Set<Object> differentValues = new HashSet<>();
    final ValueReference property = currentProperty;
    final FeatureSet resource = (FeatureSet) layer.getData();
    final Query query = new Query();
    try {
        query.setTypeName(resource.getType().getName());
    } catch (DataStoreException ex) {
        LOGGER.log(Level.FINE, "Error while accessing data", ex);
    }
    query.setProperties(new String[] { property.getXPath() });
    try (Stream<Feature> stream = resource.subset(query).features(false)) {
        final Iterator<Feature> features = stream.iterator();
        while (features.hasNext()) {
            final Feature feature = features.next();
            differentValues.add(property.apply(feature));
        }
    } catch (DataStoreException | FeatureStoreRuntimeException ex) {
        LOGGER.log(Level.FINE, "Error while accessing data", ex);
    }
    // generate the different rules
    fts.rules().clear();
    for (Object obj : differentValues) {
        fts.rules().add(createRule(property, obj));
    }
    // generate the other rule if asked
    if (other) {
        MutableRule r = sf.rule(createSymbolizer());
        r.setElseFilter(true);
        r.setDescription(sf.description("other", "other"));
        fts.rules().add(r);
    }
    return fts.rules();
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.geotoolkit.storage.feature.query.Query) Feature(org.opengis.feature.Feature) MutableRule(org.geotoolkit.style.MutableRule) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) FeatureSet(org.apache.sis.storage.FeatureSet) HashSet(java.util.HashSet) ValueReference(org.opengis.filter.ValueReference)

Example 3 with ValueReference

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

the class IntervalStyleBuilder method isIntervalStyle.

public boolean isIntervalStyle(final FeatureTypeStyle fts) {
    if (fts.rules().isEmpty())
        return false;
    for (Rule r : fts.rules()) {
        Filter f = r.getFilter();
        if (f == null || r.isElseFilter())
            return false;
        if (r.symbolizers().size() != 1)
            return false;
        if (f.getOperatorType() == LogicalOperatorName.AND) {
            LogicalOperator<Object> and = (LogicalOperator) f;
            if (and.getOperands().size() != 2)
                return false;
            Filter<Object> op1 = and.getOperands().get(0);
            Filter<Object> op2 = and.getOperands().get(1);
            if (op2.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_GREATER_THAN_OR_EQUAL_TO) {
                // flip order
                op1 = op2;
                op2 = and.getOperands().get(0);
            }
            if (op1.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_GREATER_THAN_OR_EQUAL_TO) {
                BinaryComparisonOperator under = (BinaryComparisonOperator) op1;
                Expression exp1 = under.getOperand1();
                Expression exp2 = under.getOperand2();
                if (exp1.getFunctionName().tip().toString().equals("Divide")) {
                    List parameters = exp1.getParameters();
                    if (!properties.contains(parameters.get(0)))
                        return false;
                    if (!properties.contains(parameters.get(1)))
                        return false;
                } else if (exp1 instanceof ValueReference) {
                    ValueReference name = (ValueReference) exp1;
                    if (!properties.contains(name))
                        return false;
                } else {
                    return false;
                }
                if (!(exp2 instanceof Literal)) {
                    return false;
                }
                if (op2.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_LESS_THAN || op2.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_LESS_THAN_OR_EQUAL_TO) {
                    BinaryComparisonOperator bc = (BinaryComparisonOperator) op2;
                    Expression ex1 = under.getOperand1();
                    Expression ex2 = under.getOperand2();
                    if (exp1.getFunctionName().tip().toString().equals("Divide")) {
                        List parameters = exp1.getParameters();
                        if (!properties.contains(parameters.get(0)))
                            return false;
                        if (!properties.contains(parameters.get(1)))
                            return false;
                    } else if (exp1 instanceof ValueReference) {
                        ValueReference name = (ValueReference) ex1;
                        if (!properties.contains(name))
                            return false;
                    } else {
                        return false;
                    }
                    if (!(ex2 instanceof Literal)) {
                        return false;
                    }
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        template = r.symbolizers().get(0);
    }
    method = METHOD.MANUAL;
    nbClasses = fts.rules().size() + 1;
    return true;
}
Also used : Filter(org.opengis.filter.Filter) Expression(org.opengis.filter.Expression) LogicalOperator(org.opengis.filter.LogicalOperator) Literal(org.opengis.filter.Literal) List(java.util.List) ArrayList(java.util.ArrayList) Rule(org.opengis.style.Rule) MutableRule(org.geotoolkit.style.MutableRule) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator) ValueReference(org.opengis.filter.ValueReference)

Example 4 with ValueReference

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

the class FilterFactory2 method intersects.

public BinarySpatialOperator<Object> intersects(final String propertyName, final Geometry geometry) {
    final ValueReference name = property(propertyName);
    final Literal geom = super.literal(geometry);
    return intersects(name, geom);
}
Also used : Literal(org.opengis.filter.Literal) ValueReference(org.opengis.filter.ValueReference)

Example 5 with ValueReference

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

the class FilterFactory2 method within.

public BinarySpatialOperator<Object> within(final String propertyName, final Geometry geometry) {
    final ValueReference name = property(propertyName);
    final Literal geom = super.literal(geometry);
    return within(name, geom);
}
Also used : Literal(org.opengis.filter.Literal) ValueReference(org.opengis.filter.ValueReference)

Aggregations

ValueReference (org.opengis.filter.ValueReference)82 Literal (org.opengis.filter.Literal)53 Test (org.junit.Test)52 JAXBElement (javax.xml.bind.JAXBElement)39 Filter (org.opengis.filter.Filter)39 Marshaller (javax.xml.bind.Marshaller)36 Unmarshaller (javax.xml.bind.Unmarshaller)36 Expression (org.opengis.filter.Expression)27 BinaryComparisonOperator (org.opengis.filter.BinaryComparisonOperator)25 PropertyNameType (org.geotoolkit.ogc.xml.v100.PropertyNameType)18 PropertyNameType (org.geotoolkit.ogc.xml.v110.PropertyNameType)18 LiteralType (org.geotoolkit.ogc.xml.v100.LiteralType)16 LiteralType (org.geotoolkit.ogc.xml.v110.LiteralType)16 FilterType (org.geotoolkit.ogc.xml.v110.FilterType)14 FilterType (org.geotoolkit.ogc.xml.v100.FilterType)12 LogicalOperator (org.opengis.filter.LogicalOperator)12 ComparisonOpsType (org.geotoolkit.ogc.xml.v100.ComparisonOpsType)10 BinaryComparisonOpType (org.geotoolkit.ogc.xml.v100.BinaryComparisonOpType)9 BinaryComparisonOpType (org.geotoolkit.ogc.xml.v110.BinaryComparisonOpType)9 ComparisonOpsType (org.geotoolkit.ogc.xml.v110.ComparisonOpsType)9