@Override public String convert(final PolyLine polyLine) { final Geometry geometry = new JtsPolyLineConverter().convert(polyLine); return new WKTWriter().write(geometry); } }
@Override public PolyLine backwardConvert(final String wkt) { LineString geometry = null; final WKTReader myReader = new WKTReader(); try { geometry = (LineString) myReader.read(wkt); } catch (final ParseException | ClassCastException e) { throw new CoreException("Cannot parse wkt : {}", wkt); } return new JtsPolyLineConverter().backwardConvert(geometry); }
/** * Tests that the {@link PolyLine}'s geometry is valid * * @param polyline * the {@link PolyLine} to test * @return {@code true} if the {@link PolyLine} has valid geometry, otherwise {@code false} */ public static boolean isValidPolyLine(final PolyLine polyline) { final LineString lineString = POLYLINE_CONVERTER.convert(polyline); return lineString.isSimple(); }
/** * Query country boundaries which cover/partially cover given {@link PolyLine} * * @param polyLine * Any {@link PolyLine} or {@link Polygon} * @return a list of {@link CountryBoundary} */ public List<CountryBoundary> boundaries(final PolyLine polyLine) { return this.boundariesHelper(() -> this.query(polyLine.bounds().asEnvelope()), boundary -> boundary.intersects(JTS_POLYLINE_CONVERTER.convert(polyLine))); }
public static Geometry getJts(final PolyLine polyLine) { if (polyLine instanceof Polygon) { return new JtsPolygonConverter().convert((Polygon) polyLine); } return new JtsPolyLineConverter().convert(polyLine); }
private List<PolyLine> processPolyLine(final Geometry intersections) { final List<PolyLine> result = new ArrayList<>(); if (intersections instanceof GeometryCollection) { final GeometryCollection collection = (GeometryCollection) intersections; final int numGeometries = collection.getNumGeometries(); for (int n = 0; n < numGeometries; n++) { final Geometry geometry = collection.getGeometryN(n); result.addAll(processPolyLine(geometry)); } } else if (intersections instanceof LineString) { result.add(new JtsPolyLineConverter().backwardConvert((LineString) intersections)); } return result; }
@Test public void testCreateMultiLineStringFromMultiPolyLine() { final String wkt = "MULTILINESTRING ((107.68471354246141 2.2346191319821231, 107.68471354246141 2.2345360028045156), " + "(107.68454724550249 2.2345601370821555, 107.68453115224835 2.2345601370821555, " + "107.68449419872607 2.2344243539043736))"; final MultiPolyLine multiPolyLine = MultiPolyLine.wkt(wkt); final PolyLine polyLine1 = PolyLine.wkt( "LINESTRING (107.68471354246141 2.2346191319821231, 107.68471354246141 2.2345360028045156)"); final PolyLine polyLine2 = PolyLine .wkt("LINESTRING (107.68454724550249 2.2345601370821555, " + "107.68453115224835 2.2345601370821555, 107.68449419872607 2.2344243539043736)"); final LineString lineString1 = new JtsPolyLineConverter().convert(polyLine1); final LineString lineString2 = new JtsPolyLineConverter().convert(polyLine2); final MultiLineString multiLineString = new JtsMultiPolyLineConverter() .convert(multiPolyLine); Assert.assertTrue("First line is contained", multiLineString.contains(lineString1)); Assert.assertTrue("Second line is contained", multiLineString.contains(lineString2)); }
/** * Query country boundaries which cover/partially cover given {@link PolyLine}, with an * extension square box * * @param polyLine * Any {@link PolyLine} or {@link Polygon} * @param extension * Extension {@link Distance} * @return a list of {@link CountryBoundary} */ public List<CountryBoundary> boundaries(final PolyLine polyLine, final Distance extension) { return this.boundariesHelper( () -> this.query(polyLine.bounds().expand(extension).asEnvelope()), boundary -> boundary.intersects(JTS_POLYLINE_CONVERTER.convert(polyLine))); }
private List<? extends PolyLine> processResult(final Geometry intersections) { final List<PolyLine> result = new ArrayList<>(); if (intersections instanceof GeometryCollection) { final GeometryCollection collection = (GeometryCollection) intersections; final int numGeometries = collection.getNumGeometries(); for (int n = 0; n < numGeometries; n++) { final Geometry geometry = collection.getGeometryN(n); result.addAll(processResult(geometry)); } } else if (intersections instanceof com.vividsolutions.jts.geom.Polygon) { result.add(new JtsPolygonConverter() .backwardConvert((com.vividsolutions.jts.geom.Polygon) intersections)); } else if (intersections instanceof LineString) { result.add(new JtsPolyLineConverter().backwardConvert((LineString) intersections)); } return result; } }
@Test public void testNearestNeighborCountryCodeOnMultiLineStringOutsideBoundary() { final CountryBoundaryMap map = CountryBoundaryMap.fromPlainText(new InputStreamResource( CountryBoundaryMapTest.class.getResourceAsStream("DMA_boundary.txt"))); map.setShouldAlwaysSlicePredicate(taggable -> true); final PolyLine polyLine = PolyLine.wkt(new InputStreamResource( () -> CountryBoundaryMapTest.class.getResourceAsStream("DMA_snake_polyline.wkt")) .firstLine()); final List<Geometry> sliced = map.slice(123000000L, new JtsPolyLineConverter().convert(polyLine), Taggable.with("force-slice", "please")); int withNearestNeighborTag = 0; for (final Geometry slicedGeometry : sliced) { if (SyntheticNearestNeighborCountryCodeTag.YES.name() .equals(CountryBoundaryMap.getGeometryProperty(slicedGeometry, SyntheticNearestNeighborCountryCodeTag.KEY))) { withNearestNeighborTag++; } } Assert.assertEquals(3, withNearestNeighborTag); }
final PolyLine polyline = new JtsPolyLineConverter() .backwardConvert((LineString) geometry); final Iterable<? extends Shard> shards = sharding.shardsIntersecting(polyline); for (final Shard shard : shards)