use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class ExtentTransformation method calculateExtent.
/**
* Calculate the extent of a set of geometries.
*
* @param geometries the geometries or instances containing geometries
* @param type the type of extent to calculate
* @return the calculated extent
* @throws TransformationException if source geometries don't have a common
* CRS
* @throws NoResultException if the result extent would be <code>null</code>
*/
public static GeometryProperty<?> calculateExtent(Iterable<?> geometries, ExtentType type) throws TransformationException, NoResultException {
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
GeometryFactory fact = new GeometryFactory();
CRSDefinition commonCrs = null;
Geometry[] geomsCollectingArray = new Geometry[SIMULTAN_PROCESS_GEOMS];
short geomsCollectedIdx = 0;
for (Object value : geometries) {
traverser.traverse(value, geoFind);
for (GeometryProperty<?> geom : geoFind.getGeometries()) {
// no CRS or one common CRS is OK
if (commonCrs == null) {
commonCrs = geom.getCRSDefinition();
} else {
if (geom.getCRSDefinition() != null && !geom.getCRSDefinition().equals(commonCrs)) {
// CRS doesn't match
throw new TransformationException("Source geometries don't have a common CRS.");
}
}
Geometry g = geom.getGeometry();
// If geometry collecting array not filled.
if (geomsCollectedIdx < SIMULTAN_PROCESS_GEOMS - 1) {
geomsCollectingArray[geomsCollectedIdx++] = g;
} else // Geometry collecting array filled.
{
// add last
geomsCollectingArray[geomsCollectedIdx] = g;
// geometry
GeometryCollection gc = new GeometryCollection(geomsCollectingArray, fact);
geomsCollectingArray[0] = resolveParam(gc, type);
geomsCollectedIdx = 1;
}
}
geoFind.reset();
}
Geometry extent = resolveParam(new GeometryCollection(Arrays.copyOfRange(geomsCollectingArray, 0, geomsCollectedIdx), fact), type);
if (extent != null) {
return new DefaultGeometryProperty<Geometry>(commonCrs, extent);
}
throw new NoResultException();
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class InteriorPoint method calculateInteriorPoint.
/**
* Calculate an interior point for a given geometry or object holding a
* geometry.
*
* @param geometryHolder {@link Geometry}, {@link GeometryProperty} or
* {@link Instance} holding a geometry
* @return an interior point of the geometry
* @throws TransformationException if the interior point could not be
* calculated
*/
public static GeometryProperty<?> calculateInteriorPoint(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;
// use the first geometry encountered
int index = 0;
Geometry geom = null;
while (geom == null && index < geoms.size()) {
geom = geoms.get(index).getGeometry();
oldCRS = geoms.get(index).getCRSDefinition();
index++;
}
if (geom != null) {
try {
result = geom.getInteriorPoint();
} catch (TopologyException e) {
// calculate the point for a geometry with a small buffer to
// avoid error with polygons that have overlapping lines
result = geom.buffer(0.000001).getInteriorPoint();
if (!result.within(geom)) {
// geometry
throw new TransformationException("Could not determine interior point for geometry");
}
}
} else {
return null;
}
return new DefaultGeometryProperty<Geometry>(oldCRS, result);
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class CircleByCenterPointHandler method createGeometry.
@Override
public Object createGeometry(Instance instance, int srsDimension, IOProvider reader) throws GeometryNotSupportedException {
PointHandler handler = new PointHandler();
// XXX support for different types of line strings in one instance (we
// support only one type per instance!)
Coordinate[] controlCoord = null;
double radius = 0;
// to parse coordinates of a line string
// for use with GML 2, 3, 3.1, 3.2
Collection<Object> values = PropertyResolver.getValues(instance, "coordinates", false);
if (values != null && !values.isEmpty()) {
Object value = values.iterator().next();
if (value instanceof Instance) {
try {
controlCoord = GMLGeometryUtil.parseCoordinates((Instance) value);
} catch (ParseException e) {
throw new GeometryNotSupportedException("Could not parse coordinates", e);
}
}
}
// for use with GML 3, 3.2
if (controlCoord == null || controlCoord.length == 0) {
values = PropertyResolver.getValues(instance, "pos", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
List<Coordinate> cs = new ArrayList<Coordinate>();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
Coordinate c = GMLGeometryUtil.parseDirectPosition((Instance) value);
if (c != null) {
cs.add(c);
}
}
}
controlCoord = cs.toArray(new Coordinate[cs.size()]);
}
}
// for use with GML 3.1, 3.2
if (controlCoord == null || controlCoord.length == 0) {
values = PropertyResolver.getValues(instance, "posList", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
Object value = iterator.next();
if (value instanceof Instance) {
controlCoord = GMLGeometryUtil.parsePosList((Instance) value, srsDimension);
}
}
}
if (controlCoord == null || controlCoord.length == 0) {
values = PropertyResolver.getValues(instance, "pointRep.Point", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
List<Coordinate> cs = new ArrayList<Coordinate>();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
try {
@SuppressWarnings("unchecked") DefaultGeometryProperty<Point> point = (DefaultGeometryProperty<Point>) handler.createGeometry((Instance) value, srsDimension, reader);
cs.add(point.getGeometry().getCoordinate());
} catch (GeometryNotSupportedException e) {
throw new GeometryNotSupportedException("Could not parse Point Representation", e);
}
}
}
controlCoord = cs.toArray(new Coordinate[cs.size()]);
}
}
// for use with GML 3.1
if (controlCoord == null || controlCoord.length == 0) {
values = PropertyResolver.getValues(instance, "pointProperty.Point", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
List<Coordinate> cs = new ArrayList<Coordinate>();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
try {
@SuppressWarnings("unchecked") DefaultGeometryProperty<Point> point = (DefaultGeometryProperty<Point>) handler.createGeometry((Instance) value, srsDimension, reader);
cs.add(point.getGeometry().getCoordinate());
} catch (GeometryNotSupportedException e) {
throw new GeometryNotSupportedException("Could not parse Point Property", e);
}
}
}
controlCoord = cs.toArray(new Coordinate[cs.size()]);
}
}
// for use with GML2, 3, 3.1, 3.2
if (controlCoord == null || controlCoord.length == 0) {
values = PropertyResolver.getValues(instance, "coord", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
List<Coordinate> cs = new ArrayList<Coordinate>();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
Coordinate c = GMLGeometryUtil.parseCoord((Instance) value);
if (c != null) {
cs.add(c);
}
}
}
controlCoord = cs.toArray(new Coordinate[cs.size()]);
}
}
values = PropertyResolver.getValues(instance, "radius", false);
if (values != null && !values.isEmpty()) {
Object value = values.iterator().next();
if (value instanceof Instance) {
// ##TODO :: need to check with real time data
radius = ConversionUtil.getAs(((Instance) value).getValue(), Double.class);
}
}
if (controlCoord != null && controlCoord.length != 0 && radius != 0) {
CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
// create Arc representing a circle
Arc arc = new ArcByCenterPointImpl(controlCoord[0], radius, Angle.fromDegrees(0), Angle.fromDegrees(0), true);
// get interpolation algorithm
InterpolationAlgorithm interpol = InterpolationHelper.getInterpolation(reader, getGeometryFactory());
LineString interpolatedCircle = interpol.interpolateArc(arc);
if (interpolatedCircle == null) {
log.error("Circle could be not interpolated to Linestring");
return null;
}
return new DefaultGeometryProperty<LineString>(crsDef, interpolatedCircle);
}
throw new GeometryNotSupportedException();
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class EnvelopeHandler method createGeometry.
/**
* @see GeometryHandler#createGeometry(Instance, int, IOProvider)
*/
@Override
public Object createGeometry(Instance instance, int srsDimension, IOProvider reader) throws GeometryNotSupportedException {
MultiPoint envelope = null;
List<Point> points = new ArrayList<Point>();
Collection<Object> values = PropertyResolver.getValues(instance, "coordinates", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
try {
Coordinate[] cs = GMLGeometryUtil.parseCoordinates((Instance) value);
if (cs != null && cs.length > 0) {
points.add(getGeometryFactory().createPoint(cs[0]));
}
} catch (ParseException e) {
throw new GeometryNotSupportedException("Could not parse coordinates", e);
}
}
}
}
if (points.isEmpty()) {
values = PropertyResolver.getValues(instance, "pos", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
Coordinate c = GMLGeometryUtil.parseDirectPosition((Instance) value);
if (c != null) {
points.add(getGeometryFactory().createPoint(c));
}
}
}
}
}
if (points.isEmpty()) {
values = PropertyResolver.getValues(instance, "coord", false);
if (values != null && !values.isEmpty()) {
Iterator<Object> iterator = values.iterator();
while (iterator.hasNext()) {
Object value = iterator.next();
if (value instanceof Instance) {
Coordinate c = GMLGeometryUtil.parseCoord((Instance) value);
if (c != null) {
points.add(getGeometryFactory().createPoint(c));
}
}
}
}
}
if (!points.isEmpty()) {
Coordinate[] coordinates = new Coordinate[] { points.get(0).getCoordinate(), points.get(1).getCoordinate() };
coordinates = InterpolationHelper.moveCoordinates(reader, coordinates);
envelope = getGeometryFactory().createMultiPoint(coordinates);
}
if (envelope != null) {
CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
return new DefaultGeometryProperty<MultiPoint>(crsDef, envelope);
}
throw new GeometryNotSupportedException();
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition 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();
}
Aggregations