Search in sources :

Example 31 with CRSDefinition

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

the class SpatiaLiteGeometries method convertToInstanceGeometry.

@Override
public GeometryProperty<?> convertToInstanceGeometry(Object geom, TypeDefinition columnType, SQLiteConnection connection, Supplier<CRSDefinition> crsProvider, SimpleLog log) throws Exception {
    // show error and abort if SpatiaLite is not available
    if (!SpatiaLiteHelper.isSpatialLiteLoadedReport(connection, true)) {
    // don't throw, will prevent any data being loaded
    // throw new IllegalStateException("SpatiaLite module is not available");
    }
    // decode geometry read from DB
    GeometryMetadata columnTypeMetadata = columnType.getConstraint(GeometryMetadata.class);
    Geometry jtsGeom = decodeGeometryValue(geom, columnTypeMetadata, connection);
    // determine CRS
    CRSDefinition crsDef = null;
    String authName = columnTypeMetadata.getAuthName();
    if (authName != null && authName.equalsIgnoreCase("EPSG")) {
        String epsgCode = authName + ":" + columnTypeMetadata.getSrs();
        crsDef = new CodeDefinition(epsgCode, null);
    } else {
        String wkt = columnTypeMetadata.getSrsText();
        if (wkt != null) {
            crsDef = new WKTDefinition(wkt, null);
        }
    }
    return new DefaultGeometryProperty<Geometry>(crsDef, jtsGeom);
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryMetadata(eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) CodeDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition) WKTDefinition(eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)

Example 32 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(org.locationtech.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(org.locationtech.jts.geom.Point) MultiPoint(org.locationtech.jts.geom.MultiPoint) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) Coordinate(org.locationtech.jts.geom.Coordinate) ParseException(java.text.ParseException)

Example 33 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(org.locationtech.jts.geom.LineString) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) LinearRing(org.locationtech.jts.geom.LinearRing)

Example 34 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(org.locationtech.jts.geom.Point) Arc(eu.esdihumboldt.util.geometry.interpolation.model.Arc) DefaultGeometryProperty(eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty) Coordinate(org.locationtech.jts.geom.Coordinate) LineString(org.locationtech.jts.geom.LineString) ParseException(java.text.ParseException)

Example 35 with CRSDefinition

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

the class RingHandler 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;
    // for use with GML 2, 3, 3.1, 3.2
    // use generic geometry handler to read curveMembers as MultiLineString
    // or LineString
    Collection<GeometryProperty<?>> properties = genericHandler.createGeometry(instance, srsDimension, reader);
    if (properties != null) {
        if (properties.size() == 1) {
            // geometry could be combined
            GeometryProperty<?> prop = properties.iterator().next();
            try {
                ring = getGeometryFactory().createLinearRing(filterDuplicates(prop.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 = prop.getCRSDefinition();
                if (crsDef == null) {
                    GMLGeometryUtil.findCRS(instance);
                }
                return new DefaultGeometryProperty<LinearRing>(crsDef, ring);
            }
        } else {
            throw new GeometryNotSupportedException("Ring components could not be combined to a geometry");
        }
    }
    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) CRSDefinition(eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition) GeometryNotSupportedException(eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException) LinearRing(org.locationtech.jts.geom.LinearRing)

Aggregations

CRSDefinition (eu.esdihumboldt.hale.common.schema.geometry.CRSDefinition)37 DefaultGeometryProperty (eu.esdihumboldt.hale.common.instance.geometry.DefaultGeometryProperty)24 Geometry (org.locationtech.jts.geom.Geometry)14 CodeDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.CodeDefinition)13 GeometryProperty (eu.esdihumboldt.hale.common.schema.geometry.GeometryProperty)11 ArrayList (java.util.ArrayList)9 Point (org.locationtech.jts.geom.Point)9 WKTDefinition (eu.esdihumboldt.hale.common.instance.geometry.impl.WKTDefinition)8 GeometryNotSupportedException (eu.esdihumboldt.hale.io.gml.geometry.GeometryNotSupportedException)8 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)7 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 Coordinate (org.locationtech.jts.geom.Coordinate)6 TransformationException (eu.esdihumboldt.hale.common.align.transformation.function.TransformationException)5 GeometryMetadata (eu.esdihumboldt.hale.common.schema.model.constraint.type.GeometryMetadata)5 ParseException (java.text.ParseException)5 LineString (org.locationtech.jts.geom.LineString)5 Geometry (com.vividsolutions.jts.geom.Geometry)4 NoResultException (eu.esdihumboldt.hale.common.align.transformation.function.impl.NoResultException)3