Search in sources :

Example 1 with SimpleData

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

the class KmlFeatureUtilities method buildKMLFeature.

/**
 * create a {@link Geometry from a Feature.}
 *
 * @param noKmlFeature feature from an other type
 * @param defaultIdStyle style defined on document.
 * @return a valid kml {@link Feature}
 */
public static Feature buildKMLFeature(Feature noKmlFeature, IdAttributes defaultIdStyle) {
    // Transform geometry
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Feature placemark = kmlFactory.createPlacemark();
    final String geoColumn = AttributeConvention.GEOMETRY;
    final AbstractGeometry ag = buildKMLGeometry((Geometry) noKmlFeature.getPropertyValue(geoColumn));
    placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, ag);
    try {
        placemark.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#" + defaultIdStyle.getId()));
    } catch (URISyntaxException e) {
        LOGGER.log(Level.WARNING, "unnable to define style URI", e);
    }
    // TODO : transform datas
    final List<Data> simpleDatas = new ArrayList<>(0);
    for (final PropertyType type : noKmlFeature.getType().getProperties(true)) {
        final Property property = noKmlFeature.getProperty(type.getName().toString());
        String localPartName = property.getName().tip().toString();
        final Object value = property.getValue();
        if (localPartName.equalsIgnoreCase(KmlConstants.TAG_NAME)) {
            placemark.setPropertyValue(KmlConstants.TAG_NAME, value);
        } else if (!(property.getName().toString().equalsIgnoreCase(geoColumn) || localPartName.equalsIgnoreCase("fid"))) {
            if (value != null) {
                Data simpleData = kmlFactory.createData();
                simpleData.setName(localPartName);
                simpleData.setValue(value.toString());
                simpleDatas.add(simpleData);
            }
        }
    }
    if (!simpleDatas.isEmpty()) {
        ExtendedData extendedData = kmlFactory.createExtendedData();
        extendedData.setDatas(simpleDatas);
        placemark.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, extendedData);
    }
    return placemark;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) DefaultExtendedData(org.geotoolkit.data.kml.model.DefaultExtendedData) 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) DefaultExtendedData(org.geotoolkit.data.kml.model.DefaultExtendedData) Data(org.geotoolkit.data.kml.model.Data) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) URISyntaxException(java.net.URISyntaxException) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) URI(java.net.URI) Property(org.opengis.feature.Property)

Example 2 with SimpleData

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

the class KmlReader method readSchemaData.

private SchemaData readSchemaData() throws XMLStreamException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
    IdAttributes idAttributes = readIdAttributes();
    // SchemaData
    URI schemaURL = new URI(reader.getAttributeValue(null, ATT_SCHEMA_URL));
    List<SimpleData> simpleDatas = new ArrayList<>();
    List<Object> schemaDataExtensions = 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)) {
                        // SCHEMA DATA
                        if (TAG_SIMPLE_DATA.equals(eName)) {
                            simpleDatas.add(readSimpleData());
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_SCHEMA_DATA.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createSchemaData(objectSimpleExtensions, idAttributes, schemaURL, simpleDatas, schemaDataExtensions);
}
Also used : ArrayList(java.util.ArrayList) SimpleData(org.geotoolkit.data.kml.model.SimpleData) LineString(org.geotoolkit.data.kml.model.LineString) SimpleTypeContainer(org.geotoolkit.data.kml.xsd.SimpleTypeContainer) URI(java.net.URI) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes)

Example 3 with SimpleData

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

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

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

the class KmlWriter method writeSchemaData.

private void writeSchemaData(SchemaData value) throws XMLStreamException, KmlException {
    if (value != null) {
        writer.writeStartElement(URI_KML, TAG_SCHEMA_DATA);
        writer.writeAttribute(ATT_SCHEMA_URL, value.getSchemaURL().toString());
        writeCommonAbstractObject(value);
        for (SimpleData simpleData : value.getSimpleDatas()) {
            writeSimpleData(simpleData);
        }
        writer.writeEndElement();
    }
}
Also used : SimpleData(org.geotoolkit.data.kml.model.SimpleData)

Aggregations

SimpleData (org.geotoolkit.data.kml.model.SimpleData)6 SchemaData (org.geotoolkit.data.kml.model.SchemaData)4 Feature (org.opengis.feature.Feature)4 URI (java.net.URI)3 ExtendedData (org.geotoolkit.data.kml.model.ExtendedData)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 DefaultExtendedData (org.geotoolkit.data.kml.model.DefaultExtendedData)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 LineString (org.locationtech.jts.geom.LineString)2 MultiLineString (org.locationtech.jts.geom.MultiLineString)2 URISyntaxException (java.net.URISyntaxException)1 AbstractMap (java.util.AbstractMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1