Search in sources :

Example 1 with Graphic

use of org.opengis.style.Graphic in project geotoolkit by Geomatys.

the class ProjectedGeometryTest method createProjectedGeometry.

private static ProjectedGeometry createProjectedGeometry(Geometry geometry, Dimension canvasBounds, AffineTransform objToDisp) throws NoninvertibleTransformException, TransformException, FactoryException {
    final int canvasWidth = canvasBounds.width;
    final int canvasHeight = canvasBounds.height;
    // build a maplayer
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Geometry.class).setName("geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
    final FeatureType type = ftb.build();
    final Feature feature = type.newInstance();
    JTS.setCRS(geometry, CommonCRS.WGS84.normalizedGeographic());
    feature.setPropertyValue("geom", geometry);
    final FeatureSet col = new InMemoryFeatureSet(type, Arrays.asList(feature));
    final List<GraphicalSymbol> symbols = new ArrayList<>();
    symbols.add(SF.mark(StyleConstants.MARK_SQUARE, SF.fill(Color.BLACK), SF.stroke(Color.BLACK, 0)));
    final Graphic graphic = SF.graphic(symbols, StyleConstants.LITERAL_ONE_FLOAT, FF.literal(2), StyleConstants.LITERAL_ZERO_FLOAT, null, null);
    final PointSymbolizer ps = SF.pointSymbolizer(graphic, null);
    final MutableStyle style = SF.style(ps);
    final MapLayer layer = MapBuilder.createLayer(col);
    layer.setStyle(style);
    // build a rendering canvas
    final J2DCanvasBuffered canvas = new J2DCanvasBuffered(CommonCRS.WGS84.normalizedGeographic(), new Dimension(canvasWidth, canvasHeight));
    canvas.applyTransform(objToDisp);
    final RenderingContext2D context = canvas.prepareContext(new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB).createGraphics());
    final ProjectedGeometry pg = new ProjectedGeometry(context);
    pg.setDataGeometry(geometry, CommonCRS.WGS84.normalizedGeographic());
    Envelope env = canvas.getVisibleEnvelope();
    System.out.println(env.getMinimum(0) + " " + env.getMaximum(0));
    System.out.println(env.getMinimum(1) + " " + env.getMaximum(1));
    return pg;
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) PointSymbolizer(org.opengis.style.PointSymbolizer) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Graphic(org.opengis.style.Graphic) GraphicalSymbol(org.opengis.style.GraphicalSymbol) MapLayer(org.apache.sis.portrayal.MapLayer) ArrayList(java.util.ArrayList) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) Dimension(java.awt.Dimension) Envelope(org.opengis.geometry.Envelope) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) J2DCanvasBuffered(org.geotoolkit.display2d.canvas.J2DCanvasBuffered) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet)

Example 2 with Graphic

use of org.opengis.style.Graphic in project geotoolkit by Geomatys.

the class CachedPointSymbolizerTest method testMargin.

@Test
public void testMargin() throws FactoryException {
    final GridGeometry grid = new GridGeometry(new GridExtent(1, 1), CRS.getDomainOfValidity(CommonCRS.WGS84.normalizedGeographic()), GridOrientation.HOMOTHETY);
    final RenderingContext2D ctx = new RenderingContext2D(grid, null);
    {
        // NO ANCHOR, NO DISPLACEMENT
        final List<GraphicalSymbol> symbols = new ArrayList<>();
        final Stroke stroke = SF.stroke(Color.BLACK, 2);
        final Fill fill = SF.fill(Color.RED);
        final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
        symbols.add(mark);
        final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, DEFAULT_DISPLACEMENT);
        final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
        final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
        final float margin = cached.getMargin(null, ctx);
        // 12/2 + 2*2(stroke width)
        assertEquals(8f, margin, DELTA);
    }
    {
        // NO ANCHOR
        final List<GraphicalSymbol> symbols = new ArrayList<>();
        final Stroke stroke = SF.stroke(Color.BLACK, 2);
        final Fill fill = SF.fill(Color.RED);
        final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
        symbols.add(mark);
        final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, DEFAULT_ANCHOR_POINT, SF.displacement(10, 15));
        final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
        final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
        final float margin = cached.getMargin(null, ctx);
        // 12/2 + 2*2(stroke width) + 15(disp)
        assertEquals(23f, margin, DELTA);
    }
    {
        final List<GraphicalSymbol> symbols = new ArrayList<>();
        final Stroke stroke = SF.stroke(Color.BLACK, 2);
        final Fill fill = SF.fill(Color.RED);
        final Mark mark = SF.mark(MARK_CIRCLE, fill, stroke);
        symbols.add(mark);
        final Graphic graphic = SF.graphic(symbols, LITERAL_ONE_FLOAT, FF.literal(12), LITERAL_ONE_FLOAT, SF.anchorPoint(0, 1.7), SF.displacement(10, 15));
        final PointSymbolizer symbolizer = SF.pointSymbolizer("mySymbol", (String) null, DEFAULT_DESCRIPTION, Units.POINT, graphic);
        final CachedSymbolizer cached = GO2Utilities.getCached(symbolizer, null);
        final float margin = cached.getMargin(null, ctx);
        // 12/2 + 2*2(stroke width) + 15(disp) + 16*(1.7-0.5)
        assertEquals(23f + 19.2f, margin, DELTA);
    }
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) PointSymbolizer(org.opengis.style.PointSymbolizer) GridExtent(org.apache.sis.coverage.grid.GridExtent) Stroke(org.opengis.style.Stroke) Fill(org.opengis.style.Fill) Graphic(org.opengis.style.Graphic) RenderingContext2D(org.geotoolkit.display2d.canvas.RenderingContext2D) Mark(org.opengis.style.Mark) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with Graphic

