Search in sources :

Example 21 with GeometryProperty

use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.

the class GenericGeometryHandler method initConstraints.

/**
 * @see FixedConstraintsGeometryHandler#initConstraints()
 */
@Override
protected Collection<? extends TypeConstraint> initConstraints() {
    Collection<TypeConstraint> constraints = new ArrayList<TypeConstraint>(3);
    // binding is collection, as we can't be sure that all contained
    // geometries share the same CRS
    constraints.add(Binding.get(Collection.class));
    // set element type binding to GeometryProperty
    constraints.add(ElementType.get(GeometryProperty.class));
    // geometry binding is Geometry, as we can't narrow it down further
    constraints.add(GeometryType.get(Geometry.class));
    // set geometry factory constraint
    constraints.add(new GeometryFactory(this));
    return constraints;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) GeometryFactory(eu.esdihumboldt.hale.io.gml.geometry.constraint.GeometryFactory) TypeConstraint(eu.esdihumboldt.hale.common.schema.model.TypeConstraint) ArrayList(java.util.ArrayList) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) Collection(java.util.Collection)

Example 22 with GeometryProperty

use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.

the class LinearRingHandler method createGeometry.

/**
 * Create a {@link LinearRing} geometry from the given instance.
 *
 * @param instance the instance
 * @param srsDimension the SRS dimension
 * @param allowTryOtherDimension if trying another dimension is allowed on
 *            failure (e.g. 3D instead of 2D)
 * @param reader the I/O Provider to get value
 * @return the {@link LinearRing} geometry
 * @throws GeometryNotSupportedException if the type definition doesn't
 *             represent a geometry type supported by the handler
 */
protected GeometryProperty<LinearRing> createGeometry(Instance instance, int srsDimension, boolean allowTryOtherDimension, IOProvider reader) throws GeometryNotSupportedException {
    LinearRing ring = null;
    LineStringHandler handler = new LineStringHandler();
    // for use with GML 2, 3, 3.1, 3.2
    @SuppressWarnings("unchecked") DefaultGeometryProperty<LineString> linestring = (DefaultGeometryProperty<LineString>) handler.createGeometry(instance, srsDimension, reader);
    try {
        ring = getGeometryFactory().createLinearRing(linestring.getGeometry().getCoordinates());
    } catch (IllegalArgumentException e) {
        if (allowTryOtherDimension) {
            // the error
            // "Points of LinearRing do not form a closed linestring"
            // can be an expression of a wrong dimension being used
            // we try an alternative, to be sure (e.g. 3D instead of 2D)
            int alternativeDimension = (srsDimension == 2) ? (3) : (2);
            GeometryProperty<LinearRing> geom = createGeometry(instance, alternativeDimension, false, reader);
            log.debug("Assuming geometry is " + alternativeDimension + "-dimensional.");
            return geom;
        }
        throw e;
    }
    if (ring != null) {
        CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
        return new DefaultGeometryProperty<LinearRing>(crsDef, ring);
    }
    throw new GeometryNotSupportedException();
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) LineString(com.vividsolutions.jts.geom.LineString) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Example 23 with GeometryProperty

use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.

the class RingHandler method createGeometry.

/**
 * Create a {@link LinearRing} geometry from the given instance.
 *
 * @param instance the instance
 * @param srsDimension the SRS dimension
 * @param allowTryOtherDimension if trying another dimension is allowed on
 *            failure (e.g. 3D instead of 2D)
 * @param reader the I/O Provider to get value
 * @return the {@link LinearRing} geometry
 * @throws GeometryNotSupportedException if the type definition doesn't
 *             represent a geometry type supported by the handler
 */
