Search in sources :

Example 1 with GeometryPrecisionReducer

use of com.vividsolutions.jts.precision.GeometryPrecisionReducer in project teiid by teiid.

the class GeometryUtils method snapToGrid.

public static GeometryType snapToGrid(GeometryType geom, double size) throws FunctionExecutionException {
    if (size == 0) {
        return geom;
    }
    Geometry g1 = getGeometry(geom);
    PrecisionModel precisionModel = new PrecisionModel(1 / size);
    GeometryPrecisionReducer reducer = new GeometryPrecisionReducer(precisionModel);
    reducer.setPointwise(true);
    reducer.setChangePrecisionModel(true);
    Geometry result = reducer.reduce(g1);
    // since the wkb writer doesn't consider precision, we have to first write/read through wkt
    WKTWriter writer = new WKTWriter();
    String val = writer.write(result);
    WKTReader reader = new WKTReader(GEOMETRY_FACTORY);
    try {
        result = reader.read(new StringReader(val));
    } catch (ParseException e) {
        throw new FunctionExecutionException(e);
    }
    result.setSRID(geom.getSrid());
    return getGeometryType(result);
}
Also used : WKTWriter(com.vividsolutions.jts.io.WKTWriter) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) GeometryPrecisionReducer(com.vividsolutions.jts.precision.GeometryPrecisionReducer) StringReader(java.io.StringReader) ParseException(com.vividsolutions.jts.io.ParseException) WKTReader(com.vividsolutions.jts.io.WKTReader)

Aggregations

ParseException (com.vividsolutions.jts.io.ParseException)1 WKTReader (com.vividsolutions.jts.io.WKTReader)1 WKTWriter (com.vividsolutions.jts.io.WKTWriter)1 GeometryPrecisionReducer (com.vividsolutions.jts.precision.GeometryPrecisionReducer)1 StringReader (java.io.StringReader)1 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)1