use of org.opengis.style.Graphic 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 Graphic

use of org.opengis.style.Graphic in project geotoolkit by Geomatys.

the class IntervalStyleBuilder method derivateSymbolizer.

/**
 * Derivate a symbolizer with a new color.
 */
private Symbolizer derivateSymbolizer(final Symbolizer symbol, final Color color) {
    if (symbol instanceof PolygonSymbolizer) {
        PolygonSymbolizer ps = (PolygonSymbolizer) symbol;
        Fill fill = sf.fill(sf.literal(color), ps.getFill().getOpacity());
        return sf.polygonSymbolizer(ps.getName(), ps.getGeometryPropertyName(), ps.getDescription(), ps.getUnitOfMeasure(), ps.getStroke(), fill, ps.getDisplacement(), ps.getPerpendicularOffset());
    } else if (symbol instanceof LineSymbolizer) {
        LineSymbolizer ls = (LineSymbolizer) symbol;
        Stroke oldStroke = ls.getStroke();
        Stroke stroke = sf.stroke(sf.literal(color), oldStroke.getOpacity(), oldStroke.getWidth(), oldStroke.getLineJoin(), oldStroke.getLineCap(), oldStroke.getDashArray(), oldStroke.getDashOffset());
        return sf.lineSymbolizer(ls.getName(), ls.getGeometryPropertyName(), ls.getDescription(), ls.getUnitOfMeasure(), stroke, ls.getPerpendicularOffset());
    } else if (symbol instanceof PointSymbolizer) {
        PointSymbolizer ps = (PointSymbolizer) symbol;
        Graphic oldGraphic = ps.getGraphic();
        Mark oldMark = (Mark) oldGraphic.graphicalSymbols().get(0);
        Fill fill = sf.fill(sf.literal(color), oldMark.getFill().getOpacity());
        List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
        symbols.add(sf.mark(oldMark.getWellKnownName(), fill, oldMark.getStroke()));
        Graphic graphic = sf.graphic(symbols, oldGraphic.getOpacity(), oldGraphic.getSize(), oldGraphic.getRotation(), oldGraphic.getAnchorPoint(), oldGraphic.getDisplacement());
        return sf.pointSymbolizer(graphic, ps.getGeometryPropertyName());
    } else {
        throw new IllegalArgumentException("unexpected symbolizer type : " + symbol);
    }
}
Also used : PointSymbolizer(org.opengis.style.PointSymbolizer) Fill(org.opengis.style.Fill) Stroke(org.opengis.style.Stroke) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) Graphic(org.opengis.style.Graphic) LineSymbolizer(org.opengis.style.LineSymbolizer) GraphicalSymbol(org.opengis.style.GraphicalSymbol) ArrayList(java.util.ArrayList) Mark(org.opengis.style.Mark)

Example 5 with Graphic

use of org.opengis.style.Graphic in project geotoolkit by Geomatys.

the class IntervalStyleBuilder method createPointTemplate.

public static PointSymbolizer createPointTemplate() {
    final MutableStyleFactory sf = GO2Utilities.STYLE_FACTORY;
    final FilterFactory ff = GO2Utilities.FILTER_FACTORY;
    final Stroke stroke = sf.stroke(Color.BLACK, 1);
    final Fill fill = sf.fill(Color.BLUE);
    final List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
    symbols.add(sf.mark(StyleConstants.MARK_CIRCLE, fill, stroke));
    final Graphic gra = sf.graphic(symbols, ff.literal(1), ff.literal(12), ff.literal(0), sf.anchorPoint(), sf.displacement());
    return sf.pointSymbolizer(gra, null);
}
Also used : Stroke(org.opengis.style.Stroke) Fill(org.opengis.style.Fill) Graphic(org.opengis.style.Graphic) GraphicalSymbol(org.opengis.style.GraphicalSymbol) ArrayList(java.util.ArrayList) MutableStyleFactory(org.geotoolkit.style.MutableStyleFactory) FilterFactory(org.opengis.filter.FilterFactory)

Aggregations

Graphic (org.opengis.style.Graphic)29 ArrayList (java.util.ArrayList)16 Expression (org.opengis.filter.Expression)16 GraphicalSymbol (org.opengis.style.GraphicalSymbol)15 ExternalGraphic (org.opengis.style.ExternalGraphic)13 PointSymbolizer (org.opengis.style.PointSymbolizer)13 Unit (javax.measure.Unit)10 Description (org.opengis.style.Description)10 Stroke (org.opengis.style.Stroke)10 Fill (org.opengis.style.Fill)9 MutableStyle (org.geotoolkit.style.MutableStyle)8 AnchorPoint (org.opengis.style.AnchorPoint)8 Displacement (org.opengis.style.Displacement)8 Mark (org.opengis.style.Mark)8 LineSymbolizer (org.opengis.style.LineSymbolizer)5 Test (org.junit.Test)4 PolygonSymbolizer (org.opengis.style.PolygonSymbolizer)4 FeatureSet (org.apache.sis.storage.FeatureSet)3 SimpleInternationalString (org.apache.sis.util.SimpleInternationalString)3 Feature (org.opengis.feature.Feature)3