Search in sources :

Example 6 with GeometryProperty

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);
}
Also used : DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) TransformationException(eu.esdihumboldt.hale.common.align.transformation.function.TransformationException) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) Geometry(com.vividsolutions.jts.geom.Geometry) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)

Example 7 with GeometryProperty

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;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) BufferParameters(com.vividsolutions.jts.operation.buffer.BufferParameters) BufferBuilder(com.vividsolutions.jts.operation.buffer.BufferBuilder) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) Nullable(javax.annotation.Nullable)

Example 8 with GeometryProperty

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();
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) ArrayList(java.util.ArrayList) Collection(java.util.Collection)

Example 9 with GeometryProperty

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);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) QName(javax.xml.namespace.QName) Schema(eu.esdihumboldt.hale.common.schema.model.Schema) InstanceCollection(eu.esdihumboldt.hale.common.instance.model.InstanceCollection) TypeFilter(eu.esdihumboldt.hale.common.instance.model.TypeFilter) Test(org.junit.Test)

Example 10 with GeometryProperty

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);
        }
    }
}
Also used : GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) TypeDefinition(eu.esdihumboldt.hale.common.schema.model.TypeDefinition)

Aggregations

GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)25 Geometry (com.vividsolutions.jts.geom.Geometry)17 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)11 ArrayList (java.util.ArrayList)11 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)8 CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)8 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)7 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)7 Polygon (com.vividsolutions.jts.geom.Polygon)5 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)5 InstanceCollection (eu.esdihumboldt.hale.common.instance.model.InstanceCollection)4 Collection (java.util.Collection)4 LinearRing (com.vividsolutions.jts.geom.LinearRing)3 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)3 Point (com.vividsolutions.jts.geom.Point)3 PropertyValue (eu.esdihumboldt.hale.common.align.transformation.function.PropertyValue)3 GeometryNotSupportedException (eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException)3 QName (javax.xml.namespace.QName)3 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)3