Search in sources :

Example 1 with IconStyle

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

the class AnimatedUpdateSimpleTest method animatedUpdateReadSimpleTest.

@Test
public void animatedUpdateReadSimpleTest() throws IOException, XMLStreamException, URISyntaxException, KmlException {
    final Feature document;
    {
        final KmlReader reader = new KmlReader();
        final GxReader gxReader = new GxReader(reader);
        reader.setInput(new File(pathToTestFile));
        reader.addExtensionReader(gxReader);
        final Kml kmlObjects = reader.read();
        reader.dispose();
        document = kmlObjects.getAbstractFeature();
    }
    assertEquals(KmlModelConstants.TYPE_DOCUMENT, document.getType());
    assertEquals("gx:AnimatedUpdate example", document.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals(Boolean.TRUE, document.getPropertyValue(KmlConstants.TAG_OPEN));
    final Feature tour = (Feature) document.getProperty(KmlConstants.TAG_FEATURES).getValue();
    assertEquals(GxModelConstants.TYPE_TOUR, tour.getType());
    assertEquals("Play me!", tour.getPropertyValue(KmlConstants.TAG_NAME));
    Iterator<?> i = ((Iterable<?>) tour.getPropertyValue(KmlConstants.ATT_PLAYLIST)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    final PlayList playList = (PlayList) i.next();
    assertEquals(1, playList.getTourPrimitives().size());
    final AnimatedUpdate animatedUpdate = (AnimatedUpdate) playList.getTourPrimitives().get(0);
    assertEquals(6.5, animatedUpdate.getDuration(), DELTA);
    final Update update = animatedUpdate.getUpdate();
    assertEquals(new URI("http://moncoco.com"), update.getTargetHref());
    assertEquals(1, update.getUpdates().size());
    final Change change = (Change) update.getUpdates().get(0);
    assertEquals(1, change.getObjects().size());
    final IconStyle iconStyle = (IconStyle) change.getObjects().get(0);
    assertEquals("iconstyle1", iconStyle.getIdAttributes().getId());
    assertEquals(10.0, iconStyle.getScale(), DELTA);
    assertFalse("Expected exactly one element.", i.hasNext());
}
Also used : PlayList(org.geotoolkit.data.gx.model.PlayList) Kml(org.geotoolkit.data.kml.model.Kml) Change(org.geotoolkit.data.kml.model.Change) AnimatedUpdate(org.geotoolkit.data.gx.model.AnimatedUpdate) Update(org.geotoolkit.data.kml.model.Update) Feature(org.opengis.feature.Feature) URI(java.net.URI) IconStyle(org.geotoolkit.data.kml.model.IconStyle) AnimatedUpdate(org.geotoolkit.data.gx.model.AnimatedUpdate) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) GxReader(org.geotoolkit.data.gx.xml.GxReader) File(java.io.File) Test(org.junit.Test)

Example 2 with IconStyle

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

the class AnimatedUpdateSimpleTest method animatedUpdateWriteSimpleTest.

@Test
public void animatedUpdateWriteSimpleTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final GxFactory gxFactory = DefaultGxFactory.getInstance();
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final IdAttributes styleIdAttAtributes = kmlFactory.createIdAttributes("iconstyle1", null);
    final IconStyle iconStyle = kmlFactory.createIconStyle();
    iconStyle.setIdAttributes(styleIdAttAtributes);
    iconStyle.setScale(10.0);
    final Change change = kmlFactory.createChange();
    change.setObjects(Arrays.asList((Object) iconStyle));
    final Update update = kmlFactory.createUpdate();
    update.setTargetHref(new URI("http://moncoco.com"));
    update.setUpdates(Arrays.asList((Object) change));
    final AnimatedUpdate animatedUpdate = gxFactory.createAnimatedUpdate();
    animatedUpdate.setDuration(6.5);
    animatedUpdate.setUpdate(update);
    final PlayList playList = gxFactory.createPlayList();
    playList.setTourPrimitives(Arrays.asList((AbstractTourPrimitive) animatedUpdate));
    final Feature tour = gxFactory.createTour();
    tour.setPropertyValue(KmlConstants.TAG_NAME, "Play me!");
    tour.setPropertyValue(KmlConstants.ATT_PLAYLIST, playList);
    final Feature document = kmlFactory.createDocument();
    document.setPropertyValue(KmlConstants.TAG_NAME, "gx:AnimatedUpdate example");
    document.setPropertyValue(KmlConstants.TAG_OPEN, Boolean.TRUE);
    document.setPropertyValue(KmlConstants.TAG_FEATURES, tour);
    final Kml kml = kmlFactory.createKml(null, document, null, null);
    kml.addExtensionUri(GxConstants.URI_GX, "gx");
    final File temp = File.createTempFile("testAnimatedUpdate", ".kml");
    temp.deleteOnExit();
    final KmlWriter writer = new KmlWriter();
    final GxWriter gxWriter = new GxWriter(writer);
    writer.setOutput(temp);
    writer.addExtensionWriter(GxConstants.URI_GX, gxWriter);
    writer.write(kml);
    writer.dispose();
    DomCompare.compare(new File(pathToTestFile), temp);
}
Also used : PlayList(org.geotoolkit.data.gx.model.PlayList) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) GxWriter(org.geotoolkit.data.gx.xml.GxWriter) KmlFactory(org.geotoolkit.data.kml.KmlFactory) DefaultKmlFactory(org.geotoolkit.data.kml.DefaultKmlFactory) Change(org.geotoolkit.data.kml.model.Change) AbstractTourPrimitive(org.geotoolkit.data.gx.model.AbstractTourPrimitive) Kml(org.geotoolkit.data.kml.model.Kml) AnimatedUpdate(org.geotoolkit.data.gx.model.AnimatedUpdate) Update(org.geotoolkit.data.kml.model.Update) URI(java.net.URI) Feature(org.opengis.feature.Feature) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) IconStyle(org.geotoolkit.data.kml.model.IconStyle) AnimatedUpdate(org.geotoolkit.data.gx.model.AnimatedUpdate) File(java.io.File) Test(org.junit.Test)

