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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations