use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty 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);
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class NetworkExpansion method calculateBuffer.
/**
* Calculate a buffer geometry.
*
* @param geometryHolder the geometry or object holding a geometry
* @param bufferWidth the buffer width
* @param log the transformation log, may be <code>null</code>
* @return the buffer geometry or <code>null</code>
*/
@Nullable
public static GeometryProperty<Geometry> calculateBuffer(Object geometryHolder, double bufferWidth, @Nullable TransformationLog log) {
// find contained geometries
InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
GeometryFinder geoFind = new GeometryFinder(null);
traverser.traverse(geometryHolder, geoFind);
List<GeometryProperty<?>> geometries = geoFind.getGeometries();
GeometryProperty<?> old_geometry = null;
if (!geometries.isEmpty()) {
old_geometry = geometries.get(0);
if (geometries.size() > 1) {
if (log != null) {
log.warn(log.createMessage("Multiple geometries found, but network expansion is only done on the first.", null));
}
}
}
GeometryProperty<Geometry> result = null;
if (old_geometry != null) {
Geometry new_geometry = null;
BufferParameters bufferParameters = new BufferParameters();
bufferParameters.setEndCapStyle(CAP_STYLE);
BufferBuilder bb = new BufferBuilder(new BufferParameters());
new_geometry = bb.buffer(old_geometry.getGeometry(), bufferWidth);
result = new DefaultGeometryProperty<Geometry>(old_geometry.getCRSDefinition(), new_geometry);
}
return result;
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class GeometryUtil method getGeometryProperties.
/**
* Try to get/create geometry properties from a property value.
*
* @param value the property value, e.g. a {@link Geometry},
* {@link GeometryProperty}, a {@link Collection} or
* {@link Instance}
* @return the geometry properties or an empty list if none could be created
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Collection<GeometryProperty<?>> getGeometryProperties(Object value) {
if (value instanceof Instance) {
value = ((Instance) value).getValue();
}
if (value != null) {
Collection<GeometryProperty<?>> result = new ArrayList<GeometryProperty<?>>();
if (value instanceof GeometryProperty) {
// return the encountered GeometryProperty
result.add((GeometryProperty) value);
}
if (value instanceof Geometry) {
// create a GeometryProperty wrapping the geometry
// XXX any way to determine a CRS?
GeometryProperty prop = new DefaultGeometryProperty(null, (Geometry) value);
result.add(prop);
}
if (value instanceof Collection<?>) {
// add results from collection values
for (Object subValue : ((Iterable<?>) value)) {
result.addAll(getGeometryProperties(subValue));
}
}
return result;
}
return Collections.emptyList();
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class MsSqlDataReaderTest method testInstanceReader.
/**
* Test instance reader
*
* @throws Exception if error occurred in reading instances
*/
@SuppressWarnings("unchecked")
@Test
public void testInstanceReader() throws Exception {
// ****** read Schema ******//
Schema schema = readSchema();
assertEquals(3, schema.getMappingRelevantTypes().size());
// ****** read Instances ******//
InstanceCollection instances = readInstances(schema);
assertTrue(instances.hasSize());
assertEquals(SOURCE_TOTAL_INSTANCES_COUNT, instances.size());
InstanceCollection filteredInstances = null;
// Check SpatialTable "jdbc:sqlserver:dbo","SpatialTable"
filteredInstances = instances.select(new TypeFilter(schema.getType(new QName("jdbc:sqlserver:dbo", "SpatialTable"))));
int geometryPropertyCount = 0;
int idPropertyCount = 0;
ResourceIterator<Instance> it = filteredInstances.iterator();
while (it.hasNext()) {
Instance in = it.next();
for (String propertyName : SOURCE_GEOMETRY_TYPE_PROPERTY_NAMES) {
Object value = in.getProperty(QName.valueOf(propertyName))[0];
if (value == null)
continue;
if (value instanceof GeometryProperty) {
assertTrue(((GeometryProperty<Geometry>) value).getGeometry().toText().equalsIgnoreCase(String.valueOf(PROPERTY_GEO_VALUES[geometryPropertyCount])));
geometryPropertyCount++;
} else {
assertTrue(((int) value) == ((int) PROPERTY_ID_VALUES[idPropertyCount]));
idPropertyCount++;
}
}
}
assertEquals(SOURCE_GEOMETRY_INSTANCE_COUNT, geometryPropertyCount);
assertEquals(SOURCE_GEOMETRY_INSTANCE_COUNT, idPropertyCount);
}
use of eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty in project hale by halestudio.
the class SpatiaLiteTestSuite method checkInstances.
/**
* Checks the property definitions and values of the provided instances.
* Values will be checked for just one instance (the one with
* {@link #PROPERTY_ID_NAME} = {@link #PROPERTY_ID_VALUE}).
*
* @param instances the instances to check
* @param propertyMap the expected property names / values
*/
@SuppressWarnings("rawtypes")
public void checkInstances(InstanceCollection instances, Map<String, Object> propertyMap) {
// get type to check property definition
TypeDefinition type = instances.iterator().next().getDefinition();
checkType(type, SOUURCE_TYPE_LOCAL_NAME, propertyMap.keySet());
// Check the values of Instance with ID = 1
Instance instance = null;
Iterator<Instance> instanceIterator = instances.iterator();
while (instanceIterator.hasNext()) {
Instance currentInstance = instanceIterator.next();
Integer id = (Integer) currentInstance.getProperty(QName.valueOf(PROPERTY_ID_NAME))[0];
if (PROPERTY_ID_VALUE.equals(id)) {
instance = currentInstance;
break;
}
}
if (instance == null) {
fail(String.format("No instance found with %s = %s", PROPERTY_ID_NAME, PROPERTY_ID_VALUE));
}
for (String propertyName : propertyMap.keySet()) {
@SuppressWarnings("null") Object value = instance.getProperty(QName.valueOf(propertyName))[0];
if (value instanceof GeometryProperty) {
assertTrue(((Geometry) propertyMap.get(propertyName)).equalsExact(((GeometryProperty) value).getGeometry(), 0.000001));
} else {
assertEquals(propertyMap.get(propertyName), value);
}
}
}
Aggregations