Search in sources :

Example 1 with ExtendedData

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

the class GxReader method readTrack.

/**
 * @return
 * @throws XMLStreamException
 * @throws KmlException
 * @throws URISyntaxException
 */
public Track readTrack() throws XMLStreamException, KmlException, URISyntaxException {
    AltitudeMode altitudeMode = DEF_ALTITUDE_MODE;
    List<Calendar> whens = new ArrayList<Calendar>();
    List<Coordinate> coordinates = new ArrayList<Coordinate>();
    List<Angles> anglesList = new ArrayList<Angles>();
    Model model = null;
    ExtendedData extendedData = null;
    boucle: while (reader.hasNext()) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                final String eName = reader.getLocalName();
                final String eUri = reader.getNamespaceURI();
                if (URI_GX.equals(eUri)) {
                    if (TAG_ALTITUDE_MODE.equals(eName)) {
                        altitudeMode = this.readAltitudeMode();
                    } else if (TAG_COORD.equals(eName)) {
                        coordinates.add(this.readCoord());
                    } else if (TAG_ANGLES.equals(eName)) {
                        anglesList.add(this.readAngles());
                    }
                } else if (this.kmlReader.getVersionUri().equals(eUri)) {
                    if (KmlConstants.TAG_ALTITUDE_MODE.equals(eName)) {
                        altitudeMode = kmlReader.readAltitudeMode();
                    } else if (KmlConstants.TAG_WHEN.equals(eName)) {
                        whens.add(kmlReader.readCalendar());
                    } else if (KmlConstants.TAG_MODEL.equals(eName)) {
                        model = kmlReader.readModel();
                    } else if (KmlConstants.TAG_EXTENDED_DATA.equals(eName)) {
                        extendedData = kmlReader.readExtendedData();
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (TAG_TRACK.equals(reader.getLocalName()) && URI_GX.contains(reader.getNamespaceURI())) {
                    break boucle;
                }
                break;
        }
    }
    CoordinateSequence coord = KML_FACTORY.createCoordinates(coordinates);
    return GX_FACTORY.createTrack(altitudeMode, whens, coord, anglesList, model, extendedData);
}
Also used : CoordinateSequence(org.locationtech.jts.geom.CoordinateSequence) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) Coordinate(org.locationtech.jts.geom.Coordinate) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Model(org.geotoolkit.data.kml.model.Model) Angles(org.geotoolkit.data.gx.model.Angles) EnumAltitudeMode(org.geotoolkit.data.gx.model.EnumAltitudeMode) AltitudeMode(org.geotoolkit.data.kml.model.AltitudeMode)

Example 2 with ExtendedData

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

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

the class DataRW2Test method metadataWriteTest.

@Test
public void metadataWriteTest() throws KmlException, IOException, XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException {
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Feature placemark0 = kmlFactory.createPlacemark();
    final ExtendedData extendedData = kmlFactory.createExtendedData();
    final List<String> racine0 = new ArrayList<>();
    racine0.add("Je suis un element.");
    racine0.add("J'en suis un autre.");
    extendedData.setAnyOtherElements(Arrays.asList((Object) racine0));
    placemark0.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, extendedData);
    final Feature placemark1 = kmlFactory.createPlacemark();
    final Metadata metadata = kmlFactory.createMetadata();
    final List<String> racine1 = new ArrayList<>();
    racine1.add("Et moi aussi.");
    racine1.add("Sans commentaire.");
    metadata.setContent(Arrays.asList((Object) racine1));
    placemark1.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, metadata);
    final Feature document = kmlFactory.createDocument();
    document.setPropertyValue(KmlConstants.TAG_FEATURES, Arrays.asList(placemark0, placemark1));
    document.setPropertyValue(KmlConstants.TAG_OPEN, Boolean.TRUE);
    document.setPropertyValue(KmlConstants.TAG_NAME, "Document.kml");
    final Kml kml = kmlFactory.createKml(null, document, null, null);
    kml.addExtensionUri("http://www.sandres.com", "sam");
    final File temp = File.createTempFile("testMetadata", ".kml");
    temp.deleteOnExit();
    final DataWriter dataWriter = new DataWriter();
    final KmlWriter writer = new KmlWriter();
    writer.setOutput(temp);
    writer.addDataWriter("http://www.sandres.com", dataWriter);
    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) ArrayList(java.util.ArrayList) Metadata(org.geotoolkit.data.kml.model.Metadata) Kml(org.geotoolkit.data.kml.model.Kml) Feature(org.opengis.feature.Feature) File(java.io.File) Test(org.junit.Test)

