Search in sources :

Example 1 with Style

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

the class KmzContextInterpreter method writeRule.

/**
 * Retrieves a KML StyleSelector element mapping SLD Rule.
 */
private AbstractStyleSelector writeRule(Rule rule) throws URISyntaxException {
    final Style styleSelector = KML_FACTORY.createStyle();
    final List<? extends Symbolizer> symbolizers = rule.symbolizers();
    for (int i = 0, num = symbolizers.size(); i < num; i++) {
        this.writeSymbolizer(symbolizers.get(i), styleSelector);
    }
    // Links rule filter with Style URI
    final String id = this.getIdentificator();
    styleSelector.setIdAttributes(KML_FACTORY.createIdAttributes(id, null));
    IDENTIFICATORS_MAP.add(new SimpleEntry<Rule, URI>(rule, new URI("#" + id)));
    return styleSelector;
}
Also used : LineStyle(org.geotoolkit.data.kml.model.LineStyle) Style(org.geotoolkit.data.kml.model.Style) PolyStyle(org.geotoolkit.data.kml.model.PolyStyle) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) FeatureTypeStyle(org.opengis.style.FeatureTypeStyle) LineString(org.locationtech.jts.geom.LineString) Rule(org.opengis.style.Rule) URI(java.net.URI)

Example 2 with Style

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

the class BalloonStyleTest method balloonStyleWriteTest.

@Test
public void balloonStyleWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Coordinate coordinate = kmlFactory.createCoordinate(-122.370533, 37.823842, 0.0);
    final CoordinateSequence coordinates = kmlFactory.createCoordinates(Arrays.asList(coordinate));
    final Point point = kmlFactory.createPoint(coordinates);
    final Feature placemark = kmlFactory.createPlacemark();
    placemark.setPropertyValue(KmlConstants.TAG_NAME, "BalloonStyle");
    placemark.setPropertyValue(KmlConstants.TAG_DESCRIPTION, "An example of BalloonStyle");
    placemark.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#exampleBalloonStyle"));
    placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, point);
    final BalloonStyle balloonStyle = kmlFactory.createBalloonStyle();
    final Cdata text = new DefaultCdata("\n      <b><font color=\"#CC0000\" size=\"+3\">$[name]</font></b>\n" + "      <br/><br/>\n" + "      <font face=\"Courier\">$[description]</font>\n" + "      <br/><br/>\n" + "      Extra text that will appear in the description balloon\n" + "      <br/><br/>\n" + "      $[geDirections]\n" + "      ");
    balloonStyle.setText(text);
    balloonStyle.setBgColor(new Color(187, 255, 255, 255));
    final IdAttributes idAttributes = kmlFactory.createIdAttributes("exampleBalloonStyle", null);
    final Style style = kmlFactory.createStyle();
    style.setIdAttributes(idAttributes);
    style.setBalloonStyle(balloonStyle);
    final Feature document = kmlFactory.createDocument();
    document.setPropertyValue(KmlConstants.TAG_NAME, "BalloonStyle.kml");
    document.setPropertyValue(KmlConstants.TAG_OPEN, Boolean.TRUE);
    document.setPropertyValue(KmlConstants.TAG_STYLE_SELECTOR, style);
    document.setPropertyValue(KmlConstants.TAG_FEATURES, placemark);
    final Kml kml = kmlFactory.createKml(null, document, null, null);
    final File temp = File.createTempFile("testBalloonStyle", ".kml");
    temp.deleteOnExit();
    final 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) BalloonStyle(org.geotoolkit.data.kml.model.BalloonStyle) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) Color(java.awt.Color) 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) Cdata(org.geotoolkit.data.kml.xsd.Cdata) DefaultCdata(org.geotoolkit.data.kml.xsd.DefaultCdata) Coordinate(org.locationtech.jts.geom.Coordinate) BalloonStyle(org.geotoolkit.data.kml.model.BalloonStyle) Style(org.geotoolkit.data.kml.model.Style) DefaultCdata(org.geotoolkit.data.kml.xsd.DefaultCdata) File(java.io.File) Test(org.junit.Test)

Example 3 with Style

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

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

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

Aggregations

Style (org.geotoolkit.data.kml.model.Style)22 URI (java.net.URI)20 File (java.io.File)19 Kml (org.geotoolkit.data.kml.model.Kml)19 Test (org.junit.Test)19 Feature (org.opengis.feature.Feature)19 Color (java.awt.Color)17 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)17 Coordinate (org.locationtech.jts.geom.Coordinate)16 Point (org.geotoolkit.data.kml.model.Point)14 IdAttributes (org.geotoolkit.data.kml.model.IdAttributes)10 LabelStyle (org.geotoolkit.data.kml.model.LabelStyle)10 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)10 IconStyle (org.geotoolkit.data.kml.model.IconStyle)9 KmlWriter (org.geotoolkit.data.kml.xml.KmlWriter)9 BasicLink (org.geotoolkit.data.kml.model.BasicLink)7 LineStyle (org.geotoolkit.data.kml.model.LineStyle)7 PolyStyle (org.geotoolkit.data.kml.model.PolyStyle)7 BalloonStyle (org.geotoolkit.data.kml.model.BalloonStyle)4 LineString (org.geotoolkit.data.kml.model.LineString)4