Search in sources :

Example 1 with Icon

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

the class KmzContextInterpreter method writeCoverageMapLayer.

/**
 * Transforms a CoverageMapLayer into KML GroundOverlay.
 */
private Feature writeCoverageMapLayer(MapLayer coverageMapLayer) throws Exception {
    final Feature groundOverlay = KML_FACTORY.createGroundOverlay();
    final CoordinateReferenceSystem targetCrs = CommonCRS.WGS84.normalizedGeographic();
    final GridCoverageResource ref = (GridCoverageResource) coverageMapLayer.getData();
    final GridCoverage coverage = ref.read(null);
    final GridCoverage targetCoverage = new ResampleProcess(coverage, targetCrs, null, InterpolationCase.NEIGHBOR, null).executeNow();
    final Envelope envelope = targetCoverage.getGridGeometry().getEnvelope();
    final CharSequence name = CoverageUtilities.getName(targetCoverage);
    // Creating image file and Writting referenced image into.
    final Path img = filesDirectory.resolve(name.toString() + ".png");
    try (OutputStream outputStream = Files.newOutputStream(img, CREATE, WRITE, TRUNCATE_EXISTING)) {
        ImageIO.write(targetCoverage.render(null), "png", outputStream);
    }
    final Icon image = KML_FACTORY.createIcon(KML_FACTORY.createLink());
    image.setHref(filesDirectory.getFileName().toString() + File.separator + name);
    groundOverlay.setPropertyValue(KmlConstants.TAG_NAME, name.toString());
    groundOverlay.setPropertyValue(KmlConstants.TAG_ICON, image);
    groundOverlay.setPropertyValue(KmlConstants.TAG_ALTITUDE, 1.0);
    groundOverlay.setPropertyValue(KmlConstants.TAG_ALTITUDE_MODE, EnumAltitudeMode.CLAMP_TO_GROUND);
    final LatLonBox latLonBox = KML_FACTORY.createLatLonBox();
    latLonBox.setNorth(envelope.getMaximum(1));
    latLonBox.setSouth(envelope.getMinimum(1));
    latLonBox.setEast(envelope.getMaximum(0));
    latLonBox.setWest(envelope.getMinimum(0));
    groundOverlay.setPropertyValue(KmlConstants.TAG_LAT_LON_BOX, latLonBox);
    return groundOverlay;
}
Also used : Path(java.nio.file.Path) ResampleProcess(org.geotoolkit.processing.coverage.resample.ResampleProcess) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) ZipOutputStream(java.util.zip.ZipOutputStream) OutputStream(java.io.OutputStream) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) LatLonBox(org.geotoolkit.data.kml.model.LatLonBox) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Icon(org.geotoolkit.data.kml.model.Icon) Envelope(org.opengis.geometry.Envelope) Feature(org.opengis.feature.Feature)

Example 2 with Icon

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

the class IconExtTest method iconExtWriteTest.