Example 3 with IconStyle

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

the class AnimatedUpdateTest method animatedUpdateReadTest.

@Test
public void animatedUpdateReadTest() throws IOException, XMLStreamException, URISyntaxException, KmlException {
    final Feature document;
    {
        final KmlReader reader = new KmlReader();
        final GxReader gxReader = new GxReader(reader);
        reader.setInput(new File(pathToTestFile));
        reader.addExtensionReader(gxReader);
        final Kml kmlObjects = reader.read();
        reader.dispose();
        document = kmlObjects.getAbstractFeature();
    }
    assertEquals(KmlModelConstants.TYPE_DOCUMENT, document.getType());
    assertEquals("gx:AnimatedUpdate example", document.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals(Boolean.FALSE, document.getPropertyValue(KmlConstants.TAG_OPEN));
    Iterator<?> i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_STYLE_SELECTOR)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    {
        final Style style = (Style) i.next();
        assertEquals("pushpin", style.getIdAttributes().getId());
        assertEquals("mystyle", style.getIconStyle().getIdAttributes().getId());
        assertEquals("http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png", style.getIconStyle().getIcon().getHref());
        assertEquals(2.0, style.getIconStyle().getScale(), DELTA);
    }
    assertFalse("Expected exactly one element.", i.hasNext());
    i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_FEATURES)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    {
        final Feature placemark = (Feature) i.next();
        assertEquals(placemark.getType(), KmlModelConstants.TYPE_PLACEMARK);
        assertEquals("mountainpin1", ((IdAttributes) placemark.getPropertyValue(KmlConstants.ATT_ID)).getId());
        assertEquals("Pin on a mountaintop", placemark.getPropertyValue(KmlConstants.TAG_NAME));
        assertEquals(new URI("#pushpin"), 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(170.1435558771009, coordinate.x, DELTA);
        assertEquals(-43.60505741890396, coordinate.y, DELTA);
        assertEquals(0, coordinate.z, DELTA);
    }
    assertTrue("Expected at least 2 elements.", i.hasNext());
    final Feature tour = (Feature) i.next();
    assertEquals(GxModelConstants.TYPE_TOUR, tour.getType());
    assertEquals("Play me!", tour.getPropertyValue(KmlConstants.TAG_NAME));
    assertFalse("Expected exactly 2 elements.", i.hasNext());
    i = ((Iterable<?>) tour.getPropertyValue(KmlConstants.ATT_PLAYLIST)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    {
        final PlayList playList = (PlayList) i.next();
        assertEquals(3, playList.getTourPrimitives().size());
        final FlyTo flyTo = (FlyTo) playList.getTourPrimitives().get(0);
        assertEquals(3, flyTo.getDuration(), DELTA);
        assertEquals(EnumFlyToMode.SMOOTH, flyTo.getFlyToMode());
        final Camera camera = (Camera) flyTo.getView();
        assertEquals(170.157, camera.getLongitude(), DELTA);
        assertEquals(-43.671, camera.getLatitude(), DELTA);
        assertEquals(9700, camera.getAltitude(), DELTA);
        assertEquals(-6.333, camera.getHeading(), DELTA);
        assertEquals(33.5, camera.getTilt(), DELTA);
        final AnimatedUpdate animatedUpdate = (AnimatedUpdate) playList.getTourPrimitives().get(1);
        assertEquals(5, animatedUpdate.getDuration(), DELTA);
        final Update update = animatedUpdate.getUpdate();
        assertEquals(new URI("http://moncoco.com"), update.getTargetHref());
        assertEquals(1, update.getUpdates().size());
        final Change change = (Change) update.getUpdates().get(0);
        assertEquals(1, change.getObjects().size());
        final IconStyle iconStyle = (IconStyle) change.getObjects().get(0);
        assertEquals("mystyle", iconStyle.getIdAttributes().getTargetId());
        assertEquals(10.0, iconStyle.getScale(), DELTA);
        final Wait wait = (Wait) playList.getTourPrimitives().get(2);
        assertEquals(5, wait.getDuration(), DELTA);
    }
    assertFalse("Expected exactly one element.", i.hasNext());
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) PlayList(org.geotoolkit.data.gx.model.PlayList) FlyTo(org.geotoolkit.data.gx.model.FlyTo) Kml(org.geotoolkit.data.kml.model.Kml) Point(org.geotoolkit.data.kml.model.Point) Change(org.geotoolkit.data.kml.model.Change) AnimatedUpdate(org.geotoolkit.data.gx.model.AnimatedUpdate) Update(org.geotoolkit.data.kml.model.Update) Feature(org.opengis.feature.Feature) URI(java.net.URI) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) IconStyle(org.geotoolkit.data.kml.model.IconStyle) Coordinate(org.locationtech.jts.geom.Coordinate) AnimatedUpdate(org.geotoolkit.data.gx.model.AnimatedUpdate) IconStyle(org.geotoolkit.data.kml.model.IconStyle) Style(org.geotoolkit.data.kml.model.Style) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) Camera(org.geotoolkit.data.kml.model.Camera) Wait(org.geotoolkit.data.gx.model.Wait) GxReader(org.geotoolkit.data.gx.xml.GxReader) File(java.io.File) Test(org.junit.Test)

Example 4 with IconStyle

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

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

Aggregations

IconStyle (org.geotoolkit.data.kml.model.IconStyle)11 File (java.io.File)9 URI (java.net.URI)9 Kml (org.geotoolkit.data.kml.model.Kml)9 Test (org.junit.Test)9 Feature (org.opengis.feature.Feature)9 Style (org.geotoolkit.data.kml.model.Style)8 BasicLink (org.geotoolkit.data.kml.model.BasicLink)7 IdAttributes (org.geotoolkit.data.kml.model.IdAttributes)6 Point (org.geotoolkit.data.kml.model.Point)6 Color (java.awt.Color)5 LabelStyle (org.geotoolkit.data.kml.model.LabelStyle)5 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)5 AnimatedUpdate (org.geotoolkit.data.gx.model.AnimatedUpdate)4 PlayList (org.geotoolkit.data.gx.model.PlayList)4 Change (org.geotoolkit.data.kml.model.Change)4 LineStyle (org.geotoolkit.data.kml.model.LineStyle)4 PolyStyle (org.geotoolkit.data.kml.model.PolyStyle)4