Search in sources :

Example 1 with AbstractGeometry

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);
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry)

Example 2 with AbstractGeometry

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);
    }
}
Also used : Geometry(org.opengis.geometry.Geometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) SamplingFeature(org.geotoolkit.sampling.xml.SamplingFeature) Date(java.util.Date)

Example 3 with AbstractGeometry

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);
            }
        }
    }
}
Also used : AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) MultiSurface(org.geotoolkit.gml.xml.MultiSurface) AbstractGeometryType(org.geotoolkit.gml.xml.v321.AbstractGeometryType) MultiGeometryType(org.geotoolkit.gml.xml.v321.MultiGeometryType) MultiPointType(org.geotoolkit.gml.xml.v321.MultiPointType) AbstractSurface(org.geotoolkit.gml.xml.AbstractSurface) CurveProperty(org.geotoolkit.gml.xml.CurveProperty) AbstractSolidType(org.geotoolkit.gml.xml.v321.AbstractSolidType) MultiCurve(org.geotoolkit.gml.xml.MultiCurve) SurfaceProperty(org.geotoolkit.gml.xml.SurfaceProperty) SolidPropertyType(org.geotoolkit.gml.xml.v321.SolidPropertyType) PointType(org.geotoolkit.gml.xml.v321.PointType) MultiPointType(org.geotoolkit.gml.xml.v321.MultiPointType) AbstractCurve(org.geotoolkit.gml.xml.AbstractCurve) PointPropertyType(org.geotoolkit.gml.xml.v321.PointPropertyType) GeometryPropertyType(org.geotoolkit.gml.xml.v321.GeometryPropertyType) MultiSolidType(org.geotoolkit.gml.xml.v321.MultiSolidType)

Example 4 with AbstractGeometry

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();
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Attribute(org.opengis.feature.Attribute) FactoryException(org.opengis.util.FactoryException) Utils.buildSchemaLocationString(org.geotoolkit.feature.xml.Utils.buildSchemaLocationString) MarshallerPool(org.apache.sis.xml.MarshallerPool) JTSWrapperMarshallerPool(org.geotoolkit.internal.jaxb.JTSWrapperMarshallerPool) GMLMarshallerPool(org.geotoolkit.gml.xml.GMLMarshallerPool) Document(org.w3c.dom.Document) Feature(org.opengis.feature.Feature) GenericName(org.opengis.util.GenericName) Entry(java.util.Map.Entry) AttributeType(org.opengis.feature.AttributeType) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Utils.isAttributeProperty(org.geotoolkit.feature.xml.Utils.isAttributeProperty) Property(org.opengis.feature.Property) CurveProperty(org.geotoolkit.gml.xml.CurveProperty) SurfaceProperty(org.geotoolkit.gml.xml.SurfaceProperty) Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) Geometry(org.opengis.geometry.Geometry) JTStoGeometry(org.geotoolkit.gml.JTStoGeometry) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) Collection(java.util.Collection) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 5 with AbstractGeometry

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();
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Marshaller(javax.xml.bind.Marshaller) AbstractGeometry(org.geotoolkit.gml.xml.AbstractGeometry) FactoryException(org.opengis.util.FactoryException) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) Expression(org.opengis.filter.Expression) Collection(java.util.Collection) Map(java.util.Map)

Aggregations

AbstractGeometry (org.geotoolkit.gml.xml.AbstractGeometry)20 Geometry (org.locationtech.jts.geom.Geometry)14 FactoryException (org.opengis.util.FactoryException)11 JAXBElement (javax.xml.bind.JAXBElement)7 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)7 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 JAXBException (javax.xml.bind.JAXBException)4 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)4 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)4 LineString (org.locationtech.jts.geom.LineString)4 MultiLineString (org.locationtech.jts.geom.MultiLineString)4 MultiPolygon (org.locationtech.jts.geom.MultiPolygon)4 Polygon (org.locationtech.jts.geom.Polygon)4 Map (java.util.Map)3 JTStoGeometry (org.geotoolkit.gml.JTStoGeometry)3 CurveProperty (org.geotoolkit.gml.xml.CurveProperty)3 SurfaceProperty (org.geotoolkit.gml.xml.SurfaceProperty)3 GeoJSONGeometry (org.geotoolkit.internal.geojson.binding.GeoJSONGeometry)3 GeoJSONObject (org.geotoolkit.internal.geojson.binding.GeoJSONObject)3