@Test
public void iconExtWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final double north = 37.83234;
    final double south = 37.832122;
    final double east = -122.373033;
    final double west = -122.373724;
    final double rotation = 45;
    final LatLonBox latLonBox = kmlFactory.createLatLonBox(null, null, north, south, east, west, null, null, rotation, null, null);
    final String href = "http://www.google.com/intl/en/images/logo.gif";
    final RefreshMode refreshMode = RefreshMode.ON_INTERVAL;
    final double refreshInterval = 86400;
    final double viewBoundScale = 0.75;
    final Link link = kmlFactory.createLink();
    link.setHref(href);
    link.setRefreshMode(refreshMode);
    link.setRefreshInterval(refreshInterval);
    link.setViewBoundScale(viewBoundScale);
    link.extensions().simples(Names.BASIC_LINK).add(kmlFactory.createSimpleTypeContainer(GxConstants.URI_GX, GxConstants.TAG_X, 2));
    link.extensions().simples(Names.BASIC_LINK).add(kmlFactory.createSimpleTypeContainer(GxConstants.URI_GX, GxConstants.TAG_Y, 8));
    link.extensions().simples(Names.BASIC_LINK).add(kmlFactory.createSimpleTypeContainer(GxConstants.URI_GX, GxConstants.TAG_W, 1000));
    link.extensions().simples(Names.BASIC_LINK).add(kmlFactory.createSimpleTypeContainer(GxConstants.URI_GX, GxConstants.TAG_H, 87));
    final Icon icon = kmlFactory.createIcon(link);
    final String name = "GroundOverlay.kml";
    final Color color = new Color(255, 255, 255, 127);
    final int drawOrder = 1;
    final Feature groundOverlay = kmlFactory.createGroundOverlay();
    groundOverlay.setPropertyValue(KmlConstants.TAG_NAME, name);
    groundOverlay.setPropertyValue(KmlConstants.TAG_COLOR, color);
    groundOverlay.setPropertyValue(KmlConstants.TAG_DRAW_ORDER, drawOrder);
    groundOverlay.setPropertyValue(KmlConstants.TAG_ICON, icon);
    groundOverlay.setPropertyValue(KmlConstants.TAG_LAT_LON_BOX, latLonBox);
    final Kml kml = kmlFactory.createKml(null, groundOverlay, null, null);
    kml.addExtensionUri(GxConstants.URI_GX, "gx");
    final File temp = File.createTempFile("testIconExt", ".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 : KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) Color(java.awt.Color) RefreshMode(org.geotoolkit.data.kml.model.RefreshMode) GxWriter(org.geotoolkit.data.gx.xml.GxWriter) LatLonBox(org.geotoolkit.data.kml.model.LatLonBox) Kml(org.geotoolkit.data.kml.model.Kml) Feature(org.opengis.feature.Feature) Icon(org.geotoolkit.data.kml.model.Icon) File(java.io.File) Link(org.geotoolkit.data.kml.model.Link) Test(org.junit.Test)

Example 3 with Icon

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

the class LatLonQuadTest method latLonQuadReadTest.

@Test
public void latLonQuadReadTest() 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 groundOverlay = kmlObjects.getAbstractFeature();
    assertEquals(KmlModelConstants.TYPE_GROUND_OVERLAY, groundOverlay.getType());
    assertEquals("gx:LatLonQuad Example", groundOverlay.getPropertyValue(KmlConstants.TAG_NAME));
    final Icon icon = (Icon) groundOverlay.getPropertyValue(KmlConstants.TAG_ICON);
    assertEquals("http://code.google.com/apis/kml/documentation/Images/rectangle.gif", icon.getHref());
    assertEquals(0.75, icon.getViewBoundScale(), DELTA);
    final Extensions extensions = (Extensions) groundOverlay.getProperty(KmlConstants.TAG_EXTENSIONS).getValue();
    assertEquals(1, extensions.complexes(Extensions.Names.GROUND_OVERLAY).size());
    final LatLonQuad latLonQuad = (LatLonQuad) extensions.complexes(Extensions.Names.GROUND_OVERLAY).get(0);
    final CoordinateSequence coordinates = latLonQuad.getCoordinates();
    assertEquals(4, coordinates.size());
    final Coordinate coordinate0 = coordinates.getCoordinate(0);
    assertEquals(81.601884, coordinate0.x, DELTA);
    assertEquals(44.160723, coordinate0.y, DELTA);
    assertEquals(Double.NaN, coordinate0.z, DELTA);
    final Coordinate coordinate1 = coordinates.getCoordinate(1);
    assertEquals(83.529902, coordinate1.x, DELTA);
    assertEquals(43.665148, coordinate1.y, DELTA);
    assertEquals(Double.NaN, coordinate1.z, DELTA);
    final Coordinate coordinate2 = coordinates.getCoordinate(2);
    assertEquals(82.947737, coordinate2.x, DELTA);
    assertEquals(44.248831, coordinate2.y, DELTA);
    assertEquals(Double.NaN, coordinate2.z, DELTA);
    final Coordinate coordinate3 = coordinates.getCoordinate(3);
    assertEquals(81.509322, coordinate3.x, DELTA);
    assertEquals(44.321015, coordinate3.y, DELTA);
    assertEquals(Double.NaN, coordinate3.z, DELTA);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) Coordinate(org.locationtech.jts.geom.Coordinate) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) Kml(org.geotoolkit.data.kml.model.Kml) Icon(org.geotoolkit.data.kml.model.Icon) Extensions(org.geotoolkit.data.kml.model.Extensions) GxReader(org.geotoolkit.data.gx.xml.GxReader) File(java.io.File) Feature(org.opengis.feature.Feature) LatLonQuad(org.geotoolkit.data.gx.model.LatLonQuad) Test(org.junit.Test)

