Search in sources :

Example 6 with Expression

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

the class GeometryExpressionTest method bufferTest.

/**
 * Test a buffer expression around geometry.
 */
@Test
public void bufferTest() throws PortrayalException, IOException {
    final CoordinateReferenceSystem crs = CommonCRS.WGS84.normalizedGeographic();
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Point.class).setName("geom").setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = ftb.build();
    final Point point = GO2Utilities.JTS_FACTORY.createPoint(new Coordinate(0, 0));
    JTS.setCRS(point, crs);
    final Feature feature = type.newInstance();
    feature.setPropertyValue("geom", point);
    final Expression geomExp = FF.function("buffer", FF.property("geom"), FF.literal(10));
    final Fill fill = SF.fill(Color.RED);
    final PolygonSymbolizer symbolizer = SF.polygonSymbolizer("", geomExp, DEFAULT_DESCRIPTION, DEFAULT_UOM, null, fill, DEFAULT_DISPLACEMENT, LITERAL_ZERO_FLOAT);
    final MutableStyle style = SF.style(symbolizer);
    final MapLayers context = MapBuilder.createContext();
    final MapLayer fml = MapBuilder.createLayer(FeatureStoreUtilities.collection(feature));
    fml.setStyle(style);
    context.getComponents().add(fml);
    final GeneralEnvelope env = new GeneralEnvelope(crs);
    env.setRange(0, -20, +20);
    env.setRange(1, -20, +20);
    final CanvasDef canvasDef = new CanvasDef();
    canvasDef.setEnvelope(env);
    canvasDef.setDimension(new Dimension(40, 40));
    canvasDef.setBackground(Color.WHITE);
    final SceneDef sceneDef = new SceneDef(context);
    final BufferedImage image = DefaultPortrayalService.portray(canvasDef, sceneDef);
    // we must obtain a red point of 10pixel width at image center
    final int red = Color.RED.getRGB();
    final int white = Color.WHITE.getRGB();
    Assert.assertEquals(white, image.getRGB(20, 9));
    Assert.assertEquals(red, image.getRGB(20, 11));
    Assert.assertEquals(red, image.getRGB(20, 20));
    Assert.assertEquals(red, image.getRGB(20, 29));
    Assert.assertEquals(white, image.getRGB(20, 31));
    Assert.assertEquals(white, image.getRGB(9, 20));
    Assert.assertEquals(red, image.getRGB(11, 20));
    Assert.assertEquals(red, image.getRGB(20, 20));
    Assert.assertEquals(red, image.getRGB(29, 20));
    Assert.assertEquals(white, image.getRGB(31, 20));
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Fill(org.opengis.style.Fill) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) MapLayer(org.apache.sis.portrayal.MapLayer) Point(org.locationtech.jts.geom.Point) Dimension(java.awt.Dimension) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) Point(org.locationtech.jts.geom.Point) MutableStyle(org.geotoolkit.style.MutableStyle) Coordinate(org.locationtech.jts.geom.Coordinate) Expression(org.opengis.filter.Expression) SceneDef(org.geotoolkit.display2d.service.SceneDef) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 7 with Expression

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

the class TextSymbolizerTest method pointLabelTest.

/**
 * Render a label at check it is correctly located in the image.
 */