protected GeometryProperty<LinearRing> createGeometry(Instance instance, int srsDimension, boolean allowTryOtherDimension, IOProvider reader) throws GeometryNotSupportedException {
    LinearRing ring = null;
    // for use with GML 2, 3, 3.1, 3.2
    // use generic geometry handler to read curveMembers as MultiLineString
    // or LineString
    Collection<GeometryProperty<?>> properties = genericHandler.createGeometry(instance, srsDimension, reader);
    if (properties != null) {
        if (properties.size() == 1) {
            // geometry could be combined
            GeometryProperty<?> prop = properties.iterator().next();
            try {
                ring = getGeometryFactory().createLinearRing(filterDuplicates(prop.getGeometry().getCoordinates()));
            } catch (IllegalArgumentException e) {
                if (allowTryOtherDimension) {
                    // the error
                    // "Points of LinearRing do not form a closed
                    // linestring"
                    // can be an expression of a wrong dimension being used
                    // we try an alternative, to be sure (e.g. 3D instead of
                    // 2D)
                    int alternativeDimension = (srsDimension == 2) ? (3) : (2);
                    GeometryProperty<LinearRing> geom = createGeometry(instance, alternativeDimension, false, reader);
                    log.debug("Assuming geometry is " + alternativeDimension + "-dimensional.");
                    return geom;
                }
                throw e;
            }
            if (ring != null) {
                CRSDefinition crsDef = prop.getCRSDefinition();
                if (crsDef == null) {
                    GMLGeometryUtil.findCRS(instance);
                }
                return new DefaultGeometryProperty<LinearRing>(crsDef, ring);
            }
        } else {
            throw new GeometryNotSupportedException("Ring components could not be combined to a geometry");
        }
    }
    throw new GeometryNotSupportedException();
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Example 24 with GeometryProperty

use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.

the class OSerializationHelper method serialize.

/**
 * Serialize and/or wrap a value not supported as field in OrientDB so it
 * can be stored in the database.
 *
 * @param value the value to serialize
 * @param log the log
 * @return the document wrapping the value
 */
public static ODocument serialize(Object value, SimpleLog log) {
    /*
		 * As collections of ORecordBytes are not supported (or rather of
		 * records that are no documents, see embeddedCollectionToStream in
		 * ORecordSerializerCSVAbstract ~578) they are wrapped in a document.
		 */
    ODocument doc = new ODocument();
    // try conversion to string first
    final ConversionService cs = HalePlatform.getService(ConversionService.class);
    if (cs != null) {
        // check if conversion allowed and possible
        if (CONV_WHITE_LIST.contains(value.getClass()) && cs.canConvert(value.getClass(), String.class) && cs.canConvert(String.class, value.getClass())) {
            String stringValue = cs.convert(value, String.class);
            ConvertProxy convert = new ConvertProxy(cs, value.getClass());
            doc.field(FIELD_CONVERT_ID, CONVERTER_IDS.getId(convert));
            doc.field(FIELD_SERIALIZATION_TYPE, SERIALIZATION_TYPE_STRING);
            doc.field(FIELD_STRING_VALUE, stringValue);
            return doc;
        }
    }
    if (value instanceof Collection) {
        CollectionType type = null;
        if (value instanceof List) {
            type = CollectionType.LIST;
        } else if (value instanceof Set) {
            type = CollectionType.SET;
        }
        if (type != null) {
            // wrap collection values
            Collection<?> elements = (Collection<?>) value;
            List<Object> values = new ArrayList<Object>();
            for (Object element : elements) {
                Object convElement = convertForDB(element, log);
                values.add(convElement);
            }
            // set values
            // XXX ok to always use EMBEDDEDLIST as type?
            doc.field(FIELD_VALUES, values, OType.EMBEDDEDLIST);
            doc.field(FIELD_SERIALIZATION_TYPE, SERIALIZATION_TYPE_COLLECTION);
            doc.field(FIELD_COLLECTION_TYPE, type.name());
            return doc;
        }
    }
    ORecordBytes record = new ORecordBytes();
    int serType = SERIALIZATION_TYPE_JAVA;
    if (value instanceof GeometryProperty<?>) {
        GeometryProperty<?> geomProp = (GeometryProperty<?>) value;
        // store (runtime) CRS ID (XXX OK as storage is temporary)
        doc.field(FIELD_CRS_ID, CRS_IDS.getId(geomProp.getCRSDefinition()));
        // extract geometry
        value = geomProp.getGeometry();
        if (value != null) {
            serType = SERIALIZATION_TYPE_GEOM_PROP;
        } else {
            return null;
        }
    }
    if (value.getClass().isArray() && value.getClass().getComponentType().equals(byte.class)) {
        // direct byte array support
        record.fromStream((byte[]) value);
        serType = SERIALIZATION_TYPE_BYTEARRAY;
    }
    if (value instanceof Geometry) {
        // serialize geometry as WKB
        Geometry geom = (Geometry) value;
        Coordinate sample = geom.getCoordinate();
        int dimension = (sample != null && !Double.isNaN(sample.z)) ? (3) : (2);
        WKBWriter writer = new ExtendedWKBWriter(dimension);
        record.fromStream(writer.write(geom));
        if (serType != SERIALIZATION_TYPE_GEOM_PROP) {
            serType = SERIALIZATION_TYPE_GEOM;
        }
    } else {
        // object serialization
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        try {
            ObjectOutputStream out = new ObjectOutputStream(bytes);
            out.writeObject(value);
        } catch (IOException e) {
            log.error("Could not serialize field value of type {0}, null value is used instead.", value.getClass().getName());
            // cannot be stored - use null value instead
            return null;
        }
        record.fromStream(bytes.toByteArray());
    }
    /*
		 * XXX Class name is set in OGroup.configureDocument, as the class name
		 * may only bet set after the database was set.
		 */
    // doc.setClassName(BINARY_WRAPPER_CLASSNAME);
    doc.field(BINARY_WRAPPER_FIELD, record);
    doc.field(FIELD_SERIALIZATION_TYPE, serType);
    return doc;
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) HashSet(java.util.HashSet) Set(java.util.Set) WKBWriter(com.vividsolutions.jts.io.WKBWriter) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) Geometry(com.vividsolutions.jts.geom.Geometry) Coordinate(com.vividsolutions.jts.geom.Coordinate) ConversionService(org.springframework.core.convert.ConversionService) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 25 with GeometryProperty

