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