Search in sources :

Example 21 with CRSDefinition

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

the class ExtentTransformation method calculateExtent.

/**
 * Calculate the extent of a set of geometries.
 *
 * @param geometries the geometries or instances containing geometries
 * @param type the type of extent to calculate
 * @return the calculated extent
 * @throws TransformationException if source geometries don't have a common
 *             CRS
 * @throws NoResultException if the result extent would be <code>null</code>
 */
public static GeometryProperty<?> calculateExtent(Iterable<?> geometries, ExtentType type) throws TransformationException, NoResultException {
    InstanceTraverser traverser = new DepthFirstInstanceTraverser(true);
    GeometryFinder geoFind = new GeometryFinder(null);
    GeometryFactory fact = new GeometryFactory();
    CRSDefinition commonCrs = null;
    Geometry[] geomsCollectingArray = new Geometry[SIMULTAN_PROCESS_GEOMS];
    short geomsCollectedIdx = 0;
    for (Object value : geometries) {
        traverser.traverse(value, geoFind);
        for (GeometryProperty<?> geom : geoFind.getGeometries()) {
            // no CRS or one common CRS is OK
            if (commonCrs == null) {
                commonCrs = geom.getCRSDefinition();
            } else {
                if (geom.getCRSDefinition() != null && !geom.getCRSDefinition().equals(commonCrs)) {
                    // CRS doesn't match
                    throw new TransformationException("Source geometries don't have a common CRS.");
                }
            }
            Geometry g = geom.getGeometry();
            // If geometry collecting array not filled.
            if (geomsCollectedIdx < SIMULTAN_PROCESS_GEOMS - 1) {
                geomsCollectingArray[geomsCollectedIdx++] = g;
            } else // Geometry collecting array filled.
            {
                // add last
                geomsCollectingArray[geomsCollectedIdx] = g;
                // geometry
                GeometryCollection gc = new GeometryCollection(geomsCollectingArray, fact);
                geomsCollectingArray[0] = resolveParam(gc, type);
                geomsCollectedIdx = 1;
            }
        }
        geoFind.reset();
    }
    Geometry extent = resolveParam(new GeometryCollection(Arrays.copyOfRange(geomsCollectingArray, 0, geomsCollectedIdx), fact), type);
    if (extent != null) {
        return new DefaultGeometryProperty<Geometry>(commonCrs, extent);
    }
    throw new NoResultException();
}
Also used : DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) InstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.InstanceTraverser) 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) NoResultException(eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser) Geometry(com.vividsolutions.jts.geom.Geometry) GeometryCollection(com.vividsolutions.jts.geom.GeometryCollection) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)

Example 22 with CRSDefinition

use of eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition 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);
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) 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) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryFinder(eu.esdihumboldt.hale.common.instance.geometry.GeometryFinder) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) TopologyException(com.vividsolutions.jts.geom.TopologyException) DepthFirstInstanceTraverser(eu.esdihumboldt.hale.common.instance.helper.DepthFirstInstanceTraverser)

Example 23 with CRSDefinition

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

the class CircleByCenterPointHandler method createGeometry.

@Override
public Object createGeometry(Instance instance, int srsDimension, IOProvider reader) throws GeometryNotSupportedException {
    PointHandler handler = new PointHandler();
    // XXX support for different types of line strings in one instance (we
    // support only one type per instance!)
    Coordinate[] controlCoord = null;
    double radius = 0;
    // to parse coordinates of a line string
    // for use with GML 2, 3, 3.1, 3.2
    Collection<Object> values = PropertyResolver.getValues(instance, "coordinates", false);
    if (values != null && !values.isEmpty()) {
        Object value = values.iterator().next();
        if (value instanceof Instance) {
            try {
                controlCoord = GMLGeometryUtil.parseCoordinates((Instance) value);
            } catch (ParseException e) {
                throw new GeometryNotSupportedException("Could not parse coordinates", e);
            }
        }
    }
    // for use with GML 3, 3.2
    if (controlCoord == null || controlCoord.length == 0) {
        values = PropertyResolver.getValues(instance, "pos", false);
        if (values != null && !values.isEmpty()) {
            Iterator<Object> iterator = values.iterator();
            List<Coordinate> cs = new ArrayList<Coordinate>();
            while (iterator.hasNext()) {
                Object value = iterator.next();
                if (value instanceof Instance) {
                    Coordinate c = GMLGeometryUtil.parseDirectPosition((Instance) value);
                    if (c != null) {
                        cs.add(c);
                    }
                }
            }
            controlCoord = cs.toArray(new Coordinate[cs.size()]);
        }
    }
    // for use with GML 3.1, 3.2
    if (controlCoord == null || controlCoord.length == 0) {
        values = PropertyResolver.getValues(instance, "posList", false);
        if (values != null && !values.isEmpty()) {
            Iterator<Object> iterator = values.iterator();
            Object value = iterator.next();
            if (value instanceof Instance) {
                controlCoord = GMLGeometryUtil.parsePosList((Instance) value, srsDimension);
            }
        }
    }
    if (controlCoord == null || controlCoord.length == 0) {
        values = PropertyResolver.getValues(instance, "pointRep.Point", false);
        if (values != null && !values.isEmpty()) {
            Iterator<Object> iterator = values.iterator();
            List<Coordinate> cs = new ArrayList<Coordinate>();
            while (iterator.hasNext()) {
                Object value = iterator.next();
                if (value instanceof Instance) {
                    try {
                        @SuppressWarnings("unchecked") DefaultGeometryProperty<Point> point = (DefaultGeometryProperty<Point>) handler.createGeometry((Instance) value, srsDimension, reader);
                        cs.add(point.getGeometry().getCoordinate());
                    } catch (GeometryNotSupportedException e) {
                        throw new GeometryNotSupportedException("Could not parse Point Representation", e);
                    }
                }
            }
            controlCoord = cs.toArray(new Coordinate[cs.size()]);
        }
    }
    // for use with GML 3.1
    if (controlCoord == null || controlCoord.length == 0) {
        values = PropertyResolver.getValues(instance, "pointProperty.Point", false);
        if (values != null && !values.isEmpty()) {
            Iterator<Object> iterator = values.iterator();
            List<Coordinate> cs = new ArrayList<Coordinate>();
            while (iterator.hasNext()) {
                Object value = iterator.next();
                if (value instanceof Instance) {
                    try {
                        @SuppressWarnings("unchecked") DefaultGeometryProperty<Point> point = (DefaultGeometryProperty<Point>) handler.createGeometry((Instance) value, srsDimension, reader);
                        cs.add(point.getGeometry().getCoordinate());
                    } catch (GeometryNotSupportedException e) {
                        throw new GeometryNotSupportedException("Could not parse Point Property", e);
                    }
                }
            }
            controlCoord = cs.toArray(new Coordinate[cs.size()]);
        }
    }
    // for use with GML2, 3, 3.1, 3.2
    if (controlCoord == null || controlCoord.length == 0) {
        values = PropertyResolver.getValues(instance, "coord", false);
        if (values != null && !values.isEmpty()) {
            Iterator<Object> iterator = values.iterator();
            List<Coordinate> cs = new ArrayList<Coordinate>();
            while (iterator.hasNext()) {
                Object value = iterator.next();
                if (value instanceof Instance) {
                    Coordinate c = GMLGeometryUtil.parseCoord((Instance) value);
                    if (c != null) {
                        cs.add(c);
                    }
                }
            }
            controlCoord = cs.toArray(new Coordinate[cs.size()]);
        }
    }
    values = PropertyResolver.getValues(instance, "radius", false);
    if (values != null && !values.isEmpty()) {
        Object value = values.iterator().next();
        if (value instanceof Instance) {
            // ##TODO :: need to check with real time data
            radius = ConversionUtil.getAs(((Instance) value).getValue(), Double.class);
        }
    }
    if (controlCoord != null && controlCoord.length != 0 && radius != 0) {
        CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
        // create Arc representing a circle
        Arc arc = new ArcByCenterPointImpl(controlCoord[0], radius, Angle.fromDegrees(0), Angle.fromDegrees(0), true);
        // get interpolation algorithm
        InterpolationAlgorithm interpol = InterpolationHelper.getInterpolation(reader, getGeometryFactory());
        LineString interpolatedCircle = interpol.interpolateArc(arc);
        if (interpolatedCircle == null) {
            log.error("Circle could be not interpolated to Linestring");
            return null;
        }
        return new DefaultGeometryProperty<LineString>(crsDef, interpolatedCircle);
    }
    throw new GeometryNotSupportedException();
}
Also used : Instance(eu.esdihumboldt.hale.common.instance.model.Instance) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) ArcByCenterPointImpl(eu.esdihumboldt.util.geometry.interpolation.model.impl.ArcByCenterPointImpl) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) ArrayList(java.util.ArrayList) InterpolationAlgorithm(eu.esdihumboldt.util.geometry.interpolation.InterpolationAlgorithm) Point(com.vividsolutions.jts.geom.Point) Arc(eu.esdihumboldt.util.geometry.interpolation.model.Arc) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) Coordinate(com.vividsolutions.jts.geom.Coordinate) LineString(com.vividsolutions.jts.geom.LineString) ParseException(java.text.ParseException)

Example 24 with CRSDefinition

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

the class EnvelopeHandler method createGeometry.

/**
 * @see GeometryHandler#createGeometry(Instance, int, IOProvider)
 */
@Override
public Object createGeometry(Instance instance, int srsDimension, IOProvider reader) throws GeometryNotSupportedException {
    MultiPoint envelope = null;
    List<Point> points = new ArrayList<Point>();
    Collection<Object> values = PropertyResolver.getValues(instance, "coordinates", false);
    if (values != null && !values.isEmpty()) {
        Iterator<Object> iterator = values.iterator();
        while (iterator.hasNext()) {
            Object value = iterator.next();
            if (value instanceof Instance) {
                try {
                    Coordinate[] cs = GMLGeometryUtil.parseCoordinates((Instance) value);
                    if (cs != null && cs.length > 0) {
                        points.add(getGeometryFactory().createPoint(cs[0]));
                    }
                } catch (ParseException e) {
                    throw new GeometryNotSupportedException("Could not parse coordinates", e);
                }
            }
        }
    }
    if (points.isEmpty()) {
        values = PropertyResolver.getValues(instance, "pos", false);
        if (values != null && !values.isEmpty()) {
            Iterator<Object> iterator = values.iterator();
            while (iterator.hasNext()) {
                Object value = iterator.next();
                if (value instanceof Instance) {
                    Coordinate c = GMLGeometryUtil.parseDirectPosition((Instance) value);
                    if (c != null) {
                        points.add(getGeometryFactory().createPoint(c));
                    }
                }
            }
        }
    }
    if (points.isEmpty()) {
        values = PropertyResolver.getValues(instance, "coord", false);
        if (values != null && !values.isEmpty()) {
            Iterator<Object> iterator = values.iterator();
            while (iterator.hasNext()) {
                Object value = iterator.next();
                if (value instanceof Instance) {
                    Coordinate c = GMLGeometryUtil.parseCoord((Instance) value);
                    if (c != null) {
                        points.add(getGeometryFactory().createPoint(c));
                    }
                }
            }
        }
    }
    if (!points.isEmpty()) {
        Coordinate[] coordinates = new Coordinate[] { points.get(0).getCoordinate(), points.get(1).getCoordinate() };
        coordinates = InterpolationHelper.moveCoordinates(reader, coordinates);
        envelope = getGeometryFactory().createMultiPoint(coordinates);
    }
    if (envelope != null) {
        CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
        return new DefaultGeometryProperty<MultiPoint>(crsDef, envelope);
    }
    throw new GeometryNotSupportedException();
}
Also used : MultiPoint(com.vividsolutions.jts.geom.MultiPoint) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) ArrayList(java.util.ArrayList) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) Coordinate(com.vividsolutions.jts.geom.Coordinate) ParseException(java.text.ParseException)

Example 25 with CRSDefinition

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

the class LinearRingHandler method createGeometry.

/**
 * Create a {@link LinearRing} geometry from the given instance.
 *
 * @param instance the instance
 * @param srsDimension the SRS dimension
 * @param allowTryOtherDimension if trying another dimension is allowed on
 *            failure (e.g. 3D instead of 2D)
 * @param reader the I/O Provider to get value
 * @return the {@link LinearRing} geometry
 * @throws GeometryNotSupportedException if the type definition doesn't
 *             represent a geometry type supported by the handler
 */
protected GeometryProperty<LinearRing> createGeometry(Instance instance, int srsDimension, boolean allowTryOtherDimension, IOProvider reader) throws GeometryNotSupportedException {
    LinearRing ring = null;
    LineStringHandler handler = new LineStringHandler();
    // for use with GML 2, 3, 3.1, 3.2
    @SuppressWarnings("unchecked") DefaultGeometryProperty<LineString> linestring = (DefaultGeometryProperty<LineString>) handler.createGeometry(instance, srsDimension, reader);
    try {
        ring = getGeometryFactory().createLinearRing(linestring.getGeometry().getCoordinates());
    } catch (IllegalArgumentException e) {
        if (allowTryOtherDimension) {
            // the error
            // "Points of LinearRing do not form a closed linestring"
            // can be an expression of a wrong dimension being used
            // we try an alternative, to be sure (e.g. 3D instead of 2D)
            int alternativeDimension = (srsDimension == 2) ? (3) : (2);
            GeometryProperty<LinearRing> geom = createGeometry(instance, alternativeDimension, false, reader);
            log.debug("Assuming geometry is " + alternativeDimension + "-dimensional.");
            return geom;
        }
        throw e;
    }
    if (ring != null) {
        CRSDefinition crsDef = GMLGeometryUtil.findCRS(instance);
        return new DefaultGeometryProperty<LinearRing>(crsDef, ring);
    }
    throw new GeometryNotSupportedException();
}
Also used : DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) GeometryProperty(eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) LineString(com.vividsolutions.jts.geom.LineString) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) LinearRing(com.vividsolutions.jts.geom.LinearRing)

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