use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.

the class GeometryUtil method getGeometries.

/**
 * Get the default geometry of an instance.
 *
 * @param instance the instance
 * @param path the property path to start the search at, a <code>null</code>
 *            will yield no geometries
 * @return the default geometries or an empty collection if there is none
 */
public static Collection<GeometryProperty<?>> getGeometries(Instance instance, List<QName> path) {
    Collection<GeometryProperty<?>> geometries = new ArrayList<GeometryProperty<?>>();
    if (path == null) {
        return geometries;
    }
    // descend path and return the geometries found
    Queue<Group> parents = new LinkedList<Group>();
    parents.add(instance);
    for (int i = 0; i < path.size(); i++) {
        QName name = path.get(i);
        Queue<Group> children = new LinkedList<Group>();
        for (Group parent : parents) {
            Object[] values = parent.getProperty(name);
            if (values != null) {
                for (Object value : values) {
                    if (value instanceof Group) {
                        children.add((Group) value);
                    }
                    if (value instanceof Instance) {
                        value = ((Instance) value).getValue();
                    }
                    if (value != null && !(value instanceof Group) && i == path.size() - 1) {
                        // detect geometry values at end of path
                        // as they are not searched later on
                        Collection<GeometryProperty<?>> geoms = getGeometryProperties(value);
                        geometries.addAll(geoms);
                    }
                }
            }
        }
        // prepare for next step
        parents = children;
    }
    // early exit #1
    if (!geometries.isEmpty()) {
        // XXX is this OK in all cases?
        return geometries;
    }
    // search in those groups/instances for additional geometries
    while (!parents.isEmpty()) {
        Group parent = parents.poll();
        // add values contained in the instance
        Collection<GeometryProperty<?>> geoms = getGeometryProperties(parent);
        if (!geoms.isEmpty()) {
            geometries.addAll(geoms);
        // early exit #2
        // don't check the children as they usually are only parts of
        // the geometry found here
        } else {
            // check children for geometries
            for (QName name : parent.getPropertyNames()) {
                Object[] values = parent.getProperty(name);
                if (values != null) {
                    for (Object value : values) {
                        if (value instanceof Group) {
                            // check group later on
                            parents.add((Group) value);
                        } else {
                            // add geometries for value
                            geometries.addAll(getGeometryProperties(value));
                        }
                    }
                }
            }
        }
    }
    return geometries;
}
Also used : Group(eu.esdihumboldt.hale.common.instance.model.Group) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Aggregations

GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)25 Geometry (com.vividsolutions.jts.geom.Geometry)17 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)11 ArrayList (java.util.ArrayList)11 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)8 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)8 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)7 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)7 Polygon (com.vividsolutions.jts.geom.Polygon)5 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)5 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)4 Collection (java.util.Collection)4 LinearRing (com.vividsolutions.jts.geom.LinearRing)3 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)3 Point (com.vividsolutions.jts.geom.Point)3 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)3 GeometryNotSupportedException (eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException)3 QName (javax.xml.namespace.QName)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3