Search in sources :

Example 1 with MutableRule

use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.

the class CategoryStyleBuilder method createRule.

public MutableRule createRule(final ValueReference property, final Object obj) {
    MutableRule r = sf.rule(createSymbolizer());
    r.setFilter(ff.equal(property, ff.literal(obj)));
    r.setDescription(sf.description(obj.toString(), obj.toString()));
    return r;
}
Also used : MutableRule(org.geotoolkit.style.MutableRule)

Example 2 with MutableRule

use of org.geotoolkit.style.MutableRule 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 3 with MutableRule

use of org.geotoolkit.style.MutableRule 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 4 with MutableRule

use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.

the class IntervalStyleBuilder method generateRules.

public List<MutableRule> generateRules(final IntervalPalette palette, Filter combinedFilter) {
    analyze();
    List<MutableRule> rules = new ArrayList<MutableRule>();
    final Expression exp;
    if (normalize == null || normalize.equals(noValue)) {
        exp = classification;
    } else {
        exp = ff.divide(classification, normalize);
    }
    for (int i = 1; i < values.length; i++) {
        final MutableRule rule = sf.rule();
        double start = values[i - 1];
        double end = values[i];
        // create the filter and title
        final Filter interval;
        final String title;
        if (i == values.length - 1) {
            // last element
            Filter above = ff.greaterOrEqual(exp, ff.literal(start));
            Filter under = ff.lessOrEqual(exp, ff.literal(end));
            interval = ff.and(above, under);
            title = "[ " + FORMAT.format(start) + " -> " + FORMAT.format(end) + " ]";
        } else {
            Filter above = ff.greaterOrEqual(exp, ff.literal(start));
            Filter under = ff.less(exp, ff.literal(end));
            interval = ff.and(above, under);
            title = "[ " + FORMAT.format(start) + " -> " + FORMAT.format(end) + " [";
        }
        if (Filter.include().equals(combinedFilter) || combinedFilter == null) {
            rule.setFilter(interval);
        } else {
            rule.setFilter(ff.and(combinedFilter, interval));
        }
        rule.setName(title);
        rule.setDescription(sf.description(title, title));
        // create the style
        Symbolizer symbol = createSymbolizer(palette, (double) (i - 1) / (values.length - 2));
        rule.symbolizers().add(symbol);
        rules.add(rule);
    }
    return rules;
}
Also used : MutableRule(org.geotoolkit.style.MutableRule) Expression(org.opengis.filter.Expression) Filter(org.opengis.filter.Filter) ArrayList(java.util.ArrayList) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) PointSymbolizer(org.opengis.style.PointSymbolizer) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) LineSymbolizer(org.opengis.style.LineSymbolizer) Symbolizer(org.opengis.style.Symbolizer)

Example 5 with MutableRule

use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.

the class SEforSLD100Test method testRule.

@Test
public void testRule() throws JAXBException {
    final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
    final Marshaller MARSHALLER = POOL.acquireMarshaller();
    // Read test
    Object obj = UNMARSHALLER.unmarshal(FILE_SE_RULE);
    assertNotNull(obj);
    Rule jax = (Rule) obj;
    MutableRule rule = TRANSFORMER_GT.visitRule(jax);
    assertNotNull(rule);
    assertEquals(rule.getName(), valueName);
    assertEquals(rule.getDescription().getTitle().toString(), valueTitle);
    assertEquals(rule.getDescription().getAbstract().toString(), valueAbstract);
    assertEquals(rule.getMinScaleDenominator(), 500d, DELTA);
    assertEquals(rule.getMaxScaleDenominator(), 1000d, DELTA);
    assertNull(rule.getLegend());
    assertNotNull(rule.getFilter());
    assertEquals(rule.symbolizers().size(), 4);
    // Write test
    Rule pvt = TRANSFORMER_OGC.visit(rule, null);
    assertNotNull(pvt);
    assertEquals(pvt.getName(), valueName);
    assertEquals(pvt.getTitle(), valueTitle);
    assertEquals(pvt.getAbstract(), valueAbstract);
    assertEquals(pvt.getMinScaleDenominator(), 500d, DELTA);
    assertEquals(pvt.getMaxScaleDenominator(), 1000d, DELTA);
    assertNull(pvt.getLegendGraphic());
    assertEquals(pvt.getSymbolizer().size(), 4);
    MARSHALLER.marshal(pvt, TEST_FILE_SE_RULE);
    POOL.recycle(MARSHALLER);
    POOL.recycle(UNMARSHALLER);
}
Also used : MutableRule(org.geotoolkit.style.MutableRule) Marshaller(javax.xml.bind.Marshaller) MutableRule(org.geotoolkit.style.MutableRule) Rule(org.geotoolkit.sld.xml.v100.Rule) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Aggregations

MutableRule (org.geotoolkit.style.MutableRule)18 MutableFeatureTypeStyle (org.geotoolkit.style.MutableFeatureTypeStyle)8 Symbolizer (org.opengis.style.Symbolizer)7 MutableStyle (org.geotoolkit.style.MutableStyle)6 Feature (org.opengis.feature.Feature)5 Expression (org.opengis.filter.Expression)5 Filter (org.opengis.filter.Filter)5 Rule (org.opengis.style.Rule)5 ArrayList (java.util.ArrayList)4 LineSymbolizer (org.opengis.style.LineSymbolizer)4 PointSymbolizer (org.opengis.style.PointSymbolizer)4 PolygonSymbolizer (org.opengis.style.PolygonSymbolizer)4 List (java.util.List)3 FeatureType (org.opengis.feature.FeatureType)3 ValueReference (org.opengis.filter.ValueReference)3 FeatureTypeStyle (org.opengis.style.FeatureTypeStyle)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 JAXBElement (javax.xml.bind.JAXBElement)2