Search in sources :

Example 1 with WKTReader

use of org.locationtech.jts.io.WKTReader 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 WKTReader

use of org.locationtech.jts.io.WKTReader 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 WKTReader

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

the class JTSHelper method createGeometryFromWKT.

/**
 * Creates a JTS Geometry from an WKT representation. Switches the
 * coordinate order if needed.
 *
 * @param wkt
 *            WKT representation of the geometry
 * @param srid
 *            the SRID of the newly created geometry
 *
 * @return JTS Geometry object
 *
 * @throws ParseException
 *             If an error occurs
 */
public static Geometry createGeometryFromWKT(String wkt, int srid) throws ParseException {
    WKTReader wktReader = getWKTReaderForSRID(srid);
    LOGGER.debug("FOI Geometry: {}", wkt);
    return wktReader.read(wkt);
}
Also used : WKTReader(org.locationtech.jts.io.WKTReader)

Example 4 with WKTReader

use of org.locationtech.jts.io.WKTReader 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)

Aggregations

WKTReader (org.locationtech.jts.io.WKTReader)4 ParseException (org.locationtech.jts.io.ParseException)3 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 ValueGeometry (org.h2.value.ValueGeometry)1