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