Example 4 with Icon

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

the class GroundOverlayTest method groundOverlayWriteTest.

@Test
public void groundOverlayWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final double north = 37.83234;
    final double south = 37.832122;
    final double east = -122.373033;
    final double west = -122.373724;
    final double rotation = 45;
    final LatLonBox latLonBox = kmlFactory.createLatLonBox(null, null, north, south, east, west, null, null, rotation, null, null);
    final String href = "http://www.google.com/intl/en/images/logo.gif";
    final RefreshMode refreshMode = RefreshMode.ON_INTERVAL;
    final double refreshInterval = 86400;
    final double viewBoundScale = 0.75;
    final Link link = kmlFactory.createLink();
    link.setHref(href);
    link.setRefreshMode(refreshMode);
    link.setRefreshInterval(refreshInterval);
    link.setViewBoundScale(viewBoundScale);
    final Icon icon = kmlFactory.createIcon(link);
    final String name = "GroundOverlay.kml";
    final Color color = new Color(255, 255, 255, 127);
    final int drawOrder = 1;
    final Feature groundOverlay = kmlFactory.createGroundOverlay();
    groundOverlay.setPropertyValue(KmlConstants.TAG_NAME, name);
    groundOverlay.setPropertyValue(KmlConstants.TAG_COLOR, color);
    groundOverlay.setPropertyValue(KmlConstants.TAG_DRAW_ORDER, drawOrder);
    groundOverlay.setPropertyValue(KmlConstants.TAG_ICON, icon);
    groundOverlay.setPropertyValue(KmlConstants.TAG_LAT_LON_BOX, latLonBox);
    final Kml kml = kmlFactory.createKml(null, groundOverlay, null, null);
    final File temp = File.createTempFile("testGroundOverlay", ".kml");
    temp.deleteOnExit();
    final KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.write(kml);
    writer.dispose();
    DomCompare.compare(new File(pathToTestFile), temp);
}
Also used : KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) Color(java.awt.Color) RefreshMode(org.geotoolkit.data.kml.model.RefreshMode) LatLonBox(org.geotoolkit.data.kml.model.LatLonBox) Kml(org.geotoolkit.data.kml.model.Kml) Feature(org.opengis.feature.Feature) Icon(org.geotoolkit.data.kml.model.Icon) File(java.io.File) Link(org.geotoolkit.data.kml.model.Link) Test(org.junit.Test)

Example 5 with Icon

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

the class KmlReader method readGroundOverlay.

private Feature readGroundOverlay() throws XMLStreamException, KmlException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
    IdAttributes idAttributes = readIdAttributes();
    // AbstractFeature
    String name = null;
    boolean visibility = DEF_VISIBILITY;
    boolean open = DEF_OPEN;
    AtomPersonConstruct author = null;
    AtomLink link = null;
    String address = null;
    AddressDetails addressDetails = null;
    String phoneNumber = null;
    Object snippet = null;
    Object description = null;
    AbstractView view = null;
    AbstractTimePrimitive timePrimitive = null;
    URI styleUrl = null;
    List<AbstractStyleSelector> styleSelector = new ArrayList<>();
    Region region = null;
    Object extendedData = null;
    List<SimpleTypeContainer> featureSimpleExtensions = new ArrayList<>();
    List<Object> featureObjectExtensions = new ArrayList<>();
    // AbstractOverlay
    Color color = DEF_COLOR;
    int drawOrder = DEF_DRAW_ORDER;
    Icon icon = null;
    List<SimpleTypeContainer> abstractOverlaySimpleExtensions = new ArrayList<>();
    List<Object> abstractOverlayObjectExtensions = new ArrayList<>();
    // GroundOverlay
    double altitude = DEF_ALTITUDE;
    AltitudeMode altitudeMode = DEF_ALTITUDE_MODE;
    LatLonBox latLonBox = null;
    List<SimpleTypeContainer> groundOverlaySimpleExtensions = new ArrayList<>();
    List<Object> groundOverlayObjectExtensions = new ArrayList<>();
    boucle: while (reader.hasNext()) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                {
                    final String eName = reader.getLocalName();
                    final String eUri = reader.getNamespaceURI();
                    // KML
                    if (equalsNamespace(eUri)) {
                        switch(eName) {
                            case TAG_NAME:
                                name = reader.getElementText();
                                break;
                            case TAG_VISIBILITY:
                                visibility = parseBoolean(reader.getElementText());
                                break;
                            case TAG_OPEN:
                                open = parseBoolean(reader.getElementText());
                                break;
                            case TAG_ADDRESS:
                                address = reader.getElementText();
                                break;
                            case TAG_PHONE_NUMBER:
                                phoneNumber = reader.getElementText();
                                break;
                            case TAG_SNIPPET:
                                snippet = readElementText();
                                break;
                            case TAG_SNIPPET_BIG:
                                snippet = readSnippet();
                                break;
                            case TAG_DESCRIPTION:
                                description = readElementText();
                                break;
                            case TAG_STYLE_URL:
                                styleUrl = new URI(reader.getElementText());
                                break;
                            case TAG_REGION:
                                region = readRegion();
                                break;
                            case TAG_EXTENDED_DATA:
                                extendedData = readExtendedData();
                                break;
                            case TAG_META_DATA:
                                extendedData = readMetaData();
                                break;
                            case TAG_COLOR:
                                color = KmlUtilities.parseColor(reader.getElementText());
                                break;
                            case TAG_DRAW_ORDER:
                                drawOrder = Integer.parseInt(reader.getElementText());
                                break;
                            case TAG_ICON:
                                icon = readIcon(eName);
                                break;
                            case TAG_ALTITUDE:
                                altitude = parseDouble(reader.getElementText());
                                break;
                            case TAG_ALTITUDE_MODE:
                                altitudeMode = readAltitudeMode();
                                break;
                            case TAG_LAT_LON_BOX:
                                latLonBox = readLatLonBox();
                                break;
                            default:
                                {
                                    if (isAbstractView(eName)) {
                                        view = readAbstractView(eName);
                                    } else if (isAbstractTimePrimitive(eName)) {
                                        timePrimitive = readAbstractTimePrimitive(eName);
                                    } else if (isAbstractStyleSelector(eName)) {
                                        styleSelector.add(readAbstractStyleSelector(eName));
                                    }
                                    break;
                                }
                        }
                    } else if (URI_ATOM.equals(eUri)) {
                        checkVersion(URI_KML_2_2);
                        // ABSTRACT FEATURE
                        if (TAG_ATOM_AUTHOR.equals(eName)) {
                            author = readAtomPersonConstruct();
                        } else if (TAG_ATOM_LINK.equals(eName)) {
                            link = readAtomLink();
                        }
                    } else if (URI_XAL.equals(eUri)) {
                        checkVersion(URI_KML_2_2);
                        // ABSTRACT FEATURE
                        if (TAG_XAL_ADDRESS_DETAILS.equals(eName)) {
                            addressDetails = readXalAddressDetails();
                        }
                    } else // EXTENSIONS
                    {
                        KmlExtensionReader r;
                        if ((r = this.getComplexExtensionReader(TAG_GROUND_OVERLAY, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_GROUND_OVERLAY, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.FEATURE.equals(extensionLevel)) {
                                featureObjectExtensions.add(ext);
                            } else if (Extensions.Names.OVERLAY.equals(extensionLevel)) {
                                abstractOverlayObjectExtensions.add(ext);
                            } else if (Extensions.Names.GROUND_OVERLAY.equals(extensionLevel)) {
                                groundOverlayObjectExtensions.add(ext);
                            } else if (extensionLevel == null) {
                                if (ext instanceof AltitudeMode) {
                                    altitudeMode = (AltitudeMode) ext;
                                }
                            }
                        } else if ((r = getSimpleExtensionReader(TAG_GROUND_OVERLAY, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_GROUND_OVERLAY, 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.FEATURE.equals(extensionLevel)) {
                                featureSimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.OVERLAY.equals(extensionLevel)) {
                                abstractOverlaySimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.GROUND_OVERLAY.equals(extensionLevel)) {
                                groundOverlaySimpleExtensions.add((SimpleTypeContainer) ext);
                            }
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_GROUND_OVERLAY.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createGroundOverlay(objectSimpleExtensions, idAttributes, name, visibility, open, author, link, address, addressDetails, phoneNumber, snippet, description, view, timePrimitive, styleUrl, styleSelector, region, extendedData, featureSimpleExtensions, featureObjectExtensions, color, drawOrder, icon, abstractOverlaySimpleExtensions, abstractOverlayObjectExtensions, altitude, altitudeMode, latLonBox, groundOverlaySimpleExtensions, groundOverlayObjectExtensions);
}
Also used : AbstractTimePrimitive(org.geotoolkit.data.kml.model.AbstractTimePrimitive) AtomLink(org.geotoolkit.atom.model.AtomLink) 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) URI(java.net.URI) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) AtomPersonConstruct(org.geotoolkit.atom.model.AtomPersonConstruct) Entry(java.util.Map.Entry) AbstractView(org.geotoolkit.data.kml.model.AbstractView) AddressDetails(org.geotoolkit.xal.model.AddressDetails) Color(java.awt.Color) LatLonBox(org.geotoolkit.data.kml.model.LatLonBox) AbstractLatLonBox(org.geotoolkit.data.kml.model.AbstractLatLonBox) AbstractStyleSelector(org.geotoolkit.data.kml.model.AbstractStyleSelector) Point(org.geotoolkit.data.kml.model.Point) Region(org.geotoolkit.data.kml.model.Region) Icon(org.geotoolkit.data.kml.model.Icon) ItemIcon(org.geotoolkit.data.kml.model.ItemIcon)

Aggregations

Icon (org.geotoolkit.data.kml.model.Icon)15 Feature (org.opengis.feature.Feature)11 File (java.io.File)10 Kml (org.geotoolkit.data.kml.model.Kml)10 Test (org.junit.Test)10 Color (java.awt.Color)7 LatLonBox (org.geotoolkit.data.kml.model.LatLonBox)6 Extensions (org.geotoolkit.data.kml.model.Extensions)5 Link (org.geotoolkit.data.kml.model.Link)5 Point (org.geotoolkit.data.kml.model.Point)5 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)5 KmlWriter (org.geotoolkit.data.kml.xml.KmlWriter)5 IdAttributes (org.geotoolkit.data.kml.model.IdAttributes)4 ItemIcon (org.geotoolkit.data.kml.model.ItemIcon)4 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 Entry (java.util.Map.Entry)3 AtomLink (org.geotoolkit.atom.model.AtomLink)3 AtomPersonConstruct (org.geotoolkit.atom.model.AtomPersonConstruct)3