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);
}
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;
}
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);
}
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));
}
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());
}
Aggregations