Search in sources :

Example 1 with Expression

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

the class KmzContextInterpreter method writeFeature.

/**
 * Transforms a feature into KML feature (Placemak if original
 * features contents a geometry, or Folder otherwise).
 */
private Feature writeFeature(final Feature feature) {
    Feature kmlFeature = null;
    for (final PropertyType type : feature.getType().getProperties(true)) {
        final Object val = feature.getPropertyValue(type.getName().toString());
        if (val instanceof Feature) {
            kmlFeature = KML_FACTORY.createFolder();
            kmlFeature.setPropertyValue(KmlConstants.TAG_FEATURES, writeFeature((Feature) val));
        } else if (val instanceof Geometry) {
            kmlFeature = KML_FACTORY.createPlacemark();
            kmlFeature.setPropertyValue(KmlConstants.TAG_GEOMETRY, val);
        } else {
        // System.out.println("PAS FEATURE.");
        }
    }
    // Search feature style URI
    for (Entry<Rule, URI> e : IDENTIFICATORS_MAP) {
        final Rule rule = e.getKey();
        if (rule.getFilter().test(feature)) {
            kmlFeature.setPropertyValue(KmlConstants.TAG_STYLE_URL, e.getValue());
            for (Symbolizer s : rule.symbolizers()) {
                if (s instanceof TextSymbolizer) {
                    final Expression label = ((TextSymbolizer) s).getLabel();
                    if (label != null) {
                        kmlFeature.setPropertyValue(KmlConstants.TAG_NAME, writeExpression(label, String.class, feature));
                    }
                }
            }
            break;
        }
    }
    return kmlFeature;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) TextSymbolizer(org.opengis.style.TextSymbolizer) Expression(org.opengis.filter.Expression) PropertyType(org.opengis.feature.PropertyType) Rule(org.opengis.style.Rule) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature) URI(java.net.URI) ExtensionSymbolizer(org.opengis.style.ExtensionSymbolizer) PointSymbolizer(org.opengis.style.PointSymbolizer) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) LineSymbolizer(org.opengis.style.LineSymbolizer) RasterSymbolizer(org.opengis.style.RasterSymbolizer) TextSymbolizer(org.opengis.style.TextSymbolizer) Symbolizer(org.opengis.style.Symbolizer)

Example 2 with Expression

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

the class PortrayalServiceTest method testCoveragePropertyRendering.

/**
 * Test rendering of a coverage inside a feature property.
 */
@Test
public void testCoveragePropertyRendering() throws Exception {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(GridCoverage.class).setName("coverage");
    final FeatureType ft = ftb.build();
    final BufferedImage img = new BufferedImage(90, 90, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = img.createGraphics();
    g.setColor(Color.GREEN);
    g.fillRect(0, 0, 90, 90);
    g.dispose();
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CENTER, new AffineTransform2D(1, 0, 0, 1, 0.5, 0.5), CommonCRS.WGS84.normalizedGeographic()));
    gcb.setValues(img);
    final Feature f = ft.newInstance();
    f.setPropertyValue("coverage", gcb.build());
    final FeatureSet collection = new InMemoryFeatureSet(ft, Arrays.asList(f));
    final String name = "mySymbol";
    final Description desc = DEFAULT_DESCRIPTION;
    final String geometry = "coverage";
    final Unit unit = Units.POINT;
    final Expression opacity = LITERAL_ONE_FLOAT;
    final ChannelSelection channels = null;
    final OverlapBehavior overlap = null;
    final ColorMap colormap = null;
    final ContrastEnhancement enhance = null;
    final ShadedRelief relief = null;
    final Symbolizer outline = null;
    final RasterSymbolizer symbol = SF.rasterSymbolizer(name, geometry, desc, unit, opacity, channels, overlap, colormap, enhance, relief, outline);
    final MutableStyle style = SF.style(symbol);
    final MapLayer layer = MapBuilder.createLayer(collection);
    layer.setStyle(style);
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(layer);
    final SceneDef sdef = new SceneDef(context);
    final GeneralEnvelope env = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic());
    env.setRange(0, -180, +180);
    env.setRange(1, -90, +90);
    final CanvasDef cdef = new CanvasDef(new Dimension(360, 180), env);
    final BufferedImage result = DefaultPortrayalService.portray(cdef, sdef);
    final Raster raster = result.getData();
    final int[] pixel = new int[4];
    final int[] trans = new int[] { 0, 0, 0, 0 };
    final int[] green = new int[] { 0, 255, 0, 255 };
    assertNotNull(result);
    raster.getPixel(0, 0, pixel);
    assertArrayEquals(trans, pixel);
    raster.getPixel(179, 45, pixel);
    assertArrayEquals(trans, pixel);
    raster.getPixel(181, 45, pixel);
    assertArrayEquals(green, pixel);
}
Also used : FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) MapLayer(org.apache.sis.portrayal.MapLayer) Unit(javax.measure.Unit) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Dimension(java.awt.Dimension) SampleDimension(org.apache.sis.coverage.SampleDimension) Graphics2D(java.awt.Graphics2D) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) Expression(org.opengis.filter.Expression) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 3 with Expression

use of org.opengis.filter.Expression 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 4 with Expression

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

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

Aggregations

Expression (org.opengis.filter.Expression)325 Test (org.junit.Test)112 LineString (org.locationtech.jts.geom.LineString)73 Literal (org.opengis.filter.Literal)65 ArrayList (java.util.ArrayList)47 MultiLineString (org.locationtech.jts.geom.MultiLineString)46 Unit (javax.measure.Unit)45 Description (org.opengis.style.Description)40 Fill (org.opengis.style.Fill)38 GraphicFill (org.opengis.style.GraphicFill)38 Stroke (org.opengis.style.Stroke)35 Geometry (org.locationtech.jts.geom.Geometry)31 Displacement (org.opengis.style.Displacement)29 GraphicStroke (org.opengis.style.GraphicStroke)29 ValueReference (org.opengis.filter.ValueReference)27 JAXBElement (javax.xml.bind.JAXBElement)22 LineSymbolizer (org.opengis.style.LineSymbolizer)22 PointSymbolizer (org.opengis.style.PointSymbolizer)22 PolygonSymbolizer (org.opengis.style.PolygonSymbolizer)22 MutableStyle (org.geotoolkit.style.MutableStyle)21