Search in sources :

Example 1 with Location

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

the class ModelTest method modelReadTest.

@Test
public void modelReadTest() throws IOException, XMLStreamException, URISyntaxException, KmlException {
    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();
    final Feature placemark = kmlObjects.getAbstractFeature();
    assertEquals("Colorado", placemark.getPropertyValue(KmlConstants.TAG_NAME));
    final Model model = (Model) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
    assertEquals("khModel543", model.getIdAttributes().getId());
    assertEquals(EnumAltitudeMode.RELATIVE_TO_GROUND, model.getAltitudeMode());
    final Location location = model.getLocation();
    assertEquals(39.55375305703105, location.getLongitude(), DELTA);
    assertEquals(-18.9813220168456, location.getLatitude(), DELTA);
    assertEquals(1223, location.getAltitude(), DELTA);
    final Orientation orientation = model.getOrientation();
    assertEquals(45, orientation.getHeading(), DELTA);
    assertEquals(10, orientation.getTilt(), DELTA);
    assertEquals(0.5, orientation.getRoll(), DELTA);
    final Scale scale = model.getScale();
    assertEquals(4, scale.getX(), DELTA);
    assertEquals(2, scale.getY(), DELTA);
    assertEquals(3, scale.getZ(), DELTA);
    final Link link = model.getLink();
    assertEquals("house.dae", link.getHref());
    assertEquals(RefreshMode.ON_EXPIRE, link.getRefreshMode());
    final ResourceMap resourceMap = model.getRessourceMap();
    assertEquals(3, resourceMap.getAliases().size());
    final Alias alias0 = resourceMap.getAliases().get(0);
    assertEquals(new URI("../files/CU-Macky---Center-StairsnoCulling.jpg"), alias0.getTargetHref());
    assertEquals(new URI("CU-Macky---Center-StairsnoCulling.jpg"), alias0.getSourceHref());
    final Alias alias1 = resourceMap.getAliases().get(1);
    assertEquals(new URI("../files/CU-Macky-4sideturretnoCulling.jpg"), alias1.getTargetHref());
    assertEquals(new URI("CU-Macky-4sideturretnoCulling.jpg"), alias1.getSourceHref());
    final Alias alias2 = resourceMap.getAliases().get(2);
    assertEquals(new URI("../files/CU-Macky-Back-NorthnoCulling.jpg"), alias2.getTargetHref());
    assertEquals(new URI("CU-Macky-Back-NorthnoCulling.jpg"), alias2.getSourceHref());
}
Also used : ResourceMap(org.geotoolkit.data.kml.model.ResourceMap) Alias(org.geotoolkit.data.kml.model.Alias) Model(org.geotoolkit.data.kml.model.Model) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) Scale(org.geotoolkit.data.kml.model.Scale) Kml(org.geotoolkit.data.kml.model.Kml) Orientation(org.geotoolkit.data.kml.model.Orientation) File(java.io.File) Feature(org.opengis.feature.Feature) URI(java.net.URI) Link(org.geotoolkit.data.kml.model.Link) Location(org.geotoolkit.data.kml.model.Location) Test(org.junit.Test)

Example 2 with Location

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

the class ModelTest method modelWriteTest.

@Test
public void modelWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Alias alias0 = kmlFactory.createAlias();
    alias0.setTargetHref(new URI("../files/CU-Macky---Center-StairsnoCulling.jpg"));
    alias0.setSourceHref(new URI("CU-Macky---Center-StairsnoCulling.jpg"));
    final Alias alias1 = kmlFactory.createAlias();
    alias1.setTargetHref(new URI("../files/CU-Macky-4sideturretnoCulling.jpg"));
    alias1.setSourceHref(new URI("CU-Macky-4sideturretnoCulling.jpg"));
    final Alias alias2 = kmlFactory.createAlias();
    alias2.setTargetHref(new URI("../files/CU-Macky-Back-NorthnoCulling.jpg"));
    alias2.setSourceHref(new URI("CU-Macky-Back-NorthnoCulling.jpg"));
    final ResourceMap resourceMap = kmlFactory.createResourceMap();
    resourceMap.setAliases(Arrays.asList(alias0, alias1, alias2));
    final Link link = kmlFactory.createLink();
    link.setHref("house.dae");
    link.setRefreshMode(RefreshMode.ON_EXPIRE);
    final Scale scale = kmlFactory.createScale();
    scale.setX(4);
    scale.setY(2);
    scale.setZ(3);
    final Orientation orientation = kmlFactory.createOrientation();
    orientation.setHeading(45);
    orientation.setTilt(10);
    orientation.setRoll(0.5);
    final Location location = kmlFactory.createLocation();
    location.setLongitude(39.55375305703105);
    location.setLatitude(-18.9813220168456);
    location.setAltitude(1223);
    final Model model = kmlFactory.createModel();
    final IdAttributes idAttributes = kmlFactory.createIdAttributes("khModel543", null);
    model.setIdAttributes(idAttributes);
    model.setAltitudeMode(EnumAltitudeMode.RELATIVE_TO_GROUND);
    model.setLocation(location);
    model.setOrientation(orientation);
    model.setScale(scale);
    model.setLink(link);
    model.setRessourceMap(resourceMap);
    final Feature placemark = kmlFactory.createPlacemark();
    placemark.setPropertyValue(KmlConstants.TAG_NAME, "Colorado");
    placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, model);
    final Kml kml = kmlFactory.createKml(null, placemark, null, null);
    final File temp = File.createTempFile("testModel", ".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) Scale(org.geotoolkit.data.kml.model.Scale) Kml(org.geotoolkit.data.kml.model.Kml) Orientation(org.geotoolkit.data.kml.model.Orientation) URI(java.net.URI) Feature(org.opengis.feature.Feature) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) ResourceMap(org.geotoolkit.data.kml.model.ResourceMap) Alias(org.geotoolkit.data.kml.model.Alias) Model(org.geotoolkit.data.kml.model.Model) File(java.io.File) Link(org.geotoolkit.data.kml.model.Link) Location(org.geotoolkit.data.kml.model.Location) Test(org.junit.Test)

Example 3 with Location

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

the class KmlReader method readModel.

public Model readModel() throws XMLStreamException, KmlException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
    IdAttributes idAttributes = readIdAttributes();
    // AbstractGeometry
    List<SimpleTypeContainer> abstractGeometrySimpleExtensions = new ArrayList<>();
    List<Object> abstractGeometryObjectExtensions = new ArrayList<>();
    // Model
    AltitudeMode altitudeMode = DEF_ALTITUDE_MODE;
    Location location = null;
    Orientation orientation = null;
    Scale scale = null;
    Link link = null;
    ResourceMap resourceMap = null;
    List<SimpleTypeContainer> modelSimpleExtensions = new ArrayList<>();
    List<Object> modelObjectExtensions = new ArrayList<>();
    boucle: while (reader.hasNext()) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                {
                    final String eName = reader.getLocalName();
                    final String eUri = reader.getNamespaceURI();
                    if (equalsNamespace(eUri)) {
                        switch(eName) {
                            case TAG_ALTITUDE_MODE:
                                altitudeMode = readAltitudeMode();
                                break;
                            case TAG_LOCATION:
                                location = readLocation();
                                break;
                            case TAG_ORIENTATION:
                                orientation = readOrientation();
                                break;
                            case TAG_SCALE_BIG:
                                scale = readScale();
                                break;
                            case TAG_LINK:
                                link = readLink(eName);
                                break;
                            case TAG_RESOURCE_MAP:
                                checkVersion(URI_KML_2_2);
                                resourceMap = readResourceMap();
                                break;
                        }
                    } else // EXTENSIONS
                    {
                        KmlExtensionReader r;
                        if ((r = this.getComplexExtensionReader(TAG_MODEL, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_MODEL, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.GEOMETRY.equals(extensionLevel)) {
                                abstractGeometryObjectExtensions.add(ext);
                            } else if (Extensions.Names.MODEL.equals(extensionLevel)) {
                                modelObjectExtensions.add(ext);
                            } else if (extensionLevel == null) {
                                if (ext instanceof AltitudeMode) {
                                    altitudeMode = (AltitudeMode) ext;
                                }
                            }
                        } else if ((r = getSimpleExtensionReader(TAG_MODEL, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_MODEL, 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.MODEL.equals(extensionLevel)) {
                                modelSimpleExtensions.add((SimpleTypeContainer) ext);
                            }
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_MODEL.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createModel(objectSimpleExtensions, idAttributes, abstractGeometrySimpleExtensions, abstractGeometryObjectExtensions, altitudeMode, location, orientation, scale, link, resourceMap, modelSimpleExtensions, modelObjectExtensions);
}
Also used : ArrayList(java.util.ArrayList) Scale(org.geotoolkit.data.kml.model.Scale) 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) Orientation(org.geotoolkit.data.kml.model.Orientation) Extensions(org.geotoolkit.data.kml.model.Extensions) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) ResourceMap(org.geotoolkit.data.kml.model.ResourceMap) Entry(java.util.Map.Entry) AtomLink(org.geotoolkit.atom.model.AtomLink) Link(org.geotoolkit.data.kml.model.Link) BasicLink(org.geotoolkit.data.kml.model.BasicLink) Location(org.geotoolkit.data.kml.model.Location)

Aggregations

Link (org.geotoolkit.data.kml.model.Link)3 Location (org.geotoolkit.data.kml.model.Location)3 Orientation (org.geotoolkit.data.kml.model.Orientation)3 ResourceMap (org.geotoolkit.data.kml.model.ResourceMap)3 Scale (org.geotoolkit.data.kml.model.Scale)3 File (java.io.File)2 URI (java.net.URI)2 Alias (org.geotoolkit.data.kml.model.Alias)2 IdAttributes (org.geotoolkit.data.kml.model.IdAttributes)2 Kml (org.geotoolkit.data.kml.model.Kml)2 Model (org.geotoolkit.data.kml.model.Model)2 Test (org.junit.Test)2 Feature (org.opengis.feature.Feature)2 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1 AtomLink (org.geotoolkit.atom.model.AtomLink)1 AltitudeMode (org.geotoolkit.data.kml.model.AltitudeMode)1 BasicLink (org.geotoolkit.data.kml.model.BasicLink)1 EnumAltitudeMode (org.geotoolkit.data.kml.model.EnumAltitudeMode)1 Extensions (org.geotoolkit.data.kml.model.Extensions)1