Search in sources :

Example 1 with ParseException

use of org.locationtech.jts.io.ParseException in project h2database by h2database.

the class ValueGeometry method get.

/**
 * Get or create a geometry value for the given geometry.
 *
 * @param s the WKT representation of the geometry
 * @param srid the srid of the object
 * @return the value
 */
public static ValueGeometry get(String s, int srid) {
    try {
        GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), srid);
        Geometry g = new WKTReader(geometryFactory).read(s);
        return get(g);
    } catch (ParseException ex) {
        throw DbException.convert(ex);
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) PrecisionModel(org.locationtech.jts.geom.PrecisionModel) ParseException(org.locationtech.jts.io.ParseException) WKTReader(org.locationtech.jts.io.WKTReader)

Example 2 with ParseException

use of org.locationtech.jts.io.ParseException in project h2database by h2database.

the class TestSpatial method geomFromText.

/**
 * Convert the text to a geometry object.
 *
 * @param text the geometry as a Well Known Text
 * @param srid the projection id
 * @return Geometry object
 */
public static Geometry geomFromText(String text, int srid) throws SQLException {
    WKTReader wktReader = new WKTReader();
    try {
        Geometry geom = wktReader.read(text);
        geom.setSRID(srid);
        return geom;
    } catch (ParseException ex) {
        throw new SQLException(ex);
    }
}
Also used : ValueGeometry(org.h2.value.ValueGeometry) Geometry(org.locationtech.jts.geom.Geometry) SQLException(java.sql.SQLException) ParseException(org.locationtech.jts.io.ParseException) WKTReader(org.locationtech.jts.io.WKTReader)

Example 3 with ParseException

use of org.locationtech.jts.io.ParseException in project arctic-sea by 52North.

the class ODataFesParser method parseGeometry.

/**
 * Parse the value expression as an {@code Geometry} in WKT or EWKT format. Geographies are handled as if they would
 * be geometries.
 *
 * @param val the geometry value
 *
 * @return the geometry
 *
 * @throws DecodingException if the geometry is invalid
 */
private static Geometry parseGeometry(ValueExpr val) throws DecodingException {
    String value = val.getValue();
    if (value.startsWith(GEOGRAPHY_TYPE)) {
        value = value.substring(GEOGRAPHY_TYPE.length());
    }
    if (value.startsWith(GEOMETRY_TYPE)) {
        value = value.substring(GEOMETRY_TYPE.length());
    }
    value = stripQuotes(value).toUpperCase();
    int srid = 4326;
    if (value.startsWith(SRID_PREFIX)) {
        int sep = value.indexOf(';');
        if (sep > SRID_PREFIX.length() && value.length() > sep) {
            try {
                srid = Integer.parseInt(value.substring(SRID_PREFIX.length(), sep));
            } catch (NumberFormatException ex) {
                throw invalidGeometry(val, ex);
            }
            value = value.substring(sep + 1);
        } else {
            throw invalidGeometry(val);
        }
    }
    PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING);
    GeometryFactory geometryFactory = new GeometryFactory(precisionModel, srid);
    WKTReader wktReader = new WKTReader(geometryFactory);
    try {
        return wktReader.read(value);
    } catch (ParseException ex) {
        throw invalidGeometry(val, ex);
    }
}
Also used : GeometryFactory(org.locationtech.jts.geom.GeometryFactory) PrecisionModel(org.locationtech.jts.geom.PrecisionModel) ParseException(org.locationtech.jts.io.ParseException) WKTReader(org.locationtech.jts.io.WKTReader)

Example 4 with ParseException

use of org.locationtech.jts.io.ParseException in project arctic-sea by 52North.

the class GmlDecoderv311 method parsePointType.

private Object parsePointType(PointType xbPointType) throws DecodingException {
    String geomWKT = null;
    int srid = -1;
    if (xbPointType.getSrsName() != null) {
        srid = CRSHelper.parseSrsName(xbPointType.getSrsName());
    }
    if (xbPointType.getPos() != null) {
        DirectPositionType xbPos = xbPointType.getPos();
        if (srid == -1 && xbPos.getSrsName() != null) {
            srid = CRSHelper.parseSrsName(xbPos.getSrsName());
        }
        String directPosition = getString4Pos(xbPos);
        geomWKT = JTSHelper.createWKTPointFromCoordinateString(directPosition);
    } else if (xbPointType.getCoordinates() != null) {
        CoordinatesType xbCoords = xbPointType.getCoordinates();
        String directPosition = getString4Coordinates(xbCoords);
        geomWKT = JTSHelper.createWKTPointFromCoordinateString(directPosition);
    } else {
        throw new DecodingException("For geometry type 'gml:Point' only elements 'gml:pos' and 'gml:coordinates' are allowed");
    }
    checkSrid(srid);
    if (srid == -1) {
        throw new DecodingException("No SrsName ist specified for geometry!");
    }
    try {
        return JTSHelper.createGeometryFromWKT(geomWKT, srid);
    } catch (ParseException ex) {
        throw new DecodingException(ex);
    }
}
Also used : DirectPositionType(net.opengis.gml.DirectPositionType) DecodingException(org.n52.svalbard.decode.exception.DecodingException) DateTimeParseException(org.n52.shetland.util.DateTimeParseException) ParseException(org.locationtech.jts.io.ParseException) CoordinatesType(net.opengis.gml.CoordinatesType)

Example 5 with ParseException

use of org.locationtech.jts.io.ParseException in project arctic-sea by 52North.

the class GmlDecoderv311 method getGeometry4BBOX.

private Geometry getGeometry4BBOX(EnvelopeDocument xbBbox) throws DecodingException {
    EnvelopeType xbEnvelope = xbBbox.getEnvelope();
    // parse srid; if not set, throw exception!
    int srid = CRSHelper.parseSrsName(xbEnvelope.getSrsName());
    String lower = xbEnvelope.getLowerCorner().getStringValue();
    String upper = xbEnvelope.getUpperCorner().getStringValue();
    String geomWKT = String.format("MULTIPOINT(%s, %s)", lower, upper);
    try {
        return JTSHelper.createGeometryFromWKT(geomWKT, srid).getEnvelope();
    } catch (ParseException ex) {
        throw new DecodingException(ex);
    }
}
Also used : EnvelopeType(net.opengis.gml.EnvelopeType) DecodingException(org.n52.svalbard.decode.exception.DecodingException) DateTimeParseException(org.n52.shetland.util.DateTimeParseException) ParseException(org.locationtech.jts.io.ParseException)

Aggregations

ParseException (org.locationtech.jts.io.ParseException)8 DateTimeParseException (org.n52.shetland.util.DateTimeParseException)5 DecodingException (org.n52.svalbard.decode.exception.DecodingException)5 WKTReader (org.locationtech.jts.io.WKTReader)3 DirectPositionType (net.opengis.gml.x32.DirectPositionType)2 Geometry (org.locationtech.jts.geom.Geometry)2 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)2 PrecisionModel (org.locationtech.jts.geom.PrecisionModel)2 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 CoordinatesType (net.opengis.gml.CoordinatesType)1 DirectPositionType (net.opengis.gml.DirectPositionType)1 EnvelopeType (net.opengis.gml.EnvelopeType)1 AbstractRingPropertyType (net.opengis.gml.x32.AbstractRingPropertyType)1 AbstractRingType (net.opengis.gml.x32.AbstractRingType)1 CoordinatesType (net.opengis.gml.x32.CoordinatesType)1 DirectPositionListType (net.opengis.gml.x32.DirectPositionListType)1 LinearRingType (net.opengis.gml.x32.LinearRingType)1 ValueGeometry (org.h2.value.ValueGeometry)1