Search in sources :

Example 1 with LineString

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

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

the class KmlReader method readLineString.

private LineString readLineString() throws XMLStreamException, KmlException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
    IdAttributes idAttributes = readIdAttributes();
    // AbstractGeometry
    List<SimpleTypeContainer> abstractGeometrySimpleExtensions = new ArrayList<>();
    List<Object> abstractGeometryObjectExtensions = new ArrayList<>();
    // LineString
    boolean extrude = DEF_EXTRUDE;
    boolean tessellate = DEF_TESSELLATE;
    AltitudeMode altitudeMode = DEF_ALTITUDE_MODE;
    CoordinateSequence coordinates = null;
    List<SimpleTypeContainer> lineStringSimpleExtensions = new ArrayList<>();
    List<Object> lineStringObjectExtensions = new ArrayList<>();
    boucle: while (reader.hasNext()) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                {
                    final String eName = reader.getLocalName();
                    final String eUri = reader.getNamespaceURI();
                    // LINE STRING
                    if (equalsNamespace(eUri)) {
                        switch(eName) {
                            case TAG_EXTRUDE:
                                extrude = parseBoolean(reader.getElementText());
                                break;
                            case TAG_TESSELLATE:
                                tessellate = parseBoolean(reader.getElementText());
                                break;
                            case TAG_ALTITUDE_MODE:
                                altitudeMode = readAltitudeMode();
                                break;
                            case TAG_COORDINATES:
                                coordinates = readCoordinates(reader.getElementText());
                                break;
                        }
                    } else // EXTENSIONS
                    {
                        KmlExtensionReader r;
                        if ((r = this.getComplexExtensionReader(TAG_LINE_STRING, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_LINE_STRING, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.GEOMETRY.equals(extensionLevel)) {
                                abstractGeometryObjectExtensions.add(ext);
                            } else if (Extensions.Names.LINE_STRING.equals(extensionLevel)) {
                                lineStringObjectExtensions.add(ext);
                            } else if (extensionLevel == null) {
                                if (ext instanceof AltitudeMode) {
                                    altitudeMode = (AltitudeMode) ext;
                                }
                            }
                        } else if ((r = getSimpleExtensionReader(TAG_LINE_STRING, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_LINE_STRING, 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.GEOMETRY.equals(extensionLevel)) {
                                abstractGeometrySimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.LINE_STRING.equals(extensionLevel)) {
                                lineStringSimpleExtensions.add((SimpleTypeContainer) ext);
                            }
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_LINE_STRING.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createLineString(objectSimpleExtensions, idAttributes, abstractGeometrySimpleExtensions, abstractGeometryObjectExtensions, extrude, tessellate, altitudeMode, coordinates, lineStringSimpleExtensions, lineStringObjectExtensions);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) ArrayList(java.util.ArrayList) LineString(org.geotoolkit.data.kml.model.LineString) SimpleTypeContainer(org.geotoolkit.data.kml.xsd.SimpleTypeContainer) EnumAltitudeMode(org.geotoolkit.data.kml.model.EnumAltitudeMode) AltitudeMode(org.geotoolkit.data.kml.model.AltitudeMode) Extensions(org.geotoolkit.data.kml.model.Extensions) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) Entry(java.util.Map.Entry)

Example 3 with LineString

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

the class MultiGeometryTest method multiGeometryReadTest.

@Test
public void multiGeometryReadTest() throws IOException, XMLStreamException, KmlException, URISyntaxException {
    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();
    final Feature placemark = kmlObjects.getAbstractFeature();
    assertEquals("SF Marina Harbor Master", placemark.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals(Boolean.FALSE, placemark.getPropertyValue(KmlConstants.TAG_VISIBILITY));
    final MultiGeometry multiGeometry = (MultiGeometry) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
    assertEquals(2, multiGeometry.getGeometries().size());
    LineString lineString0 = (LineString) multiGeometry.getGeometries().get(0);
    LineString lineString1 = (LineString) multiGeometry.getGeometries().get(1);
    final CoordinateSequence coordinates0 = lineString0.getCoordinateSequence();
    final CoordinateSequence coordinates1 = lineString1.getCoordinateSequence();
    assertEquals(2, coordinates0.size());
    assertEquals(2, coordinates1.size());
    final Coordinate coordinate00 = coordinates0.getCoordinate(0);
    final Coordinate coordinate01 = coordinates0.getCoordinate(1);
    final Coordinate coordinate10 = coordinates1.getCoordinate(0);
    final Coordinate coordinate11 = coordinates1.getCoordinate(1);
    assertEquals(-122.4425587930444, coordinate00.x, DELTA);
    assertEquals(37.80666418607323, coordinate00.y, DELTA);
    assertEquals(0, coordinate00.z, DELTA);
    assertEquals(-122.4428379594768, coordinate01.x, DELTA);
    assertEquals(37.80663578323093, coordinate01.y, DELTA);
    assertEquals(0, coordinate01.z, DELTA);
    assertEquals(-122.4425509770566, coordinate10.x, DELTA);
    assertEquals(37.80662588061205, coordinate10.y, DELTA);
    assertEquals(0, coordinate10.z, DELTA);
    assertEquals(-122.4428340530617, coordinate11.x, DELTA);
    assertEquals(37.8065999493009, coordinate11.y, DELTA);
    assertEquals(0, coordinate11.z, DELTA);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) LineString(org.geotoolkit.data.kml.model.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) Kml(org.geotoolkit.data.kml.model.Kml) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) File(java.io.File) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 4 with LineString

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

the class LineStyleTest method lineStyleWriteTest.

@Test
public void lineStyleWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Coordinate coordinate0 = kmlFactory.createCoordinate(-122.364383, 37.824664, 0);
    final Coordinate coordinate1 = kmlFactory.createCoordinate(-122.364152, 37.824322, 0);
    final CoordinateSequence coordinates = kmlFactory.createCoordinates(Arrays.asList(coordinate0, coordinate1));
    final LineString lineString = kmlFactory.createLineString(coordinates);
    lineString.setTessellate(true);
    lineString.setExtrude(true);
    final Feature placemark = kmlFactory.createPlacemark();
    placemark.setPropertyValue(KmlConstants.TAG_NAME, "LineStyle Example");
    placemark.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#linestyleExample"));
    placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, lineString);
    final Style style = kmlFactory.createStyle();
    final LineStyle lineStyle = kmlFactory.createLineStyle();
    lineStyle.setWidth(4);
    lineStyle.setColor(new Color(255, 0, 0, 127));
    style.setLineStyle(lineStyle);
    final IdAttributes idAttributes = kmlFactory.createIdAttributes("linestyleExample", null);
    style.setIdAttributes(idAttributes);
    final Feature document = kmlFactory.createDocument();
    document.setPropertyValue(KmlConstants.TAG_STYLE_SELECTOR, style);
    document.setPropertyValue(KmlConstants.TAG_FEATURES, placemark);
    document.setPropertyValue(KmlConstants.TAG_NAME, "LineStyle.kml");
    document.setPropertyValue(KmlConstants.TAG_OPEN, Boolean.TRUE);
    final Kml kml = kmlFactory.createKml(null, document, null, null);
    final File temp = File.createTempFile("testLineStyle", ".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) LineStyle(org.geotoolkit.data.kml.model.LineStyle) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) Color(java.awt.Color) 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) LineString(org.geotoolkit.data.kml.model.LineString) LineStyle(org.geotoolkit.data.kml.model.LineStyle) Style(org.geotoolkit.data.kml.model.Style) File(java.io.File) Test(org.junit.Test)

Example 5 with LineString

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

the class AltitudeModeTest method altitudeModeReadTest.

@Test
public void altitudeModeReadTest() throws IOException, XMLStreamException, URISyntaxException, KmlException {
    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();
    final Feature placemark = kmlObjects.getAbstractFeature();
    assertEquals(KmlModelConstants.TYPE_PLACEMARK, placemark.getType());
    assertEquals("gx:altitudeMode Example", placemark.getPropertyValue(KmlConstants.TAG_NAME));
    final LookAt lookAt = (LookAt) placemark.getPropertyValue(KmlConstants.TAG_VIEW);
    assertEquals(146.806, lookAt.getLongitude(), DELTA);
    assertEquals(12.219, lookAt.getLatitude(), DELTA);
    assertEquals(-60, lookAt.getHeading(), DELTA);
    assertEquals(70, lookAt.getTilt(), DELTA);
    assertEquals(6300, lookAt.getRange(), DELTA);
    assertEquals(EnumAltitudeMode.RELATIVE_TO_SEA_FLOOR, lookAt.getAltitudeMode());
    final LineString lineString = (LineString) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
    assertTrue(lineString.getExtrude());
    assertEquals(EnumAltitudeMode.RELATIVE_TO_SEA_FLOOR, lineString.getAltitudeMode());
    CoordinateSequence coordinates = lineString.getCoordinateSequence();
    assertEquals(5, coordinates.size());
    Coordinate coordinate0 = coordinates.getCoordinate(0);
    assertEquals(146.825, coordinate0.x, DELTA);
    assertEquals(12.233, coordinate0.y, DELTA);
    assertEquals(400.0, coordinate0.z, DELTA);
    Coordinate coordinate1 = coordinates.getCoordinate(1);
    assertEquals(146.820, coordinate1.x, DELTA);
    assertEquals(12.222, coordinate1.y, DELTA);
    assertEquals(400.0, coordinate1.z, DELTA);
    Coordinate coordinate2 = coordinates.getCoordinate(2);
    assertEquals(146.812, coordinate2.x, DELTA);
    assertEquals(12.212, coordinate2.y, DELTA);
    assertEquals(400.0, coordinate2.z, DELTA);
    Coordinate coordinate3 = coordinates.getCoordinate(3);
    assertEquals(146.796, coordinate3.x, DELTA);
    assertEquals(12.209, coordinate3.y, DELTA);
    assertEquals(400.0, coordinate3.z, DELTA);
    Coordinate coordinate4 = coordinates.getCoordinate(4);
    assertEquals(146.788, coordinate4.x, DELTA);
    assertEquals(12.205, coordinate4.y, DELTA);
    assertEquals(400.0, coordinate4.z, DELTA);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) LookAt(org.geotoolkit.data.kml.model.LookAt) LineString(org.geotoolkit.data.kml.model.LineString) Coordinate(org.locationtech.jts.geom.Coordinate) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) Kml(org.geotoolkit.data.kml.model.Kml) GxReader(org.geotoolkit.data.gx.xml.GxReader) File(java.io.File) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Aggregations

LineString (org.geotoolkit.data.kml.model.LineString)12 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)11 File (java.io.File)10 Kml (org.geotoolkit.data.kml.model.Kml)10 Test (org.junit.Test)10 Coordinate (org.locationtech.jts.geom.Coordinate)10 Feature (org.opengis.feature.Feature)10 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)5 KmlWriter (org.geotoolkit.data.kml.xml.KmlWriter)5 LookAt (org.geotoolkit.data.kml.model.LookAt)4 LineStyle (org.geotoolkit.data.kml.model.LineStyle)3 LinearRing (org.geotoolkit.data.kml.model.LinearRing)3 Point (org.geotoolkit.data.kml.model.Point)3 Polygon (org.geotoolkit.data.kml.model.Polygon)3 Style (org.geotoolkit.data.kml.model.Style)3 Color (java.awt.Color)2 URI (java.net.URI)2 AbstractGeometry (org.geotoolkit.data.kml.model.AbstractGeometry)2 Boundary (org.geotoolkit.data.kml.model.Boundary)2 IdAttributes (org.geotoolkit.data.kml.model.IdAttributes)2