Search in sources :

Example 1 with BasicLink

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

the class KMLGraphicBuilder method legendPlacemark.

private static Image legendPlacemark(Feature placemark, KmlCache cache) throws IOException {
    int nameWidth = 0;
    legendCommonFeature(placemark, cache);
    final String featureName = (String) placemark.getPropertyValue(KmlConstants.TAG_NAME);
    if (featureName != null) {
        nameWidth = FONT_METRICS.stringWidth(featureName);
    }
    // Apply styles
    final Style s = retrieveStyle(placemark, cache);
    final BufferedImage image = new BufferedImage(LEGEND_WIDTH_EXT + nameWidth, LEGEND_HEIGHT_EXT, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D graphic = (Graphics2D) image.getGraphics();
    graphic.setFont(FONT);
    final AbstractGeometry geometry = (AbstractGeometry) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
    if (s != null && s.getIconStyle() != null) {
        final IconStyle iconStyle = s.getIconStyle();
        final BasicLink bl = iconStyle.getIcon();
        if (bl != null) {
            if (bl.getHref() != null) {
                final URL img = new URL(bl.getHref());
                final BufferedImage buff = ImageIO.read(img);
                graphic.drawImage(buff, 0, 4, LEGEND_WIDTH_INT, LEGEND_HEIGHT_INT, null);
            }
        }
    } else if (geometry instanceof LineString) {
        graphic.drawImage(ICON_PLACEMARK_LINE_STRING, 0, 4, null);
    } else if (geometry instanceof LinearRing) {
        graphic.drawImage(ICON_PLACEMARK_LINEAR_RING, 0, 4, null);
    } else if (geometry instanceof Polygon) {
        graphic.drawImage(ICON_PLACEMARK_POLYGON, 0, 4, null);
    } else {
        graphic.drawImage(ICON_PLACEMARK, 0, 4, null);
    }
    if (featureName != null) {
        graphic.setColor(Color.BLACK);
        graphic.drawString(featureName, LEGEND_WIDTH_EXT, LEGEND_HEIGHT_INT);
    }
    return image;
}
Also used : IconStyle(org.geotoolkit.data.kml.model.IconStyle) AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) LineString(org.geotoolkit.data.kml.model.LineString) LineStyle(org.geotoolkit.data.kml.model.LineStyle) IconStyle(org.geotoolkit.data.kml.model.IconStyle) BalloonStyle(org.geotoolkit.data.kml.model.BalloonStyle) Style(org.geotoolkit.data.kml.model.Style) PolyStyle(org.geotoolkit.data.kml.model.PolyStyle) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) LineString(org.geotoolkit.data.kml.model.LineString) LinearRing(org.geotoolkit.data.kml.model.LinearRing) Polygon(org.geotoolkit.data.kml.model.Polygon) Point(org.geotoolkit.data.kml.model.Point) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) Graphics2D(java.awt.Graphics2D) BasicLink(org.geotoolkit.data.kml.model.BasicLink)

Example 2 with BasicLink

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

the class KmlReader method readIconStyle.

