GeospatialUtilsJTS extends GeospatialUtils { public static JtsGeometry geometry(String string) { if (StringUtils.isBlank(string)) { throw new IndexException("Shape shouldn't be blank"); } try { GeometryFactory geometryFactory = CONTEXT.getGeometryFactory(); WKTReader reader = new WKTReader(geometryFactory); Geometry geometry = reader.read(string); if (!geometry.isValid()) { geometry = geometry.buffer(0); } return CONTEXT.makeShape(geometry); } catch (ParseException | IllegalArgumentException e) { throw new IndexException(e, "Shape '{}' is not parseable", string); } }  static JtsGeometry geometry(String string); static final JtsSpatialContext CONTEXT; }
@Test public void testParseWKTPoint() throws Exception { String wkt = "POINT (30.01 -10.7)"; JtsGeometry geometry = geometry(wkt); assertEquals("Unable to parse WKT point", wkt, geometry.toString()); }
@Test public void testParseWKTLine() throws Exception { String wkt = "LINESTRING (30 10, 10 30, 40 40)"; JtsGeometry geometry = geometry(wkt); assertEquals("Unable to parse WKT line", wkt, geometry.toString()); }
@Test public void testParseWKTPolygon() throws Exception { String wkt = "LINESTRING (30 10, 10 30, 40 40)"; JtsGeometry geometry = geometry(wkt); assertEquals("Unable to parse WKT polygon", wkt, geometry.toString()); }
@Test public void testParseWKTMultipoint() throws Exception { String wkt = "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))"; JtsGeometry geometry = geometry(wkt); assertEquals("Unable to parse WKT multipoint", wkt, geometry.toString()); }
@Test public void testParseWKTMultiline() throws Exception { String wkt = "MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))"; JtsGeometry geometry = geometry(wkt); assertEquals("Unable to parse WKT multiline", wkt, geometry.toString()); }
@Test public void testParseWKTMultipolygon() throws Exception { String wkt = "MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))"; JtsGeometry geometry = geometry(wkt); assertEquals("Unable to parse WKT multipolygon", wkt, geometry.toString()); }
@Test public void testParseWKTSelfIntersectingPolygon() throws Exception { String wkt = "POLYGON((5 0, 10 0, 10 10, 0 10, 0 0, 5 0, 3 3, 5 6, 7 3, 5 0))"; JtsGeometry geometry = geometry(wkt); assertEquals("Unable to parse invalid WKT shape", "POLYGON ((5 0, 0 0, 0 10, 10 10, 10 0, 5 0), (5 0, 7 3, 5 6, 3 3, 5 0))", geometry.toString()); }