use of eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser 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.instance.helper.DepthFirstInstanceTraverser in project hale by halestudio.
the class SpatialIndexInstanceProcessor method process.
/**
* @see eu.esdihumboldt.hale.common.instance.processing.InstanceProcessor#process(eu.esdihumboldt.hale.common.instance.model.Instance,
* eu.esdihumboldt.hale.common.instance.model.InstanceReference)
*/
@Override
public void process(Instance instance, InstanceReference reference) {
SpatialIndexService<Localizable, Localizable> index = getSpatialIndexService();
final GeometryFinder finder = new GeometryFinder(null);
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
traverser.traverse(instance, finder);
final List<Geometry> geometries = new ArrayList<>();
for (GeometryProperty<?> property : finder.getGeometries()) {
Geometry g = property.getGeometry();
for (int i = 0; i < g.getNumGeometries(); i++) {
geometries.add(g.getGeometryN(i));
}
}
final BoundingBox boundingBox = new BoundingBox();
for (Geometry geometry : geometries) {
boundingBox.add(BoundingBox.compute(geometry));
}
if (boundingBox.checkIntegrity()) {
TypedInstanceReference typedRef = new TypedInstanceReference(reference, instance.getDefinition());
index.insert(new LocalizableInstanceReference(typedRef, boundingBox));
}
}
Aggregations