Search in sources :

Example 1 with LatLonBox

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

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

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

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

Example 5 with LatLonBox

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

the class IconExtTest method iconExtReadTest.

@Test
public void iconExtReadTest() throws IOException, XMLStreamException, KmlException, URISyntaxException {
    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("GroundOverlay.kml", groundOverlay.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals("7fffffff", KmlUtilities.toKmlColor((Color) groundOverlay.getPropertyValue(KmlConstants.TAG_COLOR)));
    assertEquals(1, groundOverlay.getPropertyValue(KmlConstants.TAG_DRAW_ORDER));
    final Icon icon = (Icon) groundOverlay.getPropertyValue(KmlConstants.TAG_ICON);
    assertEquals("http://www.google.com/intl/en/images/logo.gif", icon.getHref());
    assertEquals(RefreshMode.ON_INTERVAL, icon.getRefreshMode());
    assertEquals(86400, icon.getRefreshInterval(), DELTA);
    assertEquals(0.75, icon.getViewBoundScale(), DELTA);
    final Extensions extensions = icon.extensions();
    assertEquals(4, extensions.simples(Names.BASIC_LINK).size());
    int x = (Integer) extensions.simples(Names.BASIC_LINK).get(0).getValue();
    int y = (Integer) extensions.simples(Names.BASIC_LINK).get(1).getValue();
    int w = (Integer) extensions.simples(Names.BASIC_LINK).get(2).getValue();
    int h = (Integer) extensions.simples(Names.BASIC_LINK).get(3).getValue();
    assertEquals(2, x);
    assertEquals(8, y);
    assertEquals(1000, w);
    assertEquals(87, h);
    final LatLonBox latLonBox = (LatLonBox) groundOverlay.getPropertyValue(KmlConstants.TAG_LAT_LON_BOX);
    assertEquals(37.83234, latLonBox.getNorth(), DELTA);
    assertEquals(37.832122, latLonBox.getSouth(), DELTA);
    assertEquals(-122.373033, latLonBox.getEast(), DELTA);
    assertEquals(-122.373724, latLonBox.getWest(), DELTA);
    assertEquals(45, latLonBox.getRotation(), DELTA);
}
Also used : Color(java.awt.Color) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) LatLonBox(org.geotoolkit.data.kml.model.LatLonBox) 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) Test(org.junit.Test)

Aggregations

Icon (org.geotoolkit.data.kml.model.Icon)6 LatLonBox (org.geotoolkit.data.kml.model.LatLonBox)6 Color (java.awt.Color)5 Feature (org.opengis.feature.Feature)5 File (java.io.File)4 Kml (org.geotoolkit.data.kml.model.Kml)4 Test (org.junit.Test)4 Extensions (org.geotoolkit.data.kml.model.Extensions)2 Link (org.geotoolkit.data.kml.model.Link)2 RefreshMode (org.geotoolkit.data.kml.model.RefreshMode)2 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)2 KmlWriter (org.geotoolkit.data.kml.xml.KmlWriter)2 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 GridCoverage (org.apache.sis.coverage.grid.GridCoverage)1 GridCoverageResource (org.apache.sis.storage.GridCoverageResource)1