@Test
public void pointLabelTest() throws Exception {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(Point.class).setName("geom").setCRS(CommonCRS.defaultGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = ftb.build();
    final Feature feature = type.newInstance();
    feature.setPropertyValue("geom", GF.createPoint(new Coordinate(0, 0)));
    final FeatureSet collection = new InMemoryFeatureSet(type, Arrays.asList(feature));
    // text symbolizer style
    final String name = "mySymbol";
    final Description desc = DEFAULT_DESCRIPTION;
    // use the default geometry of the feature
    final String geometry = null;
    final Unit unit = Units.POINT;
    final Expression label = FF.literal("LABEL");
    final Font font = SF.font(FF.literal("Arial"), FONT_STYLE_ITALIC, FONT_WEIGHT_BOLD, FF.literal(14));
    final LabelPlacement placement = SF.pointPlacement();
    final Halo halo = SF.halo(Color.WHITE, 0);
    final Fill fill = SF.fill(Color.BLUE);
    final TextSymbolizer symbol = SF.textSymbolizer(name, geometry, desc, unit, label, font, placement, halo, fill);
    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 GeneralEnvelope env = new GeneralEnvelope(CommonCRS.defaultGeographic());
    env.setRange(0, -180, +180);
    env.setRange(1, -90, +90);
    final Hints hints = new Hints();
    hints.put(GO2Hints.KEY_COLOR_MODEL, ColorModel.getRGBdefault());
    final SceneDef scenedef = new SceneDef(context, hints);
    final CanvasDef canvasdef = new CanvasDef(new Dimension(360, 180), env);
    canvasdef.setBackground(Color.WHITE);
    final BufferedImage buffer = DefaultPortrayalService.portray(canvasdef, scenedef);
    // ImageIO.write(buffer, "PNG", new File("test.png"));
    // we expect to have a blue label at the center of the image
    final int[] pixel = new int[4];
    final int[] blue = new int[] { 0, 0, 255, 255 };
    final Raster raster = buffer.getData();
    boolean found = false;
    for (int x = 160; x < 200; x++) {
        // should be exactly at the center
        raster.getPixel(x, 90, pixel);
        if (Arrays.equals(blue, pixel)) {
            found = true;
        }
    }
    assertTrue("label not found", found);
}
Also used : FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) Fill(org.opengis.style.Fill) Description(org.opengis.style.Description) Hints(org.geotoolkit.factory.Hints) GO2Hints(org.geotoolkit.display2d.GO2Hints) MapLayer(org.apache.sis.portrayal.MapLayer) Unit(javax.measure.Unit) Feature(org.opengis.feature.Feature) Font(org.opengis.style.Font) BufferedImage(java.awt.image.BufferedImage) MutableStyle(org.geotoolkit.style.MutableStyle) SceneDef(org.geotoolkit.display2d.service.SceneDef) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) Raster(java.awt.image.Raster) Point(org.locationtech.jts.geom.Point) Dimension(java.awt.Dimension) Point(org.locationtech.jts.geom.Point) LabelPlacement(org.opengis.style.LabelPlacement) Coordinate(org.locationtech.jts.geom.Coordinate) Expression(org.opengis.filter.Expression) TextSymbolizer(org.opengis.style.TextSymbolizer) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) GeneralEnvelope(org.apache.sis.geometry.GeneralEnvelope) CanvasDef(org.geotoolkit.display2d.service.CanvasDef) Halo(org.opengis.style.Halo) MapLayers(org.apache.sis.portrayal.MapLayers) Test(org.junit.Test)

Example 8 with Expression

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

the class StyleCacheTest method GraphicCacheTest.

@Test
public void GraphicCacheTest() throws Exception {
    // Test a complex graphic
    final Expression Lookup = FF.property("POP_CNTRY");
    final List<InterpolationPoint> values = new ArrayList<InterpolationPoint>();
    // test color interpolation ---------------------------------------------
    values.clear();
    values.add(new DefaultInterpolationPoint(0d, FF.literal(3d)));
    values.add(new DefaultInterpolationPoint(500000000d, FF.literal(50d)));
    Interpolate interpolate = new DefaultInterpolate(Lookup, values, Method.COLOR, Mode.CUBIC, null);
    List<GraphicalSymbol> symbols = new ArrayList<GraphicalSymbol>();
    symbols.add(SF.mark(StyleConstants.MARK_CIRCLE, SF.fill(Color.RED), SF.stroke()));
    Graphic graphic = SF.graphic(symbols, StyleConstants.DEFAULT_GRAPHIC_OPACITY, interpolate, StyleConstants.DEFAULT_GRAPHIC_ROTATION, StyleConstants.DEFAULT_ANCHOR_POINT, StyleConstants.DEFAULT_DISPLACEMENT);
    CachedGraphic cached = CachedGraphic.cache(graphic);
    assertFalse(cached.isStatic());
    assertEquals(VisibilityState.DYNAMIC, cached.isStaticVisible());
}
Also used : DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) DefaultInterpolate(org.geotoolkit.style.function.DefaultInterpolate) Interpolate(org.geotoolkit.style.function.Interpolate) CachedGraphic(org.geotoolkit.display2d.style.CachedGraphic) Expression(org.opengis.filter.Expression) Graphic(org.opengis.style.Graphic) CachedGraphic(org.geotoolkit.display2d.style.CachedGraphic) ExternalGraphic(org.opengis.style.ExternalGraphic) GraphicalSymbol(org.opengis.style.GraphicalSymbol) ArrayList(java.util.ArrayList) DefaultInterpolate(org.geotoolkit.style.function.DefaultInterpolate) DefaultInterpolationPoint(org.geotoolkit.style.function.DefaultInterpolationPoint) Test(org.junit.Test)

Example 9 with Expression

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

the class PatternSymbolizerTest method testXml.

/**
 * Test Jaxb xml support.
 */
