use of org.geotoolkit.data.kml.model.AbstractGeometry 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.AbstractGeometry in project geotoolkit by Geomatys.
the class KmlFeatureUtilities method extractFeature.
/**
* create a {@link SimpleFeature} from an {@link Map.Entry}
* @param idgeom current id iterator
* @param geometry : {@link Map.Entry} which contains a geometry
* @return a {@link SimpleFeature}
*/
private static Feature extractFeature(int idgeom, Map.Entry<Object, Map<String, String>> geometry) {
final Object geom = geometry.getKey();
Geometry finalGeom = null;
// if it's a simple geometry
if (geom instanceof Geometry) {
finalGeom = (Geometry) geometry.getKey();
try {
// if it's lineString it can cut meridian. So test it
if (finalGeom instanceof LineString) {
LineString lineString = (LineString) finalGeom;
MultiLineString multiLine = cutAtMeridian(lineString);
if (multiLine != null) {
finalGeom = multiLine;
}
}
} catch (Exception ex) {
LOGGER.log(Level.WARNING, ex.getMessage(), ex);
}
} else // it's a geometry collection
if (geom instanceof DefaultMultiGeometry) {
final DefaultMultiGeometry kmlabstractGeometry = (DefaultMultiGeometry) geom;
final List<Geometry> multiGeometry = new ArrayList<>(0);
// loop on geometry to add id on a GeometryList
for (AbstractGeometry abstractGeometry : kmlabstractGeometry.getGeometries()) {
if (abstractGeometry instanceof Geometry) {
final Geometry currentGeom = (Geometry) abstractGeometry;
multiGeometry.add(currentGeom);
}
}
final GeometryFactory gf = JTS.getFactory();
Geometry[] geometryArray = new Geometry[multiGeometry.size()];
for (int i = 0; i < multiGeometry.size(); i++) {
geometryArray[i] = multiGeometry.get(i);
}
finalGeom = new GeometryCollection(geometryArray, gf);
}
if (finalGeom != null) {
return BuildSimpleFeature(idgeom, geometry.getValue(), finalGeom);
}
return null;
}
use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.
the class KMLGraphicBuilder method legendPlacemark.
private static Image legendPlacemark(Feature placemark, KmlCache cache) throws IOException {
int nameWidth = 0;
legendCommonFeature(placemark, cache);
final String featureName = (String) placemark.getPropertyValue(KmlConstants.TAG_NAME);
if (featureName != null) {
nameWidth = FONT_METRICS.stringWidth(featureName);
}
// Apply styles
final Style s = retrieveStyle(placemark, cache);
final BufferedImage image = new BufferedImage(LEGEND_WIDTH_EXT + nameWidth, LEGEND_HEIGHT_EXT, BufferedImage.TYPE_INT_ARGB);
final Graphics2D graphic = (Graphics2D) image.getGraphics();
graphic.setFont(FONT);
final AbstractGeometry geometry = (AbstractGeometry) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
if (s != null && s.getIconStyle() != null) {
final IconStyle iconStyle = s.getIconStyle();
final BasicLink bl = iconStyle.getIcon();
if (bl != null) {
if (bl.getHref() != null) {
final URL img = new URL(bl.getHref());
final BufferedImage buff = ImageIO.read(img);
graphic.drawImage(buff, 0, 4, LEGEND_WIDTH_INT, LEGEND_HEIGHT_INT, null);
}
}
} else if (geometry instanceof LineString) {
graphic.drawImage(ICON_PLACEMARK_LINE_STRING, 0, 4, null);
} else if (geometry instanceof LinearRing) {
graphic.drawImage(ICON_PLACEMARK_LINEAR_RING, 0, 4, null);
} else if (geometry instanceof Polygon) {
graphic.drawImage(ICON_PLACEMARK_POLYGON, 0, 4, null);
} else {
graphic.drawImage(ICON_PLACEMARK, 0, 4, null);
}
if (featureName != null) {
graphic.setColor(Color.BLACK);
graphic.drawString(featureName, LEGEND_WIDTH_EXT, LEGEND_HEIGHT_INT);
}
return image;
}
use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.
the class KmlReader method readMultiGeometry.
private MultiGeometry readMultiGeometry() throws XMLStreamException, KmlException, URISyntaxException {
// AbstractObject
List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
IdAttributes idAttributes = readIdAttributes();
// AbstractGeometry
List<SimpleTypeContainer> abstractGeometrySimpleExtensions = new ArrayList<>();
List<Object> abstractGeometryObjectExtensions = new ArrayList<>();
// Multi Geometry
List<AbstractGeometry> geometries = new ArrayList<>();
List<SimpleTypeContainer> multiGeometrySimpleExtensions = new ArrayList<>();
List<Object> multiGeometryObjectExtensions = new ArrayList<>();
boucle: while (reader.hasNext()) {
switch(reader.next()) {
case XMLStreamConstants.START_ELEMENT:
{
final String eName = reader.getLocalName();
final String eUri = reader.getNamespaceURI();
// KML
if (equalsNamespace(eUri)) {
if (isAbstractGeometry(eName)) {
geometries.add(readAbstractGeometry(eName));
}
} else // EXTENSIONS
{
KmlExtensionReader r;
if ((r = this.getComplexExtensionReader(TAG_MULTI_GEOMETRY, eUri, eName)) != null) {
Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_MULTI_GEOMETRY, eUri, eName);
Object ext = result.getKey();
Extensions.Names extensionLevel = result.getValue();
if (Extensions.Names.GEOMETRY.equals(extensionLevel)) {
abstractGeometryObjectExtensions.add(ext);
} else if (Extensions.Names.MULTI_GEOMETRY.equals(extensionLevel)) {
multiGeometryObjectExtensions.add(ext);
} else if (extensionLevel == null) {
if (ext instanceof AbstractGeometry) {
geometries.add((AbstractGeometry) ext);
}
}
} else if ((r = getSimpleExtensionReader(TAG_MULTI_GEOMETRY, eUri, eName)) != null) {
Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_MULTI_GEOMETRY, eUri, eName);
Object ext = result.getKey();
Extensions.Names extensionLevel = result.getValue();
if (Extensions.Names.OBJECT.equals(extensionLevel)) {
objectSimpleExtensions.add((SimpleTypeContainer) ext);
} else if (Extensions.Names.GEOMETRY.equals(extensionLevel)) {
abstractGeometrySimpleExtensions.add((SimpleTypeContainer) ext);
} else if (Extensions.Names.MULTI_GEOMETRY.equals(extensionLevel)) {
multiGeometrySimpleExtensions.add((SimpleTypeContainer) ext);
}
}
}
break;
}
case XMLStreamConstants.END_ELEMENT:
{
if (TAG_MULTI_GEOMETRY.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
break boucle;
}
break;
}
}
}
return KmlReader.KML_FACTORY.createMultiGeometry(objectSimpleExtensions, idAttributes, abstractGeometrySimpleExtensions, abstractGeometryObjectExtensions, geometries, multiGeometrySimpleExtensions, multiGeometryObjectExtensions);
}
use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.
the class KmlReader method readPlacemark.
private Feature readPlacemark() throws XMLStreamException, KmlException, URISyntaxException {
// AbstractObject
List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<SimpleTypeContainer>();
IdAttributes idAttributes = readIdAttributes();
// AbstractFeature
String name = null;
boolean visibility = DEF_VISIBILITY;
boolean open = DEF_OPEN;
AtomPersonConstruct author = null;
AtomLink link = null;
String address = null;
AddressDetails addressDetails = null;
String phoneNumber = null;
Object snippet = null;
Object description = null;
AbstractView view = null;
AbstractTimePrimitive timePrimitive = null;
URI styleUrl = null;
List<AbstractStyleSelector> styleSelector = new ArrayList<AbstractStyleSelector>();
Region region = null;
Object extendedData = null;
List<SimpleTypeContainer> featureSimpleExtensions = new ArrayList<SimpleTypeContainer>();
List<Object> featureObjectExtensions = new ArrayList<Object>();
// Placemark
AbstractGeometry abstractGeometry = null;
List<SimpleTypeContainer> placemarkSimpleExtensions = new ArrayList<SimpleTypeContainer>();
List<Object> placemarkObjectExtensions = new ArrayList<Object>();
boucle: while (reader.hasNext()) {
switch(reader.next()) {
case XMLStreamConstants.START_ELEMENT:
final String eName = reader.getLocalName();
final String eUri = reader.getNamespaceURI();
// KML
if (equalsNamespace(eUri)) {
switch(eName) {
case TAG_NAME:
name = reader.getElementText();
break;
case TAG_VISIBILITY:
visibility = parseBoolean(reader.getElementText());
break;
case TAG_OPEN:
open = parseBoolean(reader.getElementText());
break;
case TAG_ADDRESS:
address = reader.getElementText();
break;
case TAG_PHONE_NUMBER:
phoneNumber = reader.getElementText();
break;
case TAG_SNIPPET:
snippet = readElementText();
break;
case TAG_SNIPPET_BIG:
snippet = readSnippet();
break;
case TAG_DESCRIPTION:
description = readElementText();
break;
case TAG_STYLE_URL:
styleUrl = new URI(reader.getElementText());
break;
case TAG_REGION:
region = readRegion();
break;
case TAG_EXTENDED_DATA:
extendedData = readExtendedData();
break;
case TAG_META_DATA:
extendedData = readMetaData();
break;
default:
{
if (isAbstractView(eName)) {
view = readAbstractView(eName);
} else if (isAbstractStyleSelector(eName)) {
styleSelector.add(readAbstractStyleSelector(eName));
} else if (isAbstractTimePrimitive(eName)) {
timePrimitive = readAbstractTimePrimitive(eName);
} else if (isAbstractGeometry(eName)) {
abstractGeometry = readAbstractGeometry(eName);
}
break;
}
}
} else // ATOM
if (URI_ATOM.equals(eUri)) {
checkVersion(URI_KML_2_2);
// ABSTRACT FEATURE
if (TAG_ATOM_AUTHOR.equals(eName)) {
author = readAtomPersonConstruct();
} else if (TAG_ATOM_LINK.equals(eName)) {
link = readAtomLink();
}
} else // XAL
if (URI_XAL.equals(eUri)) {
checkVersion(URI_KML_2_2);
// ABSTRACT FEATURE
if (TAG_XAL_ADDRESS_DETAILS.equals(eName)) {
addressDetails = readXalAddressDetails();
}
} else // EXTENSIONS
{
KmlExtensionReader r;
if ((r = this.getComplexExtensionReader(TAG_PLACEMARK, eUri, eName)) != null) {
Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_PLACEMARK, eUri, eName);
Object ext = result.getKey();
Extensions.Names extensionLevel = result.getValue();
if (Extensions.Names.FEATURE.equals(extensionLevel)) {
featureObjectExtensions.add(ext);
} else if (Extensions.Names.PLACEMARK.equals(extensionLevel)) {
placemarkObjectExtensions.add(ext);
} else if (extensionLevel == null) {
if (ext instanceof AbstractGeometry) {
abstractGeometry = (AbstractGeometry) ext;
}
}
} else if ((r = getSimpleExtensionReader(TAG_PLACEMARK, eUri, eName)) != null) {
Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_PLACEMARK, eUri, eName);
Object ext = result.getKey();
Extensions.Names extensionLevel = result.getValue();
if (Extensions.Names.OBJECT.equals(extensionLevel)) {
objectSimpleExtensions.add((SimpleTypeContainer) ext);
} else if (Extensions.Names.FEATURE.equals(extensionLevel)) {
featureSimpleExtensions.add((SimpleTypeContainer) ext);
} else if (Extensions.Names.PLACEMARK.equals(extensionLevel)) {
placemarkSimpleExtensions.add((SimpleTypeContainer) ext);
}
}
}
break;
case XMLStreamConstants.END_ELEMENT:
if (TAG_PLACEMARK.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
break boucle;
}
break;
}
}
return KmlReader.KML_FACTORY.createPlacemark(objectSimpleExtensions, idAttributes, name, visibility, open, author, link, address, addressDetails, phoneNumber, snippet, description, view, timePrimitive, styleUrl, styleSelector, region, extendedData, featureSimpleExtensions, featureObjectExtensions, abstractGeometry, placemarkSimpleExtensions, placemarkObjectExtensions);
}
Aggregations