use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class GeoSpatialBound method merge.
public void merge(final GeoSpatialBound other) {
addDate(other.dateStart);
addDate(other.dateEnd);
addXCoordinate(other.minx);
addXCoordinate(other.maxx);
addYCoordinate(other.miny);
addYCoordinate(other.maxy);
for (AbstractGeometry geom : other.geometries) {
if (!this.geometries.contains(geom)) {
this.geometries.add(geom);
}
}
this.historicalLocation.putAll(other.historicalLocation);
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class GeoSpatialBound method appendLocation.
public void appendLocation(final TemporalObject time, final AnyFeature feature) {
Date d = addTime(time);
AbstractGeometry ageom = null;
if (feature instanceof SamplingFeature) {
final SamplingFeature sf = (SamplingFeature) feature;
final Geometry geom = sf.getGeometry();
if (geom instanceof AbstractGeometry) {
ageom = (AbstractGeometry) geom;
} else if (geom != null) {
ageom = GMLUtilities.getGMLFromISO(geom);
}
addGeometry(ageom);
extractBoundary(ageom);
}
if (d != null && ageom != null) {
historicalLocation.put(d, ageom);
}
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class JAXPStreamFeatureWriter method setId.
/**
* @param inc auto increment value, ids must be unique
*/
private void setId(AbstractGeometry gmlGeometry, String id) {
if (gmlGeometry.getId() == null || gmlGeometry.getId().isEmpty()) {
// do not override ids if they exist
gmlGeometry.setId(id + (gidInc));
gidInc++;
}
if (gmlGeometry instanceof MultiCurve) {
for (CurveProperty po : ((MultiCurve) gmlGeometry).getCurveMember()) {
final AbstractCurve child = po.getAbstractCurve();
if (child instanceof AbstractGeometry) {
setId((AbstractGeometry) child, id);
}
}
} else if (gmlGeometry instanceof MultiSurface) {
for (SurfaceProperty po : ((MultiSurface) gmlGeometry).getSurfaceMember()) {
final AbstractSurface child = po.getAbstractSurface();
if (child instanceof AbstractGeometry) {
setId((AbstractGeometry) child, id);
}
}
} else if (gmlGeometry instanceof MultiGeometryType) {
for (GeometryPropertyType po : ((MultiGeometryType) gmlGeometry).getGeometryMember()) {
final AbstractGeometryType child = po.getAbstractGeometry();
if (child instanceof AbstractGeometry) {
setId((AbstractGeometry) child, id);
}
}
} else if (gmlGeometry instanceof MultiSolidType) {
for (SolidPropertyType po : ((MultiSolidType) gmlGeometry).getSolidMember()) {
final AbstractSolidType child = po.getAbstractSolid().getValue();
if (child instanceof AbstractGeometry) {
setId((AbstractGeometry) child, id);
}
}
} else if (gmlGeometry instanceof MultiPointType) {
for (PointPropertyType po : ((MultiPointType) gmlGeometry).getPointMember()) {
final PointType child = po.getPoint();
if (child instanceof AbstractGeometry) {
setId((AbstractGeometry) child, id);
}
}
}
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class JAXPStreamFeatureWriter method writeProperty.
private void writeProperty(Feature parent, PropertyType typeA, Object valueA, String id) throws XMLStreamException {
final GenericName nameA = typeA.getName();
if (AttributeConvention.contains(nameA) || isAttributeProperty(nameA))
return;
final String nameProperty = nameA.tip().toString();
String namespaceProperty = getNamespace(nameA);
final boolean hasChars = typeA instanceof AttributeType && !((AttributeType) typeA).characteristics().isEmpty();
if (typeA instanceof FeatureAssociationRole && valueA instanceof Feature) {
final FeatureAssociationRole far = (FeatureAssociationRole) typeA;
final Feature ca = (Feature) valueA;
final FeatureType valueType = far.getValueType();
// write feature
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, nameProperty);
} else {
writer.writeStartElement(nameProperty);
}
// nill case
final Object isNil = Utils.isNill(ca);
if (isNil != null) {
writeAttributeProperties(ca);
writer.writeEndElement();
return;
}
// check if we are working on a gml:ReferenceType
if ("AbstractGMLType".equals(valueType.getName().tip().toString())) {
// write and xlink href in local file
Object valueId = ca.getPropertyValue(AttributeConvention.IDENTIFIER);
writer.writeAttribute(GMLConvention.XLINK_NAMESPACE, "href", "#" + valueId);
writer.writeEndElement();
return;
}
final String gmlid = getId(ca, null);
/*
Note : the GML 3.2 identifier element is this only one which does not
follow the OGC 'PropertyType' pattern and is not encapsulated.
Note : if more cases are found, a more generic approach should be used.
*/
boolean encapsulate = GMLConvention.isDecoratedProperty(far);
if (encapsulate) {
// we need to encapsulate type
final String encapName = Utils.getNameWithoutTypeSuffix(valueType.getName().tip().toString());
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, encapName);
} else {
writer.writeStartElement(encapName);
}
if (gmlid != null) {
writer.writeAttribute("gml", gmlNamespace, "id", gmlid);
}
writeAttributeProperties(ca);
writeComplexProperties(ca, getId(ca, id));
// close encapsulation
writer.writeEndElement();
} else {
if (gmlid != null) {
writer.writeAttribute("gml", gmlNamespace, "id", gmlid);
}
writeAttributeProperties(ca);
writeComplexProperties(ca, getId(ca, id));
}
writer.writeEndElement();
} else if (valueA instanceof Collection && !(AttributeConvention.isGeometryAttribute(typeA))) {
for (Object value : (Collection) valueA) {
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, nameProperty);
} else {
writer.writeStartElement(nameProperty);
}
if (hasChars)
writeCharacteristics((Attribute) parent.getProperty(nameA.toString()));
writer.writeCharacters(Utils.getStringValue(value));
writer.writeEndElement();
}
} else if (valueA != null && valueA.getClass().isArray() && !(AttributeConvention.isGeometryAttribute(typeA))) {
final int length = Array.getLength(valueA);
for (int i = 0; i < length; i++) {
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, nameProperty);
} else {
writer.writeStartElement(nameProperty);
}
if (hasChars)
writeCharacteristics((Attribute) parent.getProperty(nameA.toString()));
final Object value = Array.get(valueA, i);
final String textValue;
if (value != null && value.getClass().isArray()) {
// matrix
final StringBuilder sb = new StringBuilder();
final int length2 = Array.getLength(value);
for (int j = 0; j < length2; j++) {
final Object subValue = Array.get(value, j);
sb.append(Utils.getStringValue(subValue)).append(" ");
}
textValue = sb.toString();
} else {
textValue = Utils.getStringValue(value);
}
writer.writeCharacters(textValue);
writer.writeEndElement();
}
} else if (valueA instanceof Map && !(AttributeConvention.isGeometryAttribute(typeA))) {
final Map<?, ?> map = (Map) valueA;
for (Entry<?, ?> entry : map.entrySet()) {
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, nameProperty);
} else {
writer.writeStartElement(nameProperty);
}
if (hasChars)
writeCharacteristics((Attribute) parent.getProperty(nameA.toString()));
final Object key = entry.getKey();
if (key != null) {
writer.writeAttribute("name", (String) key);
}
writer.writeCharacters(Utils.getStringValue(entry.getValue()));
writer.writeEndElement();
}
} else if (!(AttributeConvention.isGeometryAttribute(typeA))) {
if (valueA instanceof Document) {
// special case for xml documents
final Document doc = (Document) valueA;
StaxUtils.writeElement(doc.getDocumentElement(), writer, false);
} else {
// simple type
String value = (valueA instanceof Property) ? null : Utils.getStringValue(valueA);
if ((nameProperty.equals("name") || nameProperty.equals("description")) && !gmlNamespace.equals(namespaceProperty)) {
namespaceProperty = gmlNamespace;
LOGGER.finer("the property name and description of a feature must have the GML namespace");
}
if (valueA instanceof Feature || value != null) {
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, nameProperty);
} else {
writer.writeStartElement(nameProperty);
}
if (hasChars)
writeCharacteristics((Attribute) parent.getProperty(nameA.toString()));
if (valueA instanceof Feature) {
// some types, like Observation & Measurement have Object types which can be
// properties again, we ensure to write then as proper xml tags
final Feature prop = (Feature) valueA;
final GenericName propName = prop.getType().getName();
final String namespaceURI = getNamespace(propName);
final String localPart = Utils.getNameWithoutTypeSuffix(propName.tip().toString());
if (namespaceURI != null && !namespaceURI.isEmpty()) {
writer.writeStartElement(namespaceURI, localPart);
} else {
writer.writeStartElement(localPart);
}
writeComplexProperties(prop, getId(prop, id));
writer.writeEndElement();
} else if (value != null) {
writer.writeCharacters(value);
}
writer.writeEndElement();
} else if (value == null && Utils.isNillable(typeA)) {
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, nameProperty);
} else {
writer.writeStartElement(nameProperty);
}
writer.writeAttribute("http://www.w3.org/2001/XMLSchema-instance", "nil", "1");
writer.writeEndElement();
}
}
// we add the geometry
} else {
if (valueA != null) {
final boolean descIsType = Utils.isGeometricType(typeA.getName()) && Utils.isGeometricType(nameA);
if (!descIsType) {
if (namespaceProperty != null && !namespaceProperty.isEmpty()) {
writer.writeStartElement(namespaceProperty, nameProperty);
} else {
writer.writeStartElement(nameProperty);
}
}
final CoordinateReferenceSystem crs = FeatureExt.getCRS(typeA);
final JAXBElement element;
final MarshallerPool POOL;
if ("3.1.1".equals(gmlVersion)) {
final Geometry isoGeometry = JTSUtils.toISO((org.locationtech.jts.geom.Geometry) valueA, crs);
element = OBJECT_FACTORY.buildAnyGeometry(isoGeometry);
POOL = GML_31_POOL;
} else if ("3.2.1".equals(gmlVersion)) {
AbstractGeometry gmlGeometry = null;
try {
gmlGeometry = JTStoGeometry.toGML(gmlVersion, (org.locationtech.jts.geom.Geometry) valueA, crs);
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, "Factory exception when transforming JTS geometry to GML binding", ex);
}
if (gmlGeometry != null) {
// id is requiered in version 3.2.1
// NOTE we often see gml where the geometry id is the same as the feature
// we use the last parent with an id, seems acceptable.
final String gid = (id + "_g").replace(':', '_');
setId(gmlGeometry, gid);
}
element = GML32_FACTORY.buildAnyGeometry(gmlGeometry);
POOL = GML_32_POOL;
} else {
throw new IllegalArgumentException("Unexpected GML version:" + gmlVersion);
}
try {
final Marshaller marshaller;
marshaller = POOL.acquireMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshal(marshaller, element);
POOL.recycle(marshaller);
} catch (JAXBException ex) {
LOGGER.log(Level.WARNING, "JAXB Exception while marshalling the iso geometry: " + ex.getMessage(), ex);
}
if (!descIsType)
writer.writeEndElement();
}
}
}
use of org.geotoolkit.gml.xml.AbstractGeometry in project geotoolkit by Geomatys.
the class JAXPStreamValueCollectionWriter method writeFeature.
/**
* Write the feature into the stream.
*
* @param feature The feature
* @throws XMLStreamException
*/
private void writeFeature(final Feature feature) throws XMLStreamException {
final FeatureType type = feature.getType();
// write properties in the type order
Expression exp = FilterUtilities.FF.property(valueReference);
Object valueA = exp.apply(feature);
if (valueA instanceof Collection) {
for (Object value : (Collection) valueA) {
writer.writeStartElement("wfs", "member", WFS_NAMESPACE);
writeValue(value);
writer.writeEndElement();
}
} else if (valueA instanceof Map) {
final Map<?, ?> map = (Map) valueA;
for (Map.Entry<?, ?> entry : map.entrySet()) {
writer.writeStartElement("wfs", "member", WFS_NAMESPACE);
final Object key = entry.getKey();
if (key != null) {
writer.writeAttribute("name", (String) key);
}
writeValue(entry.getValue());
writer.writeEndElement();
}
} else if (valueA != null && valueA.getClass().isArray()) {
final int length = Array.getLength(valueA);
for (int i = 0; i < length; i++) {
writer.writeStartElement("wfs", "member", WFS_NAMESPACE);
final Object value = Array.get(valueA, i);
final String textValue;
if (value != null && value.getClass().isArray()) {
// matrix
final StringBuilder sb = new StringBuilder();
final int length2 = Array.getLength(value);
for (int j = 0; j < length2; j++) {
final Object subValue = Array.get(value, j);
sb.append(Utils.getStringValue(subValue)).append(" ");
}
textValue = sb.toString();
} else {
textValue = Utils.getStringValue(value);
}
writer.writeCharacters(textValue);
writer.writeEndElement();
}
} else if (valueA instanceof org.locationtech.jts.geom.Geometry) {
writer.writeStartElement("wfs", "member", WFS_NAMESPACE);
AbstractGeometry gmlGeometry = null;
try {
gmlGeometry = JTStoGeometry.toGML("3.2.1", (org.locationtech.jts.geom.Geometry) valueA, FeatureExt.getCRS(type));
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, "Factory exception when transforming JTS geometry to GML binding", ex);
}
final JAXBElement element = GML32_FACTORY.buildAnyGeometry(gmlGeometry);
try {
final Marshaller marshaller;
marshaller = GML_32_POOL.acquireMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
marshal(marshaller, element);
GML_32_POOL.recycle(marshaller);
} catch (JAXBException ex) {
LOGGER.log(Level.WARNING, "JAXB Exception while marshalling the iso geometry: " + ex.getMessage(), ex);
}
writer.writeEndElement();
} else {
String value = Utils.getStringValue(valueA);
if (value != null) {
writer.writeStartElement("wfs", "member", WFS_NAMESPACE);
writeValue(value);
writer.writeEndElement();
}
}
}
Aggregations