use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class SpatiaLiteGeometries method convertToInstanceGeometry.
@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, SQLiteConnection connection, Supplier<CRSDefinition> crsProvider, SimpleLog log) throws Exception {
// show error and abort if SpatiaLite is not available
if (!SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, true)) {
// don't throw, will prevent any data being loaded
// throw new IllegalStateException("SpatiaLite module is not available");
}
// decode geometry read from DB
GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
Geometry jtsGeom = decodeGeometryValue(geom, columnTypeMetadata, connection);
// determine CRS
CRSDefinition crsDef = null;
String authName = columnTypeMetadata.getAuthName();
if (authName != null && authName.equalsIgnoreCase("EPSG")) {
String epsgCode = authName + ":" + columnTypeMetadata.getSrs();
crsDef = new CodeDefinition(epsgCode, null);
} else {
String wkt = columnTypeMetadata.getSrsText();
if (wkt != null) {
crsDef = new WKTDefinition(wkt, null);
}
}
return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
}
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();
}
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 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();
}
Aggregations