public boolean isValid() { return polygon.isValid(); }
/** * If validation is requested, scan the geometries and build valid polygons (in case they * aren't) by also removing holes. * * @param geometriesList * @return */ private List<Polygon> validateGeometries(List<Polygon> geometriesList) { if (forceValid && (geometriesList.size() > 0)) { List<Polygon> validated = new ArrayList<Polygon>(geometriesList.size()); for (int i = 0; i < geometriesList.size(); i++) { Polygon polygon = geometriesList.get(i); if (!polygon.isValid()) { List<Polygon> validPolygons = JTS.makeValid(polygon, true); validated.addAll(validPolygons); } else { validated.add(polygon); } } geometriesList = validated; } return geometriesList; }
/** * Converts a way with potential inner ways to a JTS geometry. * * @param way the way * @param innerWays the inner ways or null * @return the JTS geometry */ public static Geometry toJtsGeometry(TDWay way, List<TDWay> innerWays) { if (way == null) { LOGGER.warning("way is null"); return null; } if (way.isForcePolygonLine()) { // may build a single line string if inner ways are empty return buildMultiLineString(way, innerWays); } if (way.getShape() != TDWay.LINE || innerWays != null && innerWays.size() > 0) { // Have to be careful here about polygons and lines again, the problem with // polygons is that a certain direction is forced, so we do not want to reverse // closed lines that are not meant to be polygons // may contain holes if inner ways are not empty Polygon polygon = buildPolygon(way, innerWays); if (polygon.isValid()) { return polygon; } return repairInvalidPolygon(polygon); } // not a closed line return buildLineString(way); }
@Test public void testBuildInValidPolygonWith2InnerRings() { String testfile = "invalid-polygon-2-inner-rings.wkt"; List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile); Polygon polygon = JTSUtils.buildPolygon(ways.get(0), ways.subList(1, ways.size())); Geometry expected = MockingUtils.readWKTFile(testfile); Assert.isTrue(!polygon.isValid()); Assert.equals(expected, polygon); }
@Test public void testBuildValidPolygonWith2InnerRings() { String testfile = "valid-polygon-2-inner-rings.wkt"; List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile); Polygon polygon = JTSUtils.buildPolygon(ways.get(0), ways.subList(1, ways.size())); Geometry expected = MockingUtils.readWKTFile(testfile); Assert.isTrue(polygon.isValid()); Assert.equals(expected, polygon); } }
/** * @param way the TDWay * @return way as a polygon */ public static Polygon mapWayToPolygon(TDWay way) { TDNode[] wayNodes = way.getWayNodes(); if (wayNodes.length < 3) { return null; } Coordinate[] wayCoords = new Coordinate[wayNodes.length + 1]; for (int i = 0; i < wayCoords.length - 1; i++) { wayCoords[i] = new Coordinate(wayNodes[i].getLatitude(), wayNodes[i].getLongitude()); } wayCoords[wayCoords.length - 1] = wayCoords[0]; Polygon polygon = GEOMETRY_FACTORY.createPolygon(GEOMETRY_FACTORY.createLinearRing(wayCoords), null); if (!polygon.isValid()) { return null; } return polygon; }
@Test public void testBuildInvalidPolygon() { String testfile = "invalid-polygon.wkt"; List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile); Polygon polygon = JTSUtils.buildPolygon(ways.get(0)); Geometry expected = MockingUtils.readWKTFile(testfile); Assert.isTrue(!polygon.isValid()); Assert.equals(expected, polygon); }
@Test public void testBuildValidPolygon() { String testfile = "valid-polygon.wkt"; List<TDWay> ways = MockingUtils.wktPolygonToWays(testfile); Polygon polygon = JTSUtils.buildPolygon(ways.get(0)); Geometry expected = MockingUtils.readWKTFile(testfile); Assert.isTrue(polygon.isValid()); Assert.equals(expected, polygon); }
if (!polygon.isValid()) { return;
@Test public void testMakeValid() throws Exception { // An invalid polygon similar to this one // // *----* // | | // *----*----* // | | // *----* // // Will be split into 2 separate polygons through the makeValid method final int[] xPoints = {0, 5, 5, 5, 10, 10, 5, 0}; final int[] yPoints = {0, 0, 5, 10, 10, 5, 5, 5}; final int nPoints = xPoints.length; final Shape shape = new java.awt.Polygon(xPoints, yPoints, nPoints); final LinearRing geom = (LinearRing) JTS.toGeometry(shape); final GeometryFactory factory = new GeometryFactory(); final org.locationtech.jts.geom.Polygon polygon = factory.createPolygon(geom); assertFalse(polygon.isValid()); final List<org.locationtech.jts.geom.Polygon> validPols = JTS.makeValid(polygon, false); assertEquals(2, validPols.size()); org.locationtech.jts.geom.Polygon polygon1 = validPols.get(0); org.locationtech.jts.geom.Polygon polygon2 = validPols.get(1); assertEquals(5, polygon1.getNumPoints()); assertEquals(5, polygon2.getNumPoints()); } }
public boolean isValid() { org.locationtech.jts.geom.Polygon poly = (org.locationtech.jts.geom.Polygon) this.getJTSGeometry(); return poly.isValid(); }
@Test public void testInvalidMultipolygon() throws Exception { WKTReader reader = new WKTReader(); // three triangles that united form a rectangle with a triangular hole in the middle GeometryCollector collector = new GeometryCollector(); collector.setFactory(null); final Geometry p0 = reader.read("POLYGON((0 0, 0 3, 3 3, 3 0, 0 0))"); collector.add(p0); final Geometry p1 = reader.read("POLYGON((1 1, 1 2, 2 2, 2 1, 1 1))"); collector.add(p1); GeometryCollection result = collector.collect(); assertEquals(1, result.getNumGeometries()); Polygon p = (org.locationtech.jts.geom.Polygon) result.getGeometryN(0); assertTrue(p.isValid()); }
/** * Create and collect shadow polygons. * * @param shadows * @param coordinates * @param shadow * @param factory */ private static void createShadowPolygons(Collection<Polygon> shadows, Coordinate[] coordinates, double[] shadow, GeometryFactory factory) { for (int i = 1; i < coordinates.length; i++) { Coordinate startCoord = coordinates[i - 1]; Coordinate endCoord = coordinates[i]; Coordinate nextEnd = moveCoordinate(endCoord, shadow); Coordinate nextStart = moveCoordinate(startCoord, shadow); Polygon polygon = factory.createPolygon(new Coordinate[]{startCoord, endCoord, nextEnd, nextStart, startCoord}); if (polygon.isValid()) { shadows.add(polygon); } } } }
if (!geom.isValid()) { throw new SQLException("Geometry not valid");