use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class InstanceMarker method doPaintMarker.
/**
* @see BoundingBoxMarker#doPaintMarker(Graphics2D, SelectableWaypoint,
* PixelConverter, int, int, int, int, int, Rectangle, boolean)
*/
@Override
protected Area doPaintMarker(Graphics2D g, InstanceWaypoint context, PixelConverter converter, int zoom, int minX, int minY, int maxX, int maxY, Rectangle gBounds, boolean calulateArea) {
List<Area> areas = (!calulateArea) ? (null) : (new ArrayList<Area>());
List<GeometryProperty<?>> geometries = context.getGeometries();
// map CRS
CoordinateReferenceSystem mapCRS;
try {
mapCRS = CRSDecode.getLonLatCRS(converter.getMapEpsg());
// map (GeoPosition) assumes lon/lat order
} catch (Throwable e) {
log.error("Could not decode map CRS", e);
return null;
}
// paint each geometry
for (GeometryProperty<?> geometry : geometries) {
Area geometryArea = paintGeometry(g, geometry.getCRSDefinition(), geometry.getGeometry(), context, converter, zoom, geometries.size() == 1, gBounds, mapCRS, calulateArea);
if (areas != null && geometryArea != null) {
areas.add(geometryArea);
}
}
if (areas == null) {
return null;
}
if (areas.size() == 1) {
return areas.get(0);
} else if (!areas.isEmpty()) {
return new MultiArea(areas);
}
return null;
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class CalculateLength 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 length = 0;
for (GeometryProperty<?> geoProp : geoms) {
length += geoProp.getGeometry().getLength();
}
return length;
} else {
geom = geoms.get(0).getGeometry();
}
if (geom != null) {
return geom.getLength();
} else {
throw new TransformationException("Geometry for calculate length could not be retrieved.");
}
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class ReprojectGeometry method evaluate.
@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("source").get(0);
Object inputValue = input.getValue();
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(inputValue, geoFind);
List<GeometryProperty<?>> geoms = geoFind.getGeometries();
Geometry sourceGeometry = geoms.get(0).getGeometry();
CRSDefinition crsDef = geoms.get(0).getCRSDefinition();
if (crsDef == null) {
throw new TransformationException("Geometry does not have an associated Coordinate Reference System");
}
CoordinateReferenceSystem sourceCRS = crsDef.getCRS();
Geometry resultGeometry = sourceGeometry;
CoordinateReferenceSystem targetCRS = sourceCRS;
// Get input parameter
String srs = getParameterChecked(PARAMETER_REFERENCE_SYSTEM).as(String.class);
if (srs != null) {
try {
targetCRS = parseReferenceSystemParamter(srs);
} catch (Exception e) {
throw new TransformationException("Error determining destination Cordinate Reference System.", e);
}
// Retrieve transformation from cell context, or create a new
// instance
Map<Object, Object> cellContext = getExecutionContext().getCellContext();
MathTransform transform = getOrCreateMathTransform(sourceCRS, targetCRS, cellContext);
// Apply transformation
try {
resultGeometry = JTS.transform(sourceGeometry, transform);
} catch (MismatchedDimensionException | TransformException e) {
throw new TransformationException("Problem on execute transformation from: " + sourceCRS + " to " + targetCRS, e);
}
}
return new DefaultGeometryProperty<Geometry>(new CodeDefinition(CRS.toSRS(targetCRS), targetCRS), resultGeometry);
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty 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.GeometryProperty in project hale by halestudio.
the class AbstractHandlerTest method checkSingleGeometry.
/**
* Check a single geometry contained in an instance (at an arbitrary path).
*
* @param instance the geometry instance
* @param checker the checker (should throw an exception when the check
* fails)
* @return the collection of encountered geometries
*/
protected Collection<GeometryProperty<?>> checkSingleGeometry(Instance instance, @Nullable Consumer<Geometry> checker) {
GeometryFinder finder = new GeometryFinder(null);
BreadthFirstInstanceTraverser traverser = new BreadthFirstInstanceTraverser();
traverser.traverse(instance, finder);
List<GeometryProperty<?>> geoms = finder.getGeometries();
assertFalse("No geometry found in instances", geoms.isEmpty());
assertEquals("More than one geometry found in instance", 1, geoms.size());
Geometry geom = geoms.get(0).getGeometry();
if (checker != null) {
checker.accept(geom);
}
return geoms;
}
Aggregations