use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class SurfaceGeometryTest method testSurfaceArcsGml32_grid.
/**
* Test surface geometries read from a GML 3.2 file
*
* @throws Exception if an error occurs
*/
@Stories("Arcs")
@Test
public void testSurfaceArcsGml32_grid() throws Exception {
InstanceCollection instances = AbstractHandlerTest.loadXMLInstances(getClass().getResource("/data/gml/geom-gml32.xsd").toURI(), getClass().getResource("/data/surface/sample-surface-arcs.xml").toURI(), gridConfig);
// three instance expected
ResourceIterator<Instance> it = instances.iterator();
try {
List<GeometryProperty<?>> geoms = new ArrayList<>();
assertTrue("First sample feature missing", it.hasNext());
Instance instance1 = it.next();
geoms.addAll(checkSingleGeometry(instance1, null));
assertTrue("Second sample feature missing", it.hasNext());
Instance instance2 = it.next();
geoms.addAll(checkSingleGeometry(instance2, null));
assertTrue("Third sample feature missing", it.hasNext());
Instance instance3 = it.next();
geoms.addAll(checkSingleGeometry(instance3, null));
assertEquals("Unexpected number of geometries", 3, geoms.size());
Geometry geom1 = geoms.get(0).getGeometry();
Geometry geom2 = geoms.get(1).getGeometry();
Geometry geom3 = geoms.get(2).getGeometry();
Envelope envelope = new Envelope();
envelope.expandToInclude(geom1.getEnvelopeInternal());
envelope.expandToInclude(geom2.getEnvelopeInternal());
envelope.expandToInclude(geom3.getEnvelopeInternal());
PaintSettings settings = new PaintSettings(envelope, 1000, 10);
SVGPainter svg = new SVGPainter(settings);
svg.setColor(Color.BLACK);
svg.drawGeometry(geom1);
svg.setColor(Color.BLUE);
svg.drawGeometry(geom2);
svg.setColor(Color.RED);
svg.drawGeometry(geom3);
saveDrawing(svg);
// ensure that polygons could be created
assertTrue(Polygon.class.isAssignableFrom(geom1.getClass()));
assertTrue(Polygon.class.isAssignableFrom(geom2.getClass()));
assertTrue(Polygon.class.isAssignableFrom(geom3.getClass()));
// XXX how to test?
// assertTrue("Geometries intersect", geom1.touches(geom2));
// assertTrue("Geometries intersect", geom2.touches(geom3));
} finally {
it.close();
}
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class SurfaceGeometryTest method testSurfaceArcsGml32.
/**
* Test surface geometries read from a GML 3.2 file
*
* @throws Exception if an error occurs
*/
@Stories("Arcs")
@Test
public void testSurfaceArcsGml32() throws Exception {
InstanceCollection instances = AbstractHandlerTest.loadXMLInstances(getClass().getResource("/data/gml/geom-gml32.xsd").toURI(), getClass().getResource("/data/surface/sample-surface-arcs.xml").toURI(), InterpolationConfigurations.segment(maxPositionalError));
// three instance expected
ResourceIterator<Instance> it = instances.iterator();
try {
List<GeometryProperty<?>> geoms = new ArrayList<>();
assertTrue("First sample feature missing", it.hasNext());
Instance instance1 = it.next();
geoms.addAll(checkSingleGeometry(instance1, null));
assertTrue("Second sample feature missing", it.hasNext());
Instance instance2 = it.next();
geoms.addAll(checkSingleGeometry(instance2, null));
assertTrue("Third sample feature missing", it.hasNext());
Instance instance3 = it.next();
geoms.addAll(checkSingleGeometry(instance3, null));
assertEquals("Unexpected number of geometries", 3, geoms.size());
Geometry geom1 = geoms.get(0).getGeometry();
Geometry geom2 = geoms.get(1).getGeometry();
Geometry geom3 = geoms.get(2).getGeometry();
Envelope envelope = new Envelope();
envelope.expandToInclude(geom1.getEnvelopeInternal());
envelope.expandToInclude(geom2.getEnvelopeInternal());
envelope.expandToInclude(geom3.getEnvelopeInternal());
PaintSettings settings = new PaintSettings(envelope, 1000, 10);
SVGPainter svg = new SVGPainter(settings);
svg.setColor(Color.BLACK);
svg.drawGeometry(geom1);
svg.setColor(Color.BLUE);
svg.drawGeometry(geom2);
svg.setColor(Color.RED);
svg.drawGeometry(geom3);
saveDrawing(svg);
// ensure that polygons could be created
assertTrue(Polygon.class.isAssignableFrom(geom1.getClass()));
assertTrue(Polygon.class.isAssignableFrom(geom2.getClass()));
assertTrue(Polygon.class.isAssignableFrom(geom3.getClass()));
// XXX how to test?
// intersection area cannot be computed
// double interArea1 = geom1.intersection(geom2).getArea();
// double interArea2 = geom2.intersection(geom3).getArea();
} finally {
it.close();
}
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class GenericGeometryHandler method createGeometry.
/**
* Create a geometry value from a given instance.
*
* @param instance the instance
* @param childGeometries the child geometries found in the instance
* @param defaultCrs the definition of the default CRS for this instance
* @param reader the IO provider
* @return the geometry value derived from the instance, the return type
* should match the {@link Binding} created in
* {@link #getTypeConstraints(TypeDefinition)}.
* @throws GeometryNotSupportedException if the type definition doesn't
* represent a geometry type supported by the handler
*/
@SuppressWarnings("unused")
protected Collection<GeometryProperty<?>> createGeometry(Instance instance, List<GeometryProperty<?>> childGeometries, CRSDefinition defaultCrs, IOProvider reader) throws GeometryNotSupportedException {
List<Geometry> geomList = new ArrayList<Geometry>();
Class<? extends Geometry> commonGeomType = null;
CRSWrapper commonCrs = null;
boolean allowCombine = true;
// TODO partition based on CRS?
for (GeometryProperty<?> geomProp : childGeometries) {
if (geomProp.getGeometry() instanceof GeometryCollection) {
GeometryCollection geomCollection = (GeometryCollection) geomProp.getGeometry();
for (int i = 0; i < geomCollection.getNumGeometries(); i++) {
// find the common geometry class
Class<? extends Geometry> geometryType = geomCollection.getGeometryN(i).getClass();
if (commonGeomType == null) {
commonGeomType = geometryType;
} else if (!commonGeomType.equals(geometryType)) {
// TODO determine common type in inheritance?
commonGeomType = Geometry.class;
}
geomList.add(geomCollection.getGeometryN(i));
}
} else {
// find the common geometry class
Class<? extends Geometry> geometryType = geomProp.getGeometry().getClass();
if (commonGeomType == null) {
commonGeomType = geometryType;
} else if (!commonGeomType.equals(geometryType)) {
// find common super class
commonGeomType = findClosestCommonSuperclass(commonGeomType, geometryType);
}
geomList.add(geomProp.getGeometry());
}
// check common CRS
CRSWrapper crs = new CRSWrapper(geomProp.getCRSDefinition());
if (commonCrs == null) {
commonCrs = crs;
} else if (!commonCrs.equals(crs)) {
allowCombine = false;
}
}
if (allowCombine && commonGeomType != null) {
if (!(commonGeomType.equals(Polygon.class) || commonGeomType.equals(Point.class) || commonGeomType.equals(LineString.class))) {
// check if it is a subclass of a supported type
if (LineString.class.isAssignableFrom(commonGeomType)) {
// for instance for InterpolatedLineString
commonGeomType = LineString.class;
}
if (Point.class.isAssignableFrom(commonGeomType)) {
commonGeomType = Point.class;
}
if (Polygon.class.isAssignableFrom(commonGeomType)) {
commonGeomType = Polygon.class;
}
}
Geometry geom = null;
if (commonGeomType.equals(Polygon.class)) {
// create a MultiPolygon
Polygon[] polygons = new Polygon[geomList.size()];
for (int i = 0; i < geomList.size(); i++) {
polygons[i] = (Polygon) geomList.get(i);
}
geom = combine(polygons, reader);
} else if (commonGeomType.equals(LineString.class)) {
// create a MultiLineString
LineString[] lines = new LineString[geomList.size()];
for (int i = 0; i < geomList.size(); i++) {
lines[i] = (LineString) geomList.get(i);
}
geom = combine(lines, reader);
} else if (commonGeomType.equals(Point.class)) {
// create a MultiPoint
Point[] points = new Point[geomList.size()];
for (int i = 0; i < geomList.size(); i++) {
points[i] = (Point) geomList.get(i);
}
geom = combine(points, reader);
}
if (geom != null) {
// returned combined property
CRSDefinition crs = (commonCrs != null && commonCrs.getCrsDef() != null) ? (commonCrs.getCrsDef()) : (defaultCrs);
return Collections.<GeometryProperty<?>>singleton(new DefaultGeometryProperty<Geometry>(crs, geom));
}
}
// fall-back: return a collection of geometry properties
if (childGeometries.isEmpty()) {
return null;
}
return childGeometries;
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty 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.GeometryProperty in project hale by halestudio.
the class CalculateArea method evaluate.
/**
* @see eu.esdihumboldt.hale.common.align.transformation.function.impl.AbstractSingleTargetPropertyTransformation#evaluate(java.lang.String,
* eu.esdihumboldt.hale.common.align.transformation.engine.TransformationEngine,
* com.google.common.collect.ListMultimap, java.lang.String,
* eu.esdihumboldt.hale.common.align.model.impl.PropertyEntityDefinition,
* java.util.Map,
* eu.esdihumboldt.hale.common.align.transformation.report.TransformationLog)
*/
@Override
protected Object evaluate(String transformationIdentifier, TransformationEngine engine, ListMultimap<String, PropertyValue> variables, String resultName, PropertyEntityDefinition resultProperty, Map<String, String> executionParameters, TransformationLog log) throws TransformationException, NoResultException {
// get input geometry
PropertyValue input = variables.get(null).get(0);
Object inputValue = input.getValue();
// 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(inputValue, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry geom = null;
if (geoms.size() > 1) {
int area = 0;
for (GeometryProperty<?> geoProp : geoms) {
area += geoProp.getGeometry().getArea();
}
return area;
} else {
geom = geoms.get(0).getGeometry();
}
if (geom != null) {
return geom.getArea();
} else {
throw new TransformationException("Geometry for calculate area could not be retrieved.");
}
}
Aggregations