@Test
public void testXml() throws JAXBException, IOException {
    final MutableStyleFactory SF = GO2Utilities.STYLE_FACTORY;
    final FilterFactory2 FF = GO2Utilities.FILTER_FACTORY;
    final Map<Expression, List<Symbolizer>> ranges = new LinkedHashMap<>();
    ranges.put(FF.literal(-1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.BLUE), null)));
    ranges.put(FF.literal(-500), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.RED), null)));
    ranges.put(FF.literal(-100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GREEN), null)));
    ranges.put(FF.literal(100), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.YELLOW), null)));
    ranges.put(FF.literal(1000), Arrays.asList(SF.polygonSymbolizer(null, SF.fill(Color.GRAY), null)));
    final PatternSymbolizer ps = new PatternSymbolizer(FF.literal(0), ranges, ThreshholdsBelongTo.PRECEDING);
    final MutableStyle style = GO2Utilities.STYLE_FACTORY.style(ps);
    final Path path = Files.createTempFile("xml", ".xml");
    IOUtilities.deleteOnExit(path);
    new StyleXmlIO().writeStyle(path, style, Specification.StyledLayerDescriptor.V_1_1_0);
}
Also used : Path(java.nio.file.Path) MutableStyle(org.geotoolkit.style.MutableStyle) Expression(org.opengis.filter.Expression) StyleXmlIO(org.geotoolkit.sld.xml.StyleXmlIO) List(java.util.List) MutableStyleFactory(org.geotoolkit.style.MutableStyleFactory) FilterFactory2(org.geotoolkit.filter.FilterFactory2) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 10 with Expression

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

the class CachedStrokeSimple method getJ2DStroke.

/**
 * Get the java2D Stroke for the given feature.
 *
 * @param candidate : evaluate stroke with the given feature
 * @param coeff : use to adjust stroke size, if in display unit value equals 1
 * @return Java2D Stroke
 */
public java.awt.Stroke getJ2DStroke(final Object candidate, float coeff) {
    evaluate();
    coeff = Math.abs(coeff);
    java.awt.Stroke j2dStroke = cachedStroke;
    // if stroke is null it means something is dynamic
    if (j2dStroke == null || coeff != 1) {
        float[] candidateDashes = cachedDashes;
        float candidateOffset = cachedDashOffset;
        int candidateCap = cachedCap;
        int candidateJoin = cachedJoin;
        float candidateWidth = cachedWidth;
        if (Float.isNaN(candidateOffset)) {
            final Expression expOffset = styleElement.getDashOffset();
            candidateOffset = GO2Utilities.evaluate(expOffset, candidate, Float.class, 1f);
        }
        if (candidateCap == Integer.MAX_VALUE) {
            final Expression expCap = styleElement.getLineCap();
            final String cap = GO2Utilities.evaluate(expCap, null, String.class, STROKE_CAP_BUTT_STRING);
            if (STROKE_CAP_BUTT_STRING.equalsIgnoreCase(cap))
                candidateCap = BasicStroke.CAP_BUTT;
            else if (STROKE_CAP_SQUARE_STRING.equalsIgnoreCase(cap))
                candidateCap = BasicStroke.CAP_SQUARE;
            else if (STROKE_CAP_ROUND_STRING.equalsIgnoreCase(cap))
                candidateCap = BasicStroke.CAP_ROUND;
            else
                candidateCap = BasicStroke.CAP_BUTT;
        }
        if (candidateJoin == Integer.MAX_VALUE) {
            final Expression expJoin = styleElement.getLineJoin();
            final String join = GO2Utilities.evaluate(expJoin, null, String.class, STROKE_JOIN_BEVEL_STRING);
            if (STROKE_JOIN_BEVEL_STRING.equalsIgnoreCase(join))
                candidateJoin = BasicStroke.JOIN_BEVEL;
            else if (STROKE_JOIN_MITRE_STRING.equalsIgnoreCase(join))
                candidateJoin = BasicStroke.JOIN_MITER;
            else if (STROKE_JOIN_ROUND_STRING.equalsIgnoreCase(join))
                candidateJoin = BasicStroke.JOIN_ROUND;
            else
                candidateJoin = BasicStroke.JOIN_BEVEL;
        }
        if (Float.isNaN(candidateWidth)) {
            final Expression expWidth = styleElement.getWidth();
            candidateWidth = GO2Utilities.evaluate(expWidth, candidate, Float.class, 1f);
            if (candidateWidth < 0) {
                candidateWidth = 0f;
            }
        }
        if (candidateDashes != null) {
            float[] s = candidateDashes.clone();
            for (int i = 0; i < s.length; i++) {
                s[i] = s[i] * coeff;
            }
            j2dStroke = new BasicStroke(candidateWidth * coeff, candidateCap, candidateJoin, 1f, s, candidateOffset);
        } else {
            j2dStroke = new BasicStroke(candidateWidth * coeff, candidateCap, candidateJoin, 10f);
        }
    }
    return j2dStroke;
}
Also used : BasicStroke(java.awt.BasicStroke) Expression(org.opengis.filter.Expression) TexturePaint(java.awt.TexturePaint) Paint(java.awt.Paint)

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