use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition 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.schema.geometry.CRSDefinition 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.schema.geometry.CRSDefinition in project hale by halestudio.
the class CRSHelperFunctionsTest method testFromWKT.
@Test
public void testFromWKT() {
String wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";
CRSDefinition crs = CRSHelperFunctions._fromWKT(wkt);
assertNotNull(crs);
assertNotNull(crs.getCRS());
assertTrue(crs instanceof WKTDefinition);
assertEquals(wkt, ((WKTDefinition) crs).getWkt());
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.
the class CRSHelperFunctionsTest method testFromCode.
@Test
public void testFromCode() {
String code = "EPSG:4326";
Map<String, Object> args = new HashMap<>();
args.put("code", code);
CRSDefinition crs = CRSHelperFunctions._from(args);
assertNotNull(crs);
assertNotNull(crs.getCRS());
assertTrue(crs instanceof CodeDefinition);
assertEquals(code, ((CodeDefinition) crs).getCode());
}
use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition 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);
}
Aggregations