Search in sources :

Example 1 with TextSymbolizer

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

the class KmzContextInterpreter method writeSymbolizer.

/**
 * Writes KML color styles mapping SLD Symbolizers.
 * Color styles are written into KML Style selector.
 */
private AbstractStyleSelector writeSymbolizer(Symbolizer symbolizer, Style styleSelector) {
    if (symbolizer instanceof ExtensionSymbolizer) {
    } else // LineSymbolizer mapping
    if (symbolizer instanceof LineSymbolizer) {
        final LineSymbolizer lineSymbolizer = (LineSymbolizer) symbolizer;
        final LineStyle lineStyle = ((styleSelector.getLineStyle() == null) ? KML_FACTORY.createLineStyle() : styleSelector.getLineStyle());
        lineStyle.setWidth((Double) this.writeExpression(lineSymbolizer.getStroke().getWidth(), Double.class, null));
        lineStyle.setColor((Color) this.writeExpression(lineSymbolizer.getStroke().getColor(), Color.class, null));
        styleSelector.setLineStyle(lineStyle);
    } else // PointSymbolizezr mapping
    if (symbolizer instanceof PointSymbolizer) {
    // PointSymbolizer pointSymbolizer = (PointSymbolizer) symbolizer;
    // IconStyle iconStyle = KML_FACTORY.createIconStyle();
    // GraphicalSymbol gs = ((GraphicalSymbol) pointSymbolizer.getGraphic().graphicalSymbols().get(0));
    // gs.
    } else // PolygonSymbolizer mapping
    if (symbolizer instanceof PolygonSymbolizer) {
        final PolygonSymbolizer polygonSymbolizer = (PolygonSymbolizer) symbolizer;
        final PolyStyle polyStyle = KML_FACTORY.createPolyStyle();
        // Fill
        if (polygonSymbolizer.getFill() == null) {
            polyStyle.setFill(false);
        } else {
            polyStyle.setFill(true);
            polyStyle.setColor((Color) this.writeExpression(polygonSymbolizer.getFill().getColor(), Color.class, null));
        }
        // Outline
        if (polygonSymbolizer.getStroke() == null) {
            polyStyle.setOutline(false);
        } else if (styleSelector.getLineStyle() == null) {
            polyStyle.setOutline(true);
            final LineStyle lineStyle = KML_FACTORY.createLineStyle();
            lineStyle.setColor((Color) this.writeExpression(polygonSymbolizer.getStroke().getColor(), Color.class, null));
            lineStyle.setWidth((Double) this.writeExpression(polygonSymbolizer.getStroke().getWidth(), Double.class, null));
            styleSelector.setLineStyle(lineStyle);
        }
        styleSelector.setPolyStyle(polyStyle);
    } else if (symbolizer instanceof RasterSymbolizer) {
    } else if (symbolizer instanceof TextSymbolizer) {
        final TextSymbolizer textSymbolizer = (TextSymbolizer) symbolizer;
        final LabelStyle labelStyle = KML_FACTORY.createLabelStyle();
        if (textSymbolizer.getFont() != null) {
            textSymbolizer.getFont().getSize();
        }
        if (textSymbolizer.getFill() != null) {
            labelStyle.setColor((Color) this.writeExpression(textSymbolizer.getFill().getColor(), Color.class, null));
        }
        styleSelector.setLabelStyle(labelStyle);
    }
    return styleSelector;
}
Also used : RasterSymbolizer(org.opengis.style.RasterSymbolizer) PointSymbolizer(org.opengis.style.PointSymbolizer) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) TextSymbolizer(org.opengis.style.TextSymbolizer) LineStyle(org.geotoolkit.data.kml.model.LineStyle) LineSymbolizer(org.opengis.style.LineSymbolizer) Color(java.awt.Color) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) ExtensionSymbolizer(org.opengis.style.ExtensionSymbolizer) PolyStyle(org.geotoolkit.data.kml.model.PolyStyle)

Example 2 with TextSymbolizer

use of org.opengis.style.TextSymbolizer 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 3 with TextSymbolizer

use of org.opengis.style.TextSymbolizer 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 4 with TextSymbolizer

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

the class SEforSLD100Test method testTextSymbolizer.

@Test
public void testTextSymbolizer() throws JAXBException {
    final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
    final Marshaller MARSHALLER = POOL.acquireMarshaller();
    // Read test
    Object obj = UNMARSHALLER.unmarshal(FILE_SE_SYMBOL_TEXT);
    assertNotNull(obj);
    JAXBElement<org.geotoolkit.sld.xml.v100.TextSymbolizer> jax = (JAXBElement<org.geotoolkit.sld.xml.v100.TextSymbolizer>) obj;
    TextSymbolizer textSymbol = TRANSFORMER_GT.visit(jax.getValue());
    assertNotNull(textSymbol);
    assertEquals(textSymbol.getGeometryPropertyName(), valueGeom);
    assertEquals(Units.POINT, textSymbol.getUnitOfMeasure());
    assertNotNull(textSymbol.getFill());
    assertEquals(floatValue(textSymbol.getFill().getOpacity()), 1.0f, DELTA);
    assertEquals(stringValue(textSymbol.getFill().getColor()), "#808080");
    assertEquals(floatValue(textSymbol.getHalo().getRadius()), 5f, DELTA);
    assertEquals(floatValue(textSymbol.getHalo().getFill().getOpacity()), 0.52f, DELTA);
    assertEquals(((ValueReference) textSymbol.getLabel()).getXPath(), "aField");
    assertEquals(stringValue(textSymbol.getFont().getFamily().get(0)), "arial");
    assertEquals(stringValue(textSymbol.getFont().getFamily().get(1)), "serif");
    assertEquals(floatValue(textSymbol.getFont().getSize()), 17f, DELTA);
    assertEquals(stringValue(textSymbol.getFont().getStyle()), "italic");
    assertEquals(stringValue(textSymbol.getFont().getWeight()), "bold");
    // Write test
    JAXBElement<org.geotoolkit.sld.xml.v100.TextSymbolizer> pvt = TRANSFORMER_OGC.visit(textSymbol, null);
    assertNotNull(pvt);
    assertEquals(pvt.getValue().getGeometry().getPropertyName().getContent(), "");
    assertNotNull(pvt.getValue().getFill());
    MARSHALLER.marshal(pvt, TEST_FILE_SE_SYMBOL_TEXT);
    POOL.recycle(MARSHALLER);
    POOL.recycle(UNMARSHALLER);
}
Also used : Marshaller(javax.xml.bind.Marshaller) TextSymbolizer(org.opengis.style.TextSymbolizer) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 5 with TextSymbolizer

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

the class SEforSLD110Test method testTextSymbolizer.

@Test
public void testTextSymbolizer() throws JAXBException {
    final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
    final Marshaller MARSHALLER = POOL.acquireMarshaller();
    // Read test
    Object obj = UNMARSHALLER.unmarshal(FILE_SE_SYMBOL_TEXT);
    assertNotNull(obj);
    JAXBElement<org.geotoolkit.se.xml.v110.TextSymbolizerType> jax = (JAXBElement<org.geotoolkit.se.xml.v110.TextSymbolizerType>) obj;
    TextSymbolizer textSymbol = TRANSFORMER_GT.visit(jax.getValue());
    assertNotNull(textSymbol);
    assertEquals(textSymbol.getGeometryPropertyName(), valueGeom);
    assertEquals(Units.FOOT, textSymbol.getUnitOfMeasure());
    assertNotNull(textSymbol.getFill());
    assertEquals(floatValue(textSymbol.getFill().getOpacity()), 1.0f, DELTA);
    assertEquals(colorValue(textSymbol.getFill().getColor()), ObjectConverters.convert("#FFC800", Color.class));
    assertEquals(floatValue(textSymbol.getHalo().getRadius()), 5f, DELTA);
    assertEquals(floatValue(textSymbol.getHalo().getFill().getOpacity()), 0.52f, DELTA);
    assertEquals(stringValue(textSymbol.getLabel()), "aField");
    assertEquals(stringValue(textSymbol.getFont().getFamily().get(0)), "arial");
    assertEquals(stringValue(textSymbol.getFont().getFamily().get(1)), "serif");
    assertEquals(floatValue(textSymbol.getFont().getSize()), 17f, DELTA);
    assertEquals(stringValue(textSymbol.getFont().getStyle()), "italic");
    assertEquals(stringValue(textSymbol.getFont().getWeight()), "bold");
    // Write test
    JAXBElement<org.geotoolkit.se.xml.v110.TextSymbolizerType> pvt = TRANSFORMER_OGC.visit(textSymbol, null);
    assertNotNull(pvt);
    assertEquals(pvt.getValue().getGeometry(), null);
    assertNotNull(pvt.getValue().getFill());
    MARSHALLER.marshal(pvt, TEST_FILE_SE_SYMBOL_TEXT);
    POOL.recycle(MARSHALLER);
    POOL.recycle(UNMARSHALLER);
}
Also used : Marshaller(javax.xml.bind.Marshaller) Color(java.awt.Color) JAXBElement(javax.xml.bind.JAXBElement) TextSymbolizer(org.opengis.style.TextSymbolizer) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Aggregations

TextSymbolizer (org.opengis.style.TextSymbolizer)14 LineSymbolizer (org.opengis.style.LineSymbolizer)7 PointSymbolizer (org.opengis.style.PointSymbolizer)6 PolygonSymbolizer (org.opengis.style.PolygonSymbolizer)6 MutableStyle (org.geotoolkit.style.MutableStyle)5 Expression (org.opengis.filter.Expression)5 RasterSymbolizer (org.opengis.style.RasterSymbolizer)5 Unit (javax.measure.Unit)4 Test (org.junit.Test)4 Description (org.opengis.style.Description)4 ExtensionSymbolizer (org.opengis.style.ExtensionSymbolizer)4 Fill (org.opengis.style.Fill)4 Font (org.opengis.style.Font)4 Halo (org.opengis.style.Halo)4 LabelPlacement (org.opengis.style.LabelPlacement)4 Symbolizer (org.opengis.style.Symbolizer)4 Color (java.awt.Color)2 JAXBElement (javax.xml.bind.JAXBElement)2 Marshaller (javax.xml.bind.Marshaller)2 Unmarshaller (javax.xml.bind.Unmarshaller)2