Search in sources :

Example 1 with Data

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

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

the class DataTest method dataReadTest.

@Test
public void dataReadTest() throws IOException, XMLStreamException, KmlException, URISyntaxException {
    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    final Kml kmlObjects = reader.read();
    reader.dispose();
    final Feature placemark = kmlObjects.getAbstractFeature();
    assertEquals(KmlModelConstants.TYPE_PLACEMARK, placemark.getType());
    assertEquals("Club house", placemark.getPropertyValue(KmlConstants.TAG_NAME));
    final ExtendedData extendedData = (ExtendedData) ((List) placemark.getPropertyValue(KmlConstants.TAG_EXTENDED_DATA)).get(0);
    assertEquals(3, extendedData.getDatas().size());
    final Data data0 = extendedData.getDatas().get(0);
    final Data data1 = extendedData.getDatas().get(1);
    final Data data2 = extendedData.getDatas().get(2);
    assertEquals("holeNumber", data0.getName());
    assertEquals("1", data0.getValue());
    assertEquals("holeYardage", data1.getName());
    assertEquals("234", data1.getValue());
    assertEquals("holePar", data2.getName());
    assertEquals("4", data2.getValue());
}
Also used : ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) Data(org.geotoolkit.data.kml.model.Data) Kml(org.geotoolkit.data.kml.model.Kml) File(java.io.File) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 3 with Data

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

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

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

the class DataTest method dataWriteTest.

@Test
public void dataWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Feature placemark = kmlFactory.createPlacemark();
    final ExtendedData extendedData = kmlFactory.createExtendedData();
    final Data data0 = kmlFactory.createData();
    data0.setName("holeNumber");
    data0.setValue("1");
    final Data data1 = kmlFactory.createData();
    data1.setName("holeYardage");
    data1.setValue("234");
    final Data data2 = kmlFactory.createData();
    data2.setName("holePar");
    data2.setValue("4");
    extendedData.setDatas(Arrays.asList(data0, data1, data2));
    placemark.setPropertyValue(KmlConstants.TAG_NAME, "Club house");
    placemark.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, extendedData);
    final Kml kml = kmlFactory.createKml(null, placemark, null, null);
    final File temp = File.createTempFile("testData", ".kml");
    temp.deleteOnExit();
    final KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.write(kml);
    writer.dispose();
    DomCompare.compare(new File(pathToTestFile), temp);
}
Also used : ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) KmlWriter(org.geotoolkit.data.kml.xml.KmlWriter) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) Data(org.geotoolkit.data.kml.model.Data) Kml(org.geotoolkit.data.kml.model.Kml) Feature(org.opengis.feature.Feature) File(java.io.File) Test(org.junit.Test)

Aggregations

Data (org.geotoolkit.data.kml.model.Data)5 ExtendedData (org.geotoolkit.data.kml.model.ExtendedData)5 SchemaData (org.geotoolkit.data.kml.model.SchemaData)3 SimpleData (org.geotoolkit.data.kml.model.SimpleData)3 Feature (org.opengis.feature.Feature)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Kml (org.geotoolkit.data.kml.model.Kml)2 Test (org.junit.Test)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 AbstractGeometry (org.geotoolkit.data.kml.model.AbstractGeometry)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 KmlWriter (org.geotoolkit.data.kml.xml.KmlWriter)1 LineString (org.locationtech.jts.geom.LineString)1 MultiLineString (org.locationtech.jts.geom.MultiLineString)1 Property (org.opengis.feature.Property)1 PropertyType (org.opengis.feature.PropertyType)1