private IconStyle readIconStyle() throws XMLStreamException, KmlException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
    IdAttributes idAttributes = readIdAttributes();
    // AbstractSubStyle
    List<SimpleTypeContainer> subStyleSimpleExtensions = new ArrayList<>();
    List<Object> subStyleObjectExtensions = new ArrayList<>();
    // AbstractColorStyle
    Color color = DEF_COLOR;
    ColorMode colorMode = DEF_COLOR_MODE;
    List<SimpleTypeContainer> colorStyleSimpleExtensions = new ArrayList<>();
    List<Object> colorStyleObjectExtensions = new ArrayList<>();
    // IconStyle
    double scale = DEF_SCALE;
    double heading = DEF_HEADING;
    BasicLink icon = null;
    Vec2 hotSpot = null;
    List<SimpleTypeContainer> iconStyleSimpleExtensions = new ArrayList<>();
    List<Object> iconStyleObjectExtensions = 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_COLOR:
                                color = KmlUtilities.parseColor(reader.getElementText());
                                break;
                            case TAG_COLOR_MODE:
                                colorMode = ColorMode.transform(reader.getElementText());
                                break;
                            case TAG_SCALE:
                                scale = parseDouble(reader.getElementText());
                                break;
                            case TAG_HEADING:
                                heading = parseDouble(reader.getElementText());
                                break;
                            case TAG_ICON:
                                icon = readBasicLink(TAG_ICON);
                                break;
                            case TAG_HOT_SPOT:
                                hotSpot = readVec2(TAG_HOT_SPOT);
                                break;
                        }
                    } else // EXTENSIONS
                    {
                        KmlExtensionReader r;
                        if ((r = this.getComplexExtensionReader(TAG_ICON_STYLE, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_ICON_STYLE, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.SUB_STYLE.equals(extensionLevel)) {
                                subStyleObjectExtensions.add(ext);
                            } else if (Extensions.Names.COLOR_STYLE.equals(extensionLevel)) {
                                colorStyleObjectExtensions.add(ext);
                            } else if (Extensions.Names.ICON_STYLE.equals(extensionLevel)) {
                                iconStyleObjectExtensions.add(ext);
                            }
                        } else if ((r = getSimpleExtensionReader(TAG_ICON_STYLE, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_ICON_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.SUB_STYLE.equals(extensionLevel)) {
                                subStyleSimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.COLOR_STYLE.equals(extensionLevel)) {
                                colorStyleSimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.ICON_STYLE.equals(extensionLevel)) {
                                iconStyleSimpleExtensions.add((SimpleTypeContainer) ext);
                            }
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_ICON_STYLE.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createIconStyle(objectSimpleExtensions, idAttributes, subStyleSimpleExtensions, subStyleObjectExtensions, color, colorMode, colorStyleSimpleExtensions, colorStyleObjectExtensions, scale, heading, icon, hotSpot, iconStyleSimpleExtensions, iconStyleObjectExtensions);
}
Also used : Color(java.awt.Color) ColorMode(org.geotoolkit.data.kml.model.ColorMode) ArrayList(java.util.ArrayList) LineString(org.geotoolkit.data.kml.model.LineString) SimpleTypeContainer(org.geotoolkit.data.kml.xsd.SimpleTypeContainer) Extensions(org.geotoolkit.data.kml.model.Extensions) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) BasicLink(org.geotoolkit.data.kml.model.BasicLink) Entry(java.util.Map.Entry) Vec2(org.geotoolkit.data.kml.model.Vec2)

Example 3 with BasicLink

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

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

Example 5 with BasicLink

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

the class IconStyleTest method iconStyleReadTest.

@Test
public void iconStyleReadTest() throws IOException, XMLStreamException, URISyntaxException, KmlException {
    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();
    final Feature document = kmlObjects.getAbstractFeature();
    assertEquals(KmlModelConstants.TYPE_DOCUMENT, document.getType());
    Iterator<?> i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_STYLE_SELECTOR)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    Style style = (Style) i.next();
    assertEquals("randomColorIcon", style.getIdAttributes().getId());
    IconStyle iconStyle = style.getIconStyle();
    assertEquals(new Color(0, 255, 0, 255), iconStyle.getColor());
    assertEquals(ColorMode.RANDOM, iconStyle.getColorMode());
    assertEquals(1.1, iconStyle.getScale(), DELTA);
    BasicLink icon = iconStyle.getIcon();
    assertEquals("http://maps.google.com/mapfiles/kml/pal3/icon21.png", icon.getHref());
    assertFalse("Expected exactly one element.", i.hasNext());
    i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_FEATURES)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    Feature placemark = (Feature) i.next();
    assertEquals("IconStyle.kml", placemark.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals(new URI("#randomColorIcon"), 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.36868, coordinate.x, DELTA);
    assertEquals(37.831145, coordinate.y, DELTA);
    assertEquals(0, coordinate.z, DELTA);
    assertFalse("Expected exactly one element.", i.hasNext());
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) Color(java.awt.Color) 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) 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) Test(org.junit.Test)

Aggregations

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