use of eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty in project hale by halestudio.
the class PointHandler method createGeometry.
/**
* @see GeometryHandler#createGeometry(Instance, int, IOProvider)
*/
@Override
public Object createGeometry(Instance instance, int srsDimension, IOProvider reader) throws GeometryNotSupportedException {
Point point = null;
// Point is either defined by a CoordinatesType named coordinates
Collection<Object> values = PropertyResolver.getValues(instance, "coordinates", false);
if (values != null && !values.isEmpty()) {
Object value = values.iterator().next();
if (value instanceof Instance) {
try {
Coordinate[] cs = GMLGeometryUtil.parseCoordinates((Instance) value);
if (cs != null && cs.length > 0) {
cs = InterpolationHelper.moveCoordinates(reader, cs);
point = getGeometryFactory().createPoint(cs[0]);
}
} catch (ParseException e) {
throw new GeometryNotSupportedException("Could not parse coordinates", e);
}
}
}
// or by a DirectPositionType named pos
if (point == null) {
values = PropertyResolver.getValues(instance, "pos", false);
if (values != null && !values.isEmpty()) {
Object value = values.iterator().next();
if (value instanceof Instance) {
Coordinate c = GMLGeometryUtil.parseDirectPosition((Instance) value);
if (c != null) {
c = InterpolationHelper.moveCoordinate(reader, c);
point = getGeometryFactory().createPoint(c);
}
}
}
}
// or even by a CoordType in GML 2
if (point == null) {
values = PropertyResolver.getValues(instance, "coord", false);
if (values != null && !values.isEmpty()) {
Object value = values.iterator().next();
if (value instanceof Instance) {
Coordinate c = GMLGeometryUtil.parseCoord((Instance) value);
if (c != null) {
c = InterpolationHelper.moveCoordinate(reader, c);
point = getGeometryFactory().createPoint(c);
}
}
}
}
if (point != null) {
CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
return new DefaultGeometryProperty<Point>(crsDef, point);
}
// XXX
throw new GeometryNotSupportedException();
}
use of eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty in project hale by halestudio.
the class PolygonHandler method createGeometry.
/**
* @see GeometryHandler#createGeometry(Instance, int, IOProvider)
*/
@SuppressWarnings("unchecked")
@Override
public Object createGeometry(Instance instance, int srsDimension, IOProvider reader) throws GeometryNotSupportedException {
LinearRing[] holes = null;
Polygon polygon = null;
CRSDefinition crs = null;
// for use with GML 2
// to parse outer linear rings
Collection<Object> values = PropertyResolver.getValues(instance, "outerBoundaryIs.LinearRing", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
List<LinearRing> outerRing = new ArrayList<>(1);
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
// outerRing must be a
// GeometryProperty<LinearRing> instance
GeometryProperty<LinearRing> ring = (GeometryProperty<LinearRing>) ((Instance) value).getValue();
outerRing.add(ring.getGeometry());
crs = checkCommonCrs(crs, ring.getCRSDefinition());
}
}
// to parse inner linear rings
values = PropertyResolver.getValues(instance, "innerBoundaryIs.LinearRing", false);
if (values != null && !values.isEmpty()) {
iterator = values.iterator();
List<LinearRing> innerRings = new ArrayList<LinearRing>();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
// innerRings have to be a
// GeometryProperty<LinearRing> instance
GeometryProperty<LinearRing> ring = (GeometryProperty<LinearRing>) ((Instance) value).getValue();
innerRings.add(ring.getGeometry());
crs = checkCommonCrs(crs, ring.getCRSDefinition());
}
}
holes = innerRings.toArray(new LinearRing[innerRings.size()]);
}
polygon = getGeometryFactory().createPolygon(outerRing.get(0), holes);
}
// to parse inner linear rings
if (polygon == null) {
values = PropertyResolver.getValues(instance, "interior.LinearRing", false);
Collection<Object> ringValues = PropertyResolver.getValues(instance, "interior.Ring", false);
values = combineCollections(values, ringValues);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
List<LinearRing> innerRings = new ArrayList<LinearRing>();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
// innerRings have to be a
// GeometryProperty<LinearRing> instance
GeometryProperty<LinearRing> ring = (GeometryProperty<LinearRing>) ((Instance) value).getValue();
innerRings.add(ring.getGeometry());
crs = checkCommonCrs(crs, ring.getCRSDefinition());
}
}
holes = innerRings.toArray(new LinearRing[innerRings.size()]);
}
// to parse outer linear rings
values = PropertyResolver.getValues(instance, "exterior.LinearRing", false);
ringValues = PropertyResolver.getValues(instance, "exterior.Ring", false);
values = combineCollections(values, ringValues);
List<LinearRing> outerRing = new ArrayList<>(1);
if (values != null && !values.isEmpty()) {
LinearRing outer = null;
Iterator<Object> iterator = values.iterator();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
// outerRing must be a
// GeometryProperty<LinearRing> instance
GeometryProperty<LinearRing> ring = (GeometryProperty<LinearRing>) ((Instance) value).getValue();
outer = ring.getGeometry();
crs = checkCommonCrs(crs, ring.getCRSDefinition());
}
}
outerRing.add(outer);
polygon = getGeometryFactory().createPolygon(outerRing.get(0), holes);
}
}
if (polygon != null) {
if (crs == null) {
crs = GMLGeometryUtil.findCRS(instance);
}
return new DefaultGeometryProperty<Polygon>(crs, polygon);
}
throw new GeometryNotSupportedException();
}
use of eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty in project hale by halestudio.
the class Centroid method calculateCentroid.
/**
* Calculate the centroid for a given geometry or object holding a geometry.
*
* @param geometryHolder {@link Geometry}, {@link GeometryProperty} or
* {@link Instance} holding a geometry
* @return the centroid of the geometry
* @throws TransformationException if the no geometry could be extracted
* from the input
*/
public static GeometryProperty<?> calculateCentroid(Object geometryHolder) throws TransformationException {
// depth first traverser that on cancel continues traversal but w/o the
// children of the current object
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(geometryHolder, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry result;
CRSDefinition oldCRS = null;
if (geoms.size() > 1) {
// multiple geometries -> create a multi point
// XXX is this the desired behavior?
Point[] centroids = new Point[geoms.size()];
GeometryFactory geomFactory = new GeometryFactory();
for (int i = 0; i < geoms.size(); i++) {
GeometryProperty<?> prop = geoms.get(i);
centroids[i] = prop.getGeometry().getCentroid();
if (oldCRS == null) {
// assume the same CRS for all points
oldCRS = prop.getCRSDefinition();
}
}
result = geomFactory.createMultiPoint(centroids);
} else {
Geometry geom = geoms.get(0).getGeometry();
oldCRS = geoms.get(0).getCRSDefinition();
if (geom != null) {
result = geom.getCentroid();
} else {
throw new TransformationException("Geometry for centroid could not be retrieved.");
}
}
return new DefaultGeometryProperty<Geometry>(oldCRS, result);
}
use of eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty in project hale by halestudio.
the class NetworkExpansion method calculateBuffer.
/**
* Calculate a buffer geometry.
*
* @param geometryHolder the geometry or object holding a geometry
* @param bufferWidth the buffer width
* @param log the transformation log, may be <code>null</code>
* @return the buffer geometry or <code>null</code>
*/
@Nullable
public static GeometryProperty<Geometry> calculateBuffer(Object geometryHolder, double bufferWidth, @Nullable TransformationLog log) {
// find contained geometries
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(geometryHolder, geoFind);
List<GeometryProperty<?>> geometries = geoFind.getGeometries();
GeometryProperty<?> old_geometry = null;
if (!geometries.isEmpty()) {
old_geometry = geometries.get(0);
if (geometries.size() > 1) {
if (log != null) {
log.warn(log.createMessage("Multiple geometries found, but network expansion is only done on the first.", null));
}
}
}
GeometryProperty<Geometry> result = null;
if (old_geometry != null) {
Geometry new_geometry = null;
BufferParameters bufferParameters = new BufferParameters();
bufferParameters.setEndCapStyle(CAP_STYLE);
BufferBuilder bb = new BufferBuilder(new BufferParameters());
new_geometry = bb.buffer(old_geometry.getGeometry(), bufferWidth);
result = new DefaultGeometryProperty<Geometry>(old_geometry.getCRSDefinition(), new_geometry);
}
return result;
}
use of eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty in project hale by halestudio.
the class OSerializationHelper method deserialize.
/**
* Deserialize a serialized value wrapped in the given document.
*
* @param doc the document
* @param parent the parent group
* @param childName the name of the child the value is associated to
* @return the deserialized value
*/
public static Object deserialize(ODocument doc, OGroup parent, QName childName) {
int serType = doc.field(FIELD_SERIALIZATION_TYPE);
switch(serType) {
case SERIALIZATION_TYPE_STRING:
{
// convert a string value back to its original form
Object val = doc.field(FIELD_STRING_VALUE);
String stringVal = (val == null) ? (null) : (val.toString());
ConvertProxy cp = CONVERTER_IDS.getObject((String) doc.field(FIELD_CONVERT_ID));
if (cp != null) {
return cp.convert(stringVal);
}
return stringVal;
}
case SERIALIZATION_TYPE_COLLECTION:
{
// recreate collection
Object val = doc.field(FIELD_VALUES);
Object typeVal = doc.field(FIELD_COLLECTION_TYPE);
CollectionType type = (typeVal != null) ? (CollectionType.valueOf(typeVal.toString())) : (CollectionType.LIST);
if (val instanceof Collection<?>) {
Collection<?> values = (Collection<?>) val;
Collection<Object> target = createCollection(type);
for (Object element : values) {
Object convElement = convertFromDB(element, parent, childName);
target.add(convElement);
}
return target;
}
}
break;
}
ORecordBytes record = (ORecordBytes) doc.field(BINARY_WRAPPER_FIELD);
Object result;
switch(serType) {
case SERIALIZATION_TYPE_BYTEARRAY:
result = record.toStream();
break;
case SERIALIZATION_TYPE_GEOM:
case SERIALIZATION_TYPE_GEOM_PROP:
ExtendedWKBReader reader = new ExtendedWKBReader();
try {
result = reader.read(record.toStream());
} catch (ParseException e1) {
throw new IllegalStateException("Unable to parse WKB to restore geometry", e1);
}
break;
case SERIALIZATION_TYPE_JAVA:
default:
ByteArrayInputStream bytes = new ByteArrayInputStream(record.toStream());
try {
ObjectInputStream in = new ObjectInputStream(bytes) {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
Class<?> result = resolved.get(desc.getName());
if (result == null) {
result = OsgiUtils.loadClass(desc.getName(), null);
if (resolved.size() > 200) {
resolved.entrySet().iterator().remove();
}
resolved.put(desc.getName(), result);
}
if (result == null) {
throw new IllegalStateException("Class " + desc.getName() + " not found");
}
return result;
}
};
result = in.readObject();
} catch (Exception e) {
throw new IllegalStateException("Could not deserialize field value.", e);
}
break;
}
if (serType == SERIALIZATION_TYPE_GEOM_PROP) {
// wrap geometry in geometry property
// determine CRS
CRSDefinition crs = null;
Object crsId = doc.field(FIELD_CRS_ID);
if (crsId != null) {
crs = CRS_IDS.getObject(crsId.toString());
}
// create geometry property
GeometryProperty<Geometry> prop = new DefaultGeometryProperty<Geometry>(crs, (Geometry) result);
return prop;
}
return result;
}
Aggregations