Search in sources :

Example 6 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.

the class PointHandler method createGeometry.

/**
 * @see GeometryHandler#createGeometry(Instance, int, IOProvider)
 */
@Override
public Object createGeometry(Instance instance, int srsDimension, IOProvider reader) throws GeometryNotSupportedException {
    Point point = null;
    // Point is either defined by a CoordinatesType named coordinates
    Collection<Object> values = PropertyResolver.getValues(instance, "coordinates", false);
    if (values != null && !values.isEmpty()) {
        Object value = values.iterator().next();
        if (value instanceof Instance) {
            try {
                Coordinate[] cs = GMLGeometryUtil.parseCoordinates((Instance) value);
                if (cs != null && cs.length > 0) {
                    cs = InterpolationHelper.moveCoordinates(reader, cs);
                    point = getGeometryFactory().createPoint(cs[0]);
                }
            } catch (ParseException e) {
                throw new GeometryNotSupportedException("Could not parse coordinates", e);
            }
        }
    }
    // or by a DirectPositionType named pos
    if (point == null) {
        values = PropertyResolver.getValues(instance, "pos", false);
        if (values != null && !values.isEmpty()) {
            Object value = values.iterator().next();
            if (value instanceof Instance) {
                Coordinate c = GMLGeometryUtil.parseDirectPosition((Instance) value);
                if (c != null) {
                    c = InterpolationHelper.moveCoordinate(reader, c);
                    point = getGeometryFactory().createPoint(c);
                }
            }
        }
    }
    // or even by a CoordType in GML 2
    if (point == null) {
        values = PropertyResolver.getValues(instance, "coord", false);
        if (values != null && !values.isEmpty()) {
            Object value = values.iterator().next();
            if (value instanceof Instance) {
                Coordinate c = GMLGeometryUtil.parseCoord((Instance) value);
                if (c != null) {
                    c = InterpolationHelper.moveCoordinate(reader, c);
                    point = getGeometryFactory().createPoint(c);
                }
            }
        }
    }
    if (point != null) {
        CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
        return new DefaultGeometryProperty<Point>(crsDef, point);
    }
    // XXX
    throw new GeometryNotSupportedException();
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) Coordinate(com.vividsolutions.jts.geom.Coordinate) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) Point(com.vividsolutions.jts.geom.Point) ParseException(java.text.ParseException)

Example 7 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition 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();
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) ArrayList(java.util.ArrayList) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) LinearRing(com.vividsolutions.jts.geom.LinearRing) Polygon(com.vividsolutions.jts.geom.Polygon)

Example 8 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.

the class CRSHelperFunctionsTest method testFromWKT.

@Test
public void testFromWKT() {
    String wkt = "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]";
    CRSDefinition crs = CRSHelperFunctions._fromWKT(wkt);
    assertNotNull(crs);
    assertNotNull(crs.getCRS());
    assertTrue(crs instanceof WKTDefinition);
    assertEquals(wkt, ((WKTDefinition) crs).getWkt());
}
Also used : CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition) Test(org.junit.Test)

Example 9 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition in project hale by halestudio.

the class CRSHelperFunctionsTest method testFromCode.

@Test
public void testFromCode() {
    String code = "EPSG:4326";
    Map<String, Object> args = new HashMap<>();
    args.put("code", code);
    CRSDefinition crs = CRSHelperFunctions._from(args);
    assertNotNull(crs);
    assertNotNull(crs.getCRS());
    assertTrue(crs instanceof CodeDefinition);
    assertEquals(code, ((CodeDefinition) crs).getCode());
}
Also used : HashMap(java.util.HashMap) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) Test(org.junit.Test)

Example 10 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition 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)

Aggregations

CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)30 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)20 Geometry (com.vividsolutions.jts.geom.Geometry)13 Point (com.vividsolutions.jts.geom.Point)9 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)8 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)8 GeometryNotSupportedException (eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException)8 ArrayList (java.util.ArrayList)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)7 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 GeometryFinder (eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder)6 DepthFirstInstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)6 InstanceTraverser (eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser)6 LineString (com.vividsolutions.jts.geom.LineString)5 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)5 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)5 ParseException (java.text.ParseException)5 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)3 LinearRing (com.vividsolutions.jts.geom.LinearRing)3 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)3