Example 4 with ExtendedData

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

the class DataRW2Test method metadataReadTest.

@Test
public void metadataReadTest() throws IOException, XMLStreamException, KmlException, URISyntaxException {
    final DataReader dataReader = new DataReader();
    final KmlReader reader = new KmlReader();
    reader.setInput(new File(pathToTestFile));
    reader.addDataReader(dataReader);
    final Kml kmlObjects = reader.read();
    reader.dispose();
    final Feature document = kmlObjects.getAbstractFeature();
    assertEquals(KmlModelConstants.TYPE_DOCUMENT, document.getType());
    assertEquals("Document.kml", document.getPropertyValue(KmlConstants.TAG_NAME));
    assertEquals(Boolean.TRUE, document.getPropertyValue(KmlConstants.TAG_OPEN));
    Iterator<?> i = ((Iterable<?>) document.getPropertyValue(KmlConstants.TAG_FEATURES)).iterator();
    assertTrue("Expected at least one element.", i.hasNext());
    final Feature placemark0 = (Feature) i.next();
    ExtendedData extendedData = (ExtendedData) ((List) placemark0.getPropertyValue(KmlConstants.TAG_EXTENDED_DATA)).get(0);
    assertEquals(EMPTY_LIST, extendedData.getDatas());
    assertEquals(EMPTY_LIST, extendedData.getSchemaData());
    assertEquals(1, extendedData.getAnyOtherElements().size());
    List<?> racine1 = (List<?>) extendedData.getAnyOtherElements().get(0);
    assertEquals(2, racine1.size());
    assertEquals("Je suis un element.", racine1.get(0));
    assertEquals("J'en suis un autre.", racine1.get(1));
    assertTrue("Expected at least 2 elements.", i.hasNext());
    final Feature placemark1 = (Feature) i.next();
    Metadata metadata = (Metadata) ((List) placemark1.getPropertyValue(KmlConstants.TAG_EXTENDED_DATA)).get(0);
    assertEquals(1, metadata.getContent().size());
    List<?> racine2 = (List<?>) metadata.getContent().get(0);
    assertEquals(2, racine2.size());
    assertEquals("Et moi aussi.", racine2.get(0));
    assertEquals("Sans commentaire.", racine2.get(1));
}
Also used : ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) Metadata(org.geotoolkit.data.kml.model.Metadata) KmlReader(org.geotoolkit.data.kml.xml.KmlReader) ArrayList(java.util.ArrayList) List(java.util.List) Kml(org.geotoolkit.data.kml.model.Kml) File(java.io.File) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 5 with ExtendedData

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

Aggregations

ExtendedData (org.geotoolkit.data.kml.model.ExtendedData)12 Feature (org.opengis.feature.Feature)9 File (java.io.File)8 Kml (org.geotoolkit.data.kml.model.Kml)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 Data (org.geotoolkit.data.kml.model.Data)5 SchemaData (org.geotoolkit.data.kml.model.SchemaData)5 SimpleData (org.geotoolkit.data.kml.model.SimpleData)5 KmlReader (org.geotoolkit.data.kml.xml.KmlReader)4 KmlWriter (org.geotoolkit.data.kml.xml.KmlWriter)4 List (java.util.List)3 Metadata (org.geotoolkit.data.kml.model.Metadata)3 Coordinate (org.locationtech.jts.geom.Coordinate)3 CoordinateSequence (org.locationtech.jts.geom.CoordinateSequence)3 URI (java.net.URI)2 Point (org.geotoolkit.data.kml.model.Point)2 URISyntaxException (java.net.URISyntaxException)1 Calendar (java.util.Calendar)1 Angles (org.geotoolkit.data.gx.model.Angles)1