Search in sources :

Example 1 with LabelStyle

use of org.geotoolkit.data.kml.model.LabelStyle 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 LabelStyle

use of org.geotoolkit.data.kml.model.LabelStyle in project geotoolkit by Geomatys.

the class DocumentTest method documentWriteTest.

@Test
public void documentWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Feature placemark0 = kmlFactory.createPlacemark();
    final double longitude00 = -122.371;
    final double latitude00 = 37.816;
    final double altitude00 = 0;
    final Coordinate coordinate00 = kmlFactory.createCoordinate(longitude00, latitude00, altitude00);
    final CoordinateSequence coordinates0 = kmlFactory.createCoordinates(Arrays.asList(coordinate00));
    final Point point0 = kmlFactory.createPoint(coordinates0);
    placemark0.setPropertyValue(KmlConstants.TAG_GEOMETRY, point0);
    placemark0.setPropertyValue(KmlConstants.TAG_NAME, "Document Feature 1");
    placemark0.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#exampleStyleDocument"));
    final Feature placemark1 = kmlFactory.createPlacemark();
    final double longitude10 = -122.370;
    final double latitude10 = 37.817;
    final double altitude10 = 0;
    final Coordinate coordinate10 = kmlFactory.createCoordinate(longitude10, latitude10, altitude10);
    final CoordinateSequence coordinates1 = kmlFactory.createCoordinates(Arrays.asList(coordinate10));
    final Point point1 = kmlFactory.createPoint(coordinates1);
    placemark1.setPropertyValue(KmlConstants.TAG_GEOMETRY, point1);
    placemark1.setPropertyValue(KmlConstants.TAG_NAME, "Document Feature 2");
    placemark1.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#exampleStyleDocument"));
    Style style = kmlFactory.createStyle();
    Color color = new Color(204, 0, 0, 255);
    LabelStyle labelStyle = kmlFactory.createLabelStyle();
    labelStyle.setColor(color);
    style.setLabelStyle(labelStyle);
    IdAttributes idAttributes = kmlFactory.createIdAttributes("exampleStyleDocument", null);
    style.setIdAttributes(idAttributes);
    Feature document = kmlFactory.createDocument();
    document.setPropertyValue(KmlConstants.TAG_NAME, "Document.kml");
    document.setPropertyValue(KmlConstants.TAG_FEATURES, Arrays.asList(placemark0, placemark1));
    document.setPropertyValue(KmlConstants.TAG_OPEN, true);
    document.setPropertyValue(KmlConstants.TAG_STYLE_SELECTOR, style);
    final Kml kml = kmlFactory.createKml(null, document, null, null);
    File temp = File.createTempFile("testDocument", ".kml");
    temp.deleteOnExit();
    KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.write(kml);
    writer.dispose();
    DomCompare.compare(new File(pathToTestFile), temp);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) Color(java.awt.Color) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) Point(org.geotoolkit.data.kml.model.Point) Kml(org.geotoolkit.data.kml.model.Kml) Feature(org.opengis.feature.Feature) URI(java.net.URI) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) Coordinate(org.locationtech.jts.geom.Coordinate) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) Style(org.geotoolkit.data.kml.model.Style) File(java.io.File) Test(org.junit.Test)

Example 3 with LabelStyle

use of org.geotoolkit.data.kml.model.LabelStyle in project geotoolkit by Geomatys.

the class KmlReader method readStyle.

private Style readStyle() throws XMLStreamException, KmlException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
    IdAttributes idAttributes = readIdAttributes();
    // AbstractStyleSelector
    List<SimpleTypeContainer> styleSelectorSimpleExtensions = new ArrayList<>();
    List<Object> styleSelectorObjectExtensions = new ArrayList<>();
    // Style
    IconStyle iconStyle = null;
    LabelStyle labelStyle = null;
    LineStyle lineStyle = null;
    PolyStyle polyStyle = null;
    BalloonStyle balloonStyle = null;
    ListStyle listStyle = null;
    List<SimpleTypeContainer> styleSimpleExtensions = new ArrayList<>();
    List<Object> styleObjectExtensions = new ArrayList<>();
    boucle: while (reader.hasNext()) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                {
                    final String eName = reader.getLocalName();
                    final String eUri = reader.getNamespaceURI();
                    if (equalsNamespace(eUri)) {
                        switch(eName) {
                            case TAG_ICON_STYLE:
                                iconStyle = readIconStyle();
                                break;
                            case TAG_LABEL_STYLE:
                                labelStyle = readLabelStyle();
                                break;
                            case TAG_LINE_STYLE:
                                lineStyle = readLineStyle();
                                break;
                            case TAG_POLY_STYLE:
                                polyStyle = readPolyStyle();
                                break;
                            case TAG_BALLOON_STYLE:
                                balloonStyle = readBalloonStyle();
                                break;
                            case TAG_LIST_STYLE:
                                listStyle = readListStyle();
                                break;
                        }
                    } else // EXTENSIONS
                    {
                        KmlExtensionReader r;
                        if ((r = this.getComplexExtensionReader(TAG_STYLE, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_STYLE, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.STYLE_SELECTOR.equals(extensionLevel)) {
                                styleSelectorObjectExtensions.add(ext);
                            } else if (Extensions.Names.STYLE.equals(extensionLevel)) {
                                styleObjectExtensions.add(ext);
                            }
                        } else if ((r = getSimpleExtensionReader(TAG_STYLE, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_STYLE, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.OBJECT.equals(extensionLevel)) {
                                objectSimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.STYLE_SELECTOR.equals(extensionLevel)) {
                                styleSelectorSimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.STYLE.equals(extensionLevel)) {
                                styleSimpleExtensions.add((SimpleTypeContainer) ext);
                            }
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_STYLE.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createStyle(objectSimpleExtensions, idAttributes, styleSelectorSimpleExtensions, styleSelectorObjectExtensions, iconStyle, labelStyle, lineStyle, polyStyle, balloonStyle, listStyle, styleSimpleExtensions, styleObjectExtensions);
}
Also used : BalloonStyle(org.geotoolkit.data.kml.model.BalloonStyle) ListStyle(org.geotoolkit.data.kml.model.ListStyle) LineStyle(org.geotoolkit.data.kml.model.LineStyle) ArrayList(java.util.ArrayList) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) LineString(org.geotoolkit.data.kml.model.LineString) SimpleTypeContainer(org.geotoolkit.data.kml.xsd.SimpleTypeContainer) Extensions(org.geotoolkit.data.kml.model.Extensions) PolyStyle(org.geotoolkit.data.kml.model.PolyStyle) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) IconStyle(org.geotoolkit.data.kml.model.IconStyle) Entry(java.util.Map.Entry)

Example 4 with LabelStyle

use of org.geotoolkit.data.kml.model.LabelStyle in project geotoolkit by Geomatys.

the class StyleMapTest method styleMapReadTest.

@Test
public void styleMapReadTest() throws IOException, XMLStreamException, URISyntaxException, KmlException {
    final Feature document;
    {
        final KmlReader reader = new KmlReader();
        reader.setInput(new File(pathToTestFile));
        final Kml kmlObjects = reader.read();
        reader.dispose();
        document = kmlObjects.getAbstractFeature();
    }
    assertEquals(KmlModelConstants.TYPE_DOCUMENT, document.getType());
    assertEquals("StyleMap.kml", document.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals(Boolean.TRUE, document.getPropertyValue(KmlConstants.TAG_OPEN));
    Iterator<?> i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_STYLE_SELECTOR)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    {
        Style style = (Style) i.next();
        assertEquals("normalState", style.getIdAttributes().getId());
        IconStyle iconStyle0 = style.getIconStyle();
        BasicLink icon0 = iconStyle0.getIcon();
        assertEquals("http://maps.google.com/mapfiles/kml/pal3/icon55.png", icon0.getHref());
    }
    assertTrue("Expected at least 2 elements.", i.hasNext());
    {
        Style style = (Style) i.next();
        assertEquals("highlightState", style.getIdAttributes().getId());
        IconStyle iconStyle1 = style.getIconStyle();
        assertEquals(1.1, iconStyle1.getScale(), DELTA);
        BasicLink icon1 = iconStyle1.getIcon();
        assertEquals("http://maps.google.com/mapfiles/kml/pal3/icon60.png", icon1.getHref());
        LabelStyle labelStyle1 = style.getLabelStyle();
        assertEquals(new Color(192, 0, 0, 255), labelStyle1.getColor());
        assertEquals(1.1, labelStyle1.getScale(), DELTA);
    }
    assertTrue("Expected at least 3 elements.", i.hasNext());
    {
        StyleMap styleMap = (StyleMap) i.next();
        assertEquals("styleMapExample", styleMap.getIdAttributes().getId());
        assertEquals(2, styleMap.getPairs().size());
        Pair pair0 = styleMap.getPairs().get(0);
        assertEquals(new URI("#normalState"), pair0.getStyleUrl());
        Pair pair1 = styleMap.getPairs().get(1);
        assertEquals(StyleState.HIGHLIGHT, pair1.getKey());
        assertEquals(new URI("#highlightState"), pair1.getStyleUrl());
    }
    assertFalse("Expected exactly 3 elements.", i.hasNext());
    i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_FEATURES)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    {
        Feature placemark = (Feature) i.next();
        assertEquals(KmlModelConstants.TYPE_PLACEMARK, placemark.getType());
        assertEquals("StyleMap example", placemark.getPropertyValue(KmlConstants.TAG_NAME));
        assertEquals(new URI("#styleMapExample"), placemark.getPropertyValue(KmlConstants.TAG_STYLE_URL));
        Point point = (Point) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
        CoordinateSequence coordinates = point.getCoordinateSequence();
        assertEquals(1, coordinates.size());
        Coordinate coordinate = coordinates.getCoordinate(0);
        assertEquals(-122.368987, coordinate.x, DELTA);
        assertEquals(37.817634, coordinate.y, DELTA);
        assertEquals(0, coordinate.z, DELTA);
    }
    assertFalse("Expected exactly one element.", i.hasNext());
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) StyleMap(org.geotoolkit.data.kml.model.StyleMap) Color(java.awt.Color) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) Kml(org.geotoolkit.data.kml.model.Kml) Point(org.geotoolkit.data.kml.model.Point) Feature(org.opengis.feature.Feature) URI(java.net.URI) BasicLink(org.geotoolkit.data.kml.model.BasicLink) IconStyle(org.geotoolkit.data.kml.model.IconStyle) Coordinate(org.locationtech.jts.geom.Coordinate) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) IconStyle(org.geotoolkit.data.kml.model.IconStyle) Style(org.geotoolkit.data.kml.model.Style) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) File(java.io.File) Pair(org.geotoolkit.data.kml.model.Pair) Test(org.junit.Test)

Example 5 with LabelStyle

use of org.geotoolkit.data.kml.model.LabelStyle in project geotoolkit by Geomatys.

the class StyleTest method styleWriteTest.

@Test
public void styleWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Feature placemark0 = kmlFactory.createPlacemark();
    placemark0.setPropertyValue(KmlConstants.TAG_NAME, "Google Earth - New Polygon");
    placemark0.setPropertyValue(KmlConstants.TAG_DESCRIPTION, "Here is some descriptive text");
    placemark0.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#myDefaultStyles"));
    final Feature placemark1 = kmlFactory.createPlacemark();
    placemark1.setPropertyValue(KmlConstants.TAG_NAME, "Google Earth - New Path");
    placemark1.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#myDefaultStyles"));
    final IconStyle iconStyle = kmlFactory.createIconStyle();
    final BasicLink icon = kmlFactory.createBasicLink();
    icon.setHref("http://myserver.com/icon.jpg");
    iconStyle.setIcon(icon);
    iconStyle.setColor(new Color(255, 0, 255, 161));
    iconStyle.setScale(1.399999976158142);
    final LabelStyle labelStyle = kmlFactory.createLabelStyle();
    labelStyle.setColor(new Color(255, 170, 255, 127));
    labelStyle.setScale(1.5);
    final LineStyle lineStyle = kmlFactory.createLineStyle();
    lineStyle.setColor(new Color(255, 0, 0, 255));
    lineStyle.setWidth(15);
    final PolyStyle polyStyle = kmlFactory.createPolyStyle();
    polyStyle.setColor(new Color(170, 170, 127, 127));
    polyStyle.setColorMode(ColorMode.RANDOM);
    final IdAttributes idAttributes = kmlFactory.createIdAttributes("myDefaultStyles", null);
    final Style style = kmlFactory.createStyle();
    style.setIdAttributes(idAttributes);
    style.setIconStyle(iconStyle);
    style.setLabelStyle(labelStyle);
    style.setLineStyle(lineStyle);
    style.setPolyStyle(polyStyle);
    final Feature document = kmlFactory.createDocument();
    document.setPropertyValue(KmlConstants.TAG_STYLE_SELECTOR, style);
    document.setPropertyValue(KmlConstants.TAG_FEATURES, Arrays.asList(placemark0, placemark1));
    final Kml kml = kmlFactory.createKml(null, document, null, null);
    final File temp = File.createTempFile("testStyle", ".kml");
    temp.deleteOnExit();
    final KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.write(kml);
    writer.dispose();
    DomCompare.compare(new File(pathToTestFile), temp);
}
Also used : LineStyle(org.geotoolkit.data.kml.model.LineStyle) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) Color(java.awt.Color) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) Kml(org.geotoolkit.data.kml.model.Kml) Feature(org.opengis.feature.Feature) URI(java.net.URI) PolyStyle(org.geotoolkit.data.kml.model.PolyStyle) BasicLink(org.geotoolkit.data.kml.model.BasicLink) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) IconStyle(org.geotoolkit.data.kml.model.IconStyle) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) LineStyle(org.geotoolkit.data.kml.model.LineStyle) IconStyle(org.geotoolkit.data.kml.model.IconStyle) Style(org.geotoolkit.data.kml.model.Style) PolyStyle(org.geotoolkit.data.kml.model.PolyStyle) File(java.io.File) Test(org.junit.Test)

Aggregations

LabelStyle (org.geotoolkit.data.kml.model.LabelStyle)8 Color (java.awt.Color)7 File (java.io.File)6 URI (java.net.URI)6 Kml (org.geotoolkit.data.kml.model.Kml)6 Style (org.geotoolkit.data.kml.model.Style)6 Test (org.junit.Test)6 Feature (org.opengis.feature.Feature)6 IconStyle (org.geotoolkit.data.kml.model.IconStyle)4 IdAttributes (org.geotoolkit.data.kml.model.IdAttributes)4 LineStyle (org.geotoolkit.data.kml.model.LineStyle)4 Point (org.geotoolkit.data.kml.model.Point)4 PolyStyle (org.geotoolkit.data.kml.model.PolyStyle)4 Coordinate (org.locationtech.jts.geom.Coordinate)4 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)4 BasicLink (org.geotoolkit.data.kml.model.BasicLink)3 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)3 KmlWriter (org.geotoolkit.data.kml.xml.KmlWriter)3 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1