Search in sources :

Example 1 with SchemaData

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

the class KmlReader method readExtendedData.

public ExtendedData readExtendedData() throws XMLStreamException, URISyntaxException, KmlException {
    List<Data> datas = new ArrayList<>();
    List<SchemaData> schemaDatas = new ArrayList<>();
    List<Object> anyOtherElements = 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)) {
                        // EXTENDED DATA
                        if (TAG_DATA.equals(eName)) {
                            datas.add(readData());
                        } else if (TAG_SCHEMA_DATA.equals(eName)) {
                            schemaDatas.add(readSchemaData());
                        }
                    } else // OTHER FREE DATA
                    {
                        KmlExtensionReader r;
                        if ((r = this.getDataReader(TAG_EXTENDED_DATA, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_EXTENDED_DATA, eUri, eName);
                            Object ext = result.getKey();
                            anyOtherElements.add(ext);
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_EXTENDED_DATA.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createExtendedData(datas, schemaDatas, anyOtherElements);
}
Also used : SchemaData(org.geotoolkit.data.kml.model.SchemaData) ArrayList(java.util.ArrayList) SimpleData(org.geotoolkit.data.kml.model.SimpleData) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) SchemaData(org.geotoolkit.data.kml.model.SchemaData) Data(org.geotoolkit.data.kml.model.Data) LineString(org.geotoolkit.data.kml.model.LineString)

Example 2 with SchemaData

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

the class KmlWriter method writeExtendedData.

public void writeExtendedData(ExtendedData value) throws XMLStreamException, KmlException {
    if (value != null) {
        writer.writeStartElement(URI_KML, TAG_EXTENDED_DATA);
        for (Data data : value.getDatas()) {
            writeData(data);
        }
        for (SchemaData schemaData : value.getSchemaData()) {
            writeSchemaData(schemaData);
        }
        writeDataScheduler(value.getAnyOtherElements());
        writer.writeEndElement();
    }
}
Also used : SchemaData(org.geotoolkit.data.kml.model.SchemaData) SimpleData(org.geotoolkit.data.kml.model.SimpleData) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) SchemaData(org.geotoolkit.data.kml.model.SchemaData) Data(org.geotoolkit.data.kml.model.Data)

Example 3 with SchemaData

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

the class SchemaDataTest method schemaDataWriteTest.

@Test
public void schemaDataWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Coordinate coordinate0 = kmlFactory.createCoordinate(-122, 37.002);
    final CoordinateSequence coordinates0 = kmlFactory.createCoordinates(Arrays.asList(coordinate0));
    final Point point0 = kmlFactory.createPoint(coordinates0);
    final SimpleData simpleData00 = kmlFactory.createSimpleData("TrailHeadName", "Pi in the sky");
    final SimpleData simpleData01 = kmlFactory.createSimpleData("TrailLength", "3.14159");
    final SimpleData simpleData02 = kmlFactory.createSimpleData("ElevationGain", "10");
    final SchemaData schemaData0 = kmlFactory.createSchemaData();
    schemaData0.setSimpleDatas(Arrays.asList(simpleData00, simpleData01, simpleData02));
    schemaData0.setSchemaURL(new URI("#TrailHeadTypeId"));
    final ExtendedData extendedData0 = kmlFactory.createExtendedData();
    extendedData0.setSchemaData(Arrays.asList(schemaData0));
    final Feature placemark0 = kmlFactory.createPlacemark();
    placemark0.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, extendedData0);
    placemark0.setPropertyValue(KmlConstants.TAG_NAME, "Easy trail");
    placemark0.setPropertyValue(KmlConstants.TAG_GEOMETRY, point0);
    final Coordinate coordinate1 = kmlFactory.createCoordinate(-122, 37.002);
    final CoordinateSequence coordinates1 = kmlFactory.createCoordinates(Arrays.asList(coordinate1));
    final Point point1 = kmlFactory.createPoint(coordinates1);
    final SimpleData simpleData10 = kmlFactory.createSimpleData("TrailHeadName", "Mount Everest");
    final SimpleData simpleData11 = kmlFactory.createSimpleData("TrailLength", "347.45");
    final SimpleData simpleData12 = kmlFactory.createSimpleData("ElevationGain", "10000");
    final SchemaData schemaData1 = kmlFactory.createSchemaData();
    schemaData1.setSimpleDatas(Arrays.asList(simpleData10, simpleData11, simpleData12));
    schemaData1.setSchemaURL(new URI("#TrailHeadTypeId"));
    final ExtendedData extendedData1 = kmlFactory.createExtendedData();
    extendedData1.setSchemaData(Arrays.asList(schemaData1));
    final Feature placemark1 = kmlFactory.createPlacemark();
    placemark1.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, extendedData1);
    placemark1.setPropertyValue(KmlConstants.TAG_NAME, "Difficult trail");
    placemark1.setPropertyValue(KmlConstants.TAG_GEOMETRY, point1);
    final Feature document = kmlFactory.createDocument();
    document.setPropertyValue(KmlConstants.TAG_FEATURES, Arrays.asList(placemark0, placemark1));
    final Kml kml = kmlFactory.createKml(null, document, null, null);
    final File temp = File.createTempFile("testSchemaData", ".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) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) Coordinate(org.locationtech.jts.geom.Coordinate) SchemaData(org.geotoolkit.data.kml.model.SchemaData) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) SimpleData(org.geotoolkit.data.kml.model.SimpleData) Point(org.geotoolkit.data.kml.model.Point) Kml(org.geotoolkit.data.kml.model.Kml) URI(java.net.URI) Feature(org.opengis.feature.Feature) File(java.io.File) Test(org.junit.Test)

Example 4 with SchemaData

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

the class SchemaDataTest method schemaDataReadTest.

@Test
public void schemaDataReadTest() throws IOException, XMLStreamException, KmlException, URISyntaxException {
    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();
    final Feature document = kmlObjects.getAbstractFeature();
    assertEquals(KmlModelConstants.TYPE_DOCUMENT, document.getType());
    Iterator<?> i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_FEATURES)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    {
        Feature placemark0 = (Feature) i.next();
        assertEquals(KmlModelConstants.TYPE_PLACEMARK, placemark0.getType());
        assertEquals("Easy trail", placemark0.getPropertyValue(KmlConstants.TAG_NAME));
        final ExtendedData extendedData0 = (ExtendedData) ((List) placemark0.getPropertyValue(KmlConstants.TAG_EXTENDED_DATA)).get(0);
        assertEquals(1, extendedData0.getSchemaData().size());
        final SchemaData schemaData0 = extendedData0.getSchemaData().get(0);
        assertEquals("#TrailHeadTypeId", schemaData0.getSchemaURL().toString());
        assertEquals(3, schemaData0.getSimpleDatas().size());
        final SimpleData simpelData00 = schemaData0.getSimpleDatas().get(0);
        assertEquals("TrailHeadName", simpelData00.getName());
        assertEquals("Pi in the sky", simpelData00.getContent());
        final SimpleData simpelData01 = schemaData0.getSimpleDatas().get(1);
        assertEquals("TrailLength", simpelData01.getName());
        assertEquals("3.14159", simpelData01.getContent());
        final SimpleData simpelData02 = schemaData0.getSimpleDatas().get(2);
        assertEquals("ElevationGain", simpelData02.getName());
        assertEquals("10", simpelData02.getContent());
        final Point point0 = (Point) placemark0.getPropertyValue(KmlConstants.TAG_GEOMETRY);
        final CoordinateSequence coordinates0 = point0.getCoordinateSequence();
        assertEquals(1, coordinates0.size());
        final Coordinate coordinate0 = coordinates0.getCoordinate(0);
        assertEquals(-122, coordinate0.x, DELTA);
        assertEquals(37.002, coordinate0.y, DELTA);
    }
    assertTrue("Expected at least 2 elements.", i.hasNext());
    {
        Feature placemark1 = (Feature) i.next();
        assertEquals(KmlModelConstants.TYPE_PLACEMARK, placemark1.getType());
        assertEquals("Difficult trail", placemark1.getPropertyValue(KmlConstants.TAG_NAME));
        ExtendedData extendedData1 = (ExtendedData) ((List) placemark1.getPropertyValue(KmlConstants.TAG_EXTENDED_DATA)).get(0);
        assertEquals(1, extendedData1.getSchemaData().size());
        final SchemaData schemaData1 = extendedData1.getSchemaData().get(0);
        assertEquals("#TrailHeadTypeId", schemaData1.getSchemaURL().toString());
        assertEquals(3, schemaData1.getSimpleDatas().size());
        final SimpleData simpelData10 = schemaData1.getSimpleDatas().get(0);
        assertEquals("TrailHeadName", simpelData10.getName());
        assertEquals("Mount Everest", simpelData10.getContent());
        final SimpleData simpelData11 = schemaData1.getSimpleDatas().get(1);
        assertEquals("TrailLength", simpelData11.getName());
        assertEquals("347.45", simpelData11.getContent());
        final SimpleData simpelData12 = schemaData1.getSimpleDatas().get(2);
        assertEquals("ElevationGain", simpelData12.getName());
        assertEquals("10000", simpelData12.getContent());
        final Point point1 = (Point) placemark1.getPropertyValue(KmlConstants.TAG_GEOMETRY);
        final CoordinateSequence coordinates1 = point1.getCoordinateSequence();
        assertEquals(1, coordinates1.size());
        final Coordinate coordinate1 = coordinates1.getCoordinate(0);
        assertEquals(-122, coordinate1.x, DELTA);
        assertEquals(37.002, coordinate1.y, DELTA);
    }
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) SchemaData(org.geotoolkit.data.kml.model.SchemaData) SimpleData(org.geotoolkit.data.kml.model.SimpleData) Kml(org.geotoolkit.data.kml.model.Kml) Point(org.geotoolkit.data.kml.model.Point) Feature(org.opengis.feature.Feature) Coordinate(org.locationtech.jts.geom.Coordinate) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) List(java.util.List) File(java.io.File) Test(org.junit.Test)

Example 5 with SchemaData

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

the class KmlFeatureUtilities method fillGeometryListFromFeature.

/**
 * recursive method on feature to find geometries
 * @param feature {@link org.opengis.feature.Feature} traveled to find geometry
 * @param geometries {@link List} where we add {@link Geometry}
 */
public static void fillGeometryListFromFeature(final Feature feature, final List<Map.Entry<Object, Map<String, String>>> geometries) {
    final Object geometry = feature.getPropertyValue("geometry");
    if (geometry != null) {
        // create map which have other data value
        final Map<String, String> values = new HashMap<>(0);
        // get feature name
        final String name = (String) feature.getPropertyValue("name");
        if (name != null) {
            values.put("name", name);
        }
        // get extendedData
        final DefaultExtendedData extendData = (DefaultExtendedData) feature.getPropertyValue("ExtendedData");
        if (extendData != null) {
            // loop on extendedSchemaData to find data
            final List<SchemaData> schemaDatas = extendData.getSchemaData();
            for (SchemaData schemaData : schemaDatas) {
                // get simples data to add it on values map
                final List<SimpleData> simpleDataList = schemaData.getSimpleDatas();
                for (SimpleData simpleData : simpleDataList) {
                    values.put(simpleData.getName(), simpleData.getContent());
                }
            }
        }
        // add geometry on list
        geometries.add(new AbstractMap.SimpleEntry<Object, Map<String, String>>(geometry, values));
    // it's a folder, go recursivly on childs
    } else {
        final Iterator<?> iterator = ((Iterable<?>) feature.getPropertyValue(KmlConstants.TAG_FEATURES)).iterator();
        while (iterator.hasNext()) {
            final Object object = iterator.next();
            if (object instanceof Feature) {
                final Feature candidat = (Feature) object;
                // recursive call
                fillGeometryListFromFeature(candidat, geometries);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) SchemaData(org.geotoolkit.data.kml.model.SchemaData) DefaultExtendedData(org.geotoolkit.data.kml.model.DefaultExtendedData) SimpleData(org.geotoolkit.data.kml.model.SimpleData) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature) AbstractMap(java.util.AbstractMap) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Aggregations

SchemaData (org.geotoolkit.data.kml.model.SchemaData)5 SimpleData (org.geotoolkit.data.kml.model.SimpleData)5 ExtendedData (org.geotoolkit.data.kml.model.ExtendedData)4 Feature (org.opengis.feature.Feature)3 File (java.io.File)2 Data (org.geotoolkit.data.kml.model.Data)2 Kml (org.geotoolkit.data.kml.model.Kml)2 Point (org.geotoolkit.data.kml.model.Point)2 Test (org.junit.Test)2 Coordinate (org.locationtech.jts.geom.Coordinate)2 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)2 URI (java.net.URI)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DefaultExtendedData (org.geotoolkit.data.kml.model.DefaultExtendedData)1 LineString (org.geotoolkit.data.kml.model.LineString)1 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)1