Search in sources :

Example 66 with WKTReader

use of com.vividsolutions.jts.io.WKTReader 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)

Example 67 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project teiid by teiid.

the class GeometryUtils method geometryFromClob.

public static GeometryType geometryFromClob(ClobType wkt, Integer srid, boolean allowEwkt) throws FunctionExecutionException {
    Reader r = null;
    try {
        WKTReader reader = new WKTReader(GEOMETRY_FACTORY);
        r = wkt.getCharacterStream();
        if (allowEwkt) {
            PushbackReader pbr = new PushbackReader(r, 1);
            r = pbr;
            char[] expected = new char[] { 's', 'r', 'i', 'd', '=' };
            int expectedIndex = 0;
            StringBuilder sridBuffer = null;
            for (int i = 0; i < 100000; i++) {
                int charRead = pbr.read();
                if (charRead == -1) {
                    break;
                }
                if (expectedIndex == expected.length) {
                    // parse srid
                    if (sridBuffer == null) {
                        sridBuffer = new StringBuilder(4);
                    }
                    if (charRead == ';') {
                        if (sridBuffer.length() == 0) {
                            pbr.unread(charRead);
                        }
                        break;
                    }
                    sridBuffer.append((char) charRead);
                    continue;
                }
                if (expectedIndex == 0 && Character.isWhitespace(charRead)) {
                    continue;
                }
                if (expected[expectedIndex] != Character.toLowerCase(charRead)) {
                    pbr.unread(charRead);
                    break;
                }
                expectedIndex++;
            }
            if (sridBuffer != null) {
                srid = Integer.parseInt(sridBuffer.toString());
            }
        }
        Geometry jtsGeometry = reader.read(r);
        if (jtsGeometry == null) {
            // for some reason io and parse exceptions are caught in their logic if they occur on the first word, then null is returned
            throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31203));
        }
        if (!allowEwkt && (jtsGeometry.getSRID() != GeometryType.UNKNOWN_SRID || (jtsGeometry.getCoordinate() != null && !Double.isNaN(jtsGeometry.getCoordinate().z)))) {
            // $NON-NLS-1$
            throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31160, "EWKT"));
        }
        if (srid == null) {
            srid = jtsGeometry.getSRID();
        }
        return getGeometryType(jtsGeometry, srid);
    } catch (ParseException e) {
        throw new FunctionExecutionException(e);
    } catch (SQLException e) {
        throw new FunctionExecutionException(e);
    } catch (IOException e) {
        throw new FunctionExecutionException(e);
    } finally {
        if (r != null) {
            try {
                r.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) SQLException(java.sql.SQLException) GeoJSONReader(org.wololo.jts2geojson.GeoJSONReader) WKBReader(com.vividsolutions.jts.io.WKBReader) Reader(java.io.Reader) WKTReader(com.vividsolutions.jts.io.WKTReader) StringReader(java.io.StringReader) PushbackReader(java.io.PushbackReader) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) WKTReader(com.vividsolutions.jts.io.WKTReader) PushbackReader(java.io.PushbackReader)

Example 68 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project teiid by teiid.

the class TestGeometry method testTransform.

@Test
public void testTransform() throws Exception {
    Geometry g0 = new WKTReader().read("POINT(426418.89 4957737.37)");
    Geometry g1 = GeometryTransformUtils.transform(g0, // EPSG:32632
    "+proj=utm +zone=32 +datum=WGS84 +units=m +no_defs", // EPSG:4326
    "+proj=longlat +datum=WGS84 +no_defs");
    assertEquals("POINT (8.07013599546795 44.76924401481436)", g1.toText());
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) WKTReader(com.vividsolutions.jts.io.WKTReader) Test(org.junit.Test)

Example 69 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project tutorials by eugenp.

the class HibernateSpatialTest method wktToGeometry.

private Geometry wktToGeometry(String wellKnownText) throws ParseException {
    WKTReader fromText = new WKTReader();
    Geometry geom = null;
    geom = fromText.read(wellKnownText);
    return geom;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) WKTReader(com.vividsolutions.jts.io.WKTReader)

Example 70 with WKTReader

use of com.vividsolutions.jts.io.WKTReader in project onebusaway-application-modules by camsys.

the class EnterpriseFilteredGeocoderBase method setWktFilterPolygon.

public void setWktFilterPolygon(String wkt) throws ParseException {
    WKTReader reader = new WKTReader();
    _wktFilterPolygon = (Polygon) reader.read(wkt);
}
Also used : WKTReader(com.vividsolutions.jts.io.WKTReader)

Aggregations

WKTReader (com.vividsolutions.jts.io.WKTReader)70 Geometry (com.vividsolutions.jts.geom.Geometry)50 Test (org.junit.Test)28 ParseException (com.vividsolutions.jts.io.ParseException)23 WKTWriter (com.vividsolutions.jts.io.WKTWriter)9 Metacard (ddf.catalog.data.Metacard)9 Coordinate (com.vividsolutions.jts.geom.Coordinate)6 IsValidOp (com.vividsolutions.jts.operation.valid.IsValidOp)6 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)6 GeometryOperator (org.codice.alliance.libs.klv.GeometryOperator)6 Context (org.codice.alliance.video.stream.mpegts.Context)6 MapLayer (au.org.emii.portal.menu.MapLayer)5 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)4 Polygon (com.vividsolutions.jts.geom.Polygon)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Optional (java.util.Optional)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 Before (org.junit.Before)3