From 992948a3312be5ad9bf97fc50b3c3a0de3acece1 Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Mon, 11 Jun 2018 20:25:59 -0400 Subject: [PATCH 1/2] Remove LineString upcast when accessing Polygon rings Do this to make it simpler for a user to construct new Polygons from one or more rings of an existing Polygon. Signed-off-by: Daniel Baston --- .../core/src/main/java/org/locationtech/jts/geom/Polygon.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/core/src/main/java/org/locationtech/jts/geom/Polygon.java b/modules/core/src/main/java/org/locationtech/jts/geom/Polygon.java index e8be5ccfaa..043a8bb641 100644 --- a/modules/core/src/main/java/org/locationtech/jts/geom/Polygon.java +++ b/modules/core/src/main/java/org/locationtech/jts/geom/Polygon.java @@ -220,7 +220,7 @@ public boolean isRectangle() return true; } - public LineString getExteriorRing() { + public LinearRing getExteriorRing() { return shell; } @@ -228,7 +228,7 @@ public int getNumInteriorRing() { return holes.length; } - public LineString getInteriorRingN(int n) { + public LinearRing getInteriorRingN(int n) { return holes[n]; } From af8aee0ba345d5f5f8c845c721d9df20fac3bd3d Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Wed, 13 Jun 2018 07:08:19 -0400 Subject: [PATCH 2/2] Remove unneeded LinearRing casts Signed-off-by: Daniel Baston --- .../jtstest/function/GeometryFunctions.java | 8 ++++---- .../testbuilder/geom/GeometryCombiner.java | 4 ++-- .../locationtech/jts/algorithm/PointLocator.java | 4 ++-- .../locate/SimplePointInAreaLocator.java | 4 ++-- .../jts/geom/util/GeometryTransformer.java | 4 ++-- .../jts/geomgraph/GeometryGraph.java | 4 ++-- .../org/locationtech/jts/io/gml2/GMLWriter.java | 4 ++-- .../org/locationtech/jts/io/kml/KMLWriter.java | 4 ++-- .../operation/buffer/OffsetCurveSetBuilder.java | 4 ++-- .../jts/operation/valid/IsValidOp.java | 16 ++++++++-------- .../shape/fractal/SierpinskiCarpetBuilder.java | 2 +- .../jts/operation/valid/ValidClosedRingTest.java | 6 +++--- .../locationtech/jtslab/clean/HoleRemover.java | 6 +++--- .../jtslab/clean/InvalidHoleRemover.java | 6 +++--- 14 files changed, 38 insertions(+), 38 deletions(-) diff --git a/modules/app/src/main/java/org/locationtech/jtstest/function/GeometryFunctions.java b/modules/app/src/main/java/org/locationtech/jtstest/function/GeometryFunctions.java index e63ff8014f..b3ebd08417 100644 --- a/modules/app/src/main/java/org/locationtech/jtstest/function/GeometryFunctions.java +++ b/modules/app/src/main/java/org/locationtech/jtstest/function/GeometryFunctions.java @@ -59,13 +59,13 @@ public static Geometry getGeometryN(Geometry g, int i) public static Geometry getPolygonShell(Geometry g) { if (g instanceof Polygon) { - LinearRing shell = (LinearRing) ((Polygon) g).getExteriorRing(); + LinearRing shell = ((Polygon) g).getExteriorRing(); return g.getFactory().createPolygon(shell, null); } if (g instanceof MultiPolygon) { Polygon[] poly = new Polygon[g.getNumGeometries()]; for (int i = 0; i < g.getNumGeometries(); i++) { - LinearRing shell = (LinearRing) ((Polygon) g.getGeometryN(i)).getExteriorRing(); + LinearRing shell = ((Polygon) g.getGeometryN(i)).getExteriorRing(); poly[i] = g.getFactory().createPolygon(shell, null); } return g.getFactory().createMultiPolygon(poly); @@ -82,7 +82,7 @@ public void filter(Geometry geom) { if (geom instanceof Polygon) { Polygon poly = (Polygon) geom; for (int i = 0; i < poly.getNumInteriorRing(); i++) { - Polygon hole = geom.getFactory().createPolygon((LinearRing) poly.getInteriorRingN(i), null); + Polygon hole = geom.getFactory().createPolygon(poly.getInteriorRingN(i), null); holePolys.add(hole); } } @@ -94,7 +94,7 @@ public void filter(Geometry geom) { public static Geometry getPolygonHoleN(Geometry g, int i) { if (g instanceof Polygon) { - LinearRing ring = (LinearRing) ((Polygon) g).getInteriorRingN(i); + LinearRing ring = ((Polygon) g).getInteriorRingN(i); return ring; } return null; diff --git a/modules/app/src/main/java/org/locationtech/jtstest/testbuilder/geom/GeometryCombiner.java b/modules/app/src/main/java/org/locationtech/jtstest/testbuilder/geom/GeometryCombiner.java index cedb9ae73f..87cdec1583 100644 --- a/modules/app/src/main/java/org/locationtech/jtstest/testbuilder/geom/GeometryCombiner.java +++ b/modules/app/src/main/java/org/locationtech/jtstest/testbuilder/geom/GeometryCombiner.java @@ -76,10 +76,10 @@ public Polygon addHole(Polygon poly, LinearRing hole) int nOrigHoles = poly.getNumInteriorRing(); LinearRing[] newHoles = new LinearRing[nOrigHoles + 1]; for (int i = 0; i < nOrigHoles; i++) { - newHoles[i] = (LinearRing) poly.getInteriorRingN(i); + newHoles[i] = poly.getInteriorRingN(i); } newHoles[nOrigHoles] = hole; - return geomFactory.createPolygon((LinearRing) poly.getExteriorRing(), newHoles); + return geomFactory.createPolygon(poly.getExteriorRing(), newHoles); } public Geometry combine(Geometry orig, Geometry geom) diff --git a/modules/core/src/main/java/org/locationtech/jts/algorithm/PointLocator.java b/modules/core/src/main/java/org/locationtech/jts/algorithm/PointLocator.java index 27b3a6eb39..d619e54f45 100644 --- a/modules/core/src/main/java/org/locationtech/jts/algorithm/PointLocator.java +++ b/modules/core/src/main/java/org/locationtech/jts/algorithm/PointLocator.java @@ -186,14 +186,14 @@ private int locateInPolygon(Coordinate p, Polygon poly) { if (poly.isEmpty()) return Location.EXTERIOR; - LinearRing shell = (LinearRing) poly.getExteriorRing(); + LinearRing shell = poly.getExteriorRing(); int shellLoc = locateInPolygonRing(p, shell); if (shellLoc == Location.EXTERIOR) return Location.EXTERIOR; if (shellLoc == Location.BOUNDARY) return Location.BOUNDARY; // now test if the point lies in or on the holes for (int i = 0; i < poly.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) poly.getInteriorRingN(i); + LinearRing hole = poly.getInteriorRingN(i); int holeLoc = locateInPolygonRing(p, hole); if (holeLoc == Location.INTERIOR) return Location.EXTERIOR; if (holeLoc == Location.BOUNDARY) return Location.BOUNDARY; diff --git a/modules/core/src/main/java/org/locationtech/jts/algorithm/locate/SimplePointInAreaLocator.java b/modules/core/src/main/java/org/locationtech/jts/algorithm/locate/SimplePointInAreaLocator.java index b5cd5182dc..57cca114eb 100644 --- a/modules/core/src/main/java/org/locationtech/jts/algorithm/locate/SimplePointInAreaLocator.java +++ b/modules/core/src/main/java/org/locationtech/jts/algorithm/locate/SimplePointInAreaLocator.java @@ -85,13 +85,13 @@ else if (geom instanceof GeometryCollection) { public static int locatePointInPolygon(Coordinate p, Polygon poly) { if (poly.isEmpty()) return Location.EXTERIOR; - LinearRing shell = (LinearRing) poly.getExteriorRing(); + LinearRing shell = poly.getExteriorRing(); int shellLoc = locatePointInRing(p, shell); if (shellLoc != Location.INTERIOR) return shellLoc; // now test if the point lies in or on the holes for (int i = 0; i < poly.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) poly.getInteriorRingN(i); + LinearRing hole = poly.getInteriorRingN(i); int holeLoc = locatePointInRing(p, hole); if (holeLoc == Location.BOUNDARY) return Location.BOUNDARY; if (holeLoc == Location.INTERIOR) return Location.EXTERIOR; diff --git a/modules/core/src/main/java/org/locationtech/jts/geom/util/GeometryTransformer.java b/modules/core/src/main/java/org/locationtech/jts/geom/util/GeometryTransformer.java index 23cfe85d02..f58658c41b 100644 --- a/modules/core/src/main/java/org/locationtech/jts/geom/util/GeometryTransformer.java +++ b/modules/core/src/main/java/org/locationtech/jts/geom/util/GeometryTransformer.java @@ -240,7 +240,7 @@ protected Geometry transformMultiLineString(MultiLineString geom, Geometry paren protected Geometry transformPolygon(Polygon geom, Geometry parent) { boolean isAllValidLinearRings = true; - Geometry shell = transformLinearRing((LinearRing) geom.getExteriorRing(), geom); + Geometry shell = transformLinearRing(geom.getExteriorRing(), geom); if (shell == null || ! (shell instanceof LinearRing) @@ -249,7 +249,7 @@ protected Geometry transformPolygon(Polygon geom, Geometry parent) { ArrayList holes = new ArrayList(); for (int i = 0; i < geom.getNumInteriorRing(); i++) { - Geometry hole = transformLinearRing((LinearRing) geom.getInteriorRingN(i), geom); + Geometry hole = transformLinearRing(geom.getInteriorRingN(i), geom); if (hole == null || hole.isEmpty()) { continue; } diff --git a/modules/core/src/main/java/org/locationtech/jts/geomgraph/GeometryGraph.java b/modules/core/src/main/java/org/locationtech/jts/geomgraph/GeometryGraph.java index caa8e83175..b5bae0439e 100644 --- a/modules/core/src/main/java/org/locationtech/jts/geomgraph/GeometryGraph.java +++ b/modules/core/src/main/java/org/locationtech/jts/geomgraph/GeometryGraph.java @@ -268,12 +268,12 @@ private void addPolygonRing(LinearRing lr, int cwLeft, int cwRight) private void addPolygon(Polygon p) { addPolygonRing( - (LinearRing) p.getExteriorRing(), + p.getExteriorRing(), Location.EXTERIOR, Location.INTERIOR); for (int i = 0; i < p.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) p.getInteriorRingN(i); + LinearRing hole = p.getInteriorRingN(i); // Holes are topologically labelled opposite to the shell, since // the interior of the polygon lies on their opposite side diff --git a/modules/core/src/main/java/org/locationtech/jts/io/gml2/GMLWriter.java b/modules/core/src/main/java/org/locationtech/jts/io/gml2/GMLWriter.java index be41101a30..834546379a 100644 --- a/modules/core/src/main/java/org/locationtech/jts/io/gml2/GMLWriter.java +++ b/modules/core/src/main/java/org/locationtech/jts/io/gml2/GMLWriter.java @@ -270,7 +270,7 @@ private void writePolygon(Polygon p, Writer writer, int level) startLine(level + 1, writer); startGeomTag(GMLConstants.GML_OUTER_BOUNDARY_IS, null, writer); - writeLinearRing((LinearRing) p.getExteriorRing(), writer, level + 2); + writeLinearRing(p.getExteriorRing(), writer, level + 2); startLine(level + 1, writer); endGeomTag(GMLConstants.GML_OUTER_BOUNDARY_IS, writer); @@ -279,7 +279,7 @@ private void writePolygon(Polygon p, Writer writer, int level) startLine(level + 1, writer); startGeomTag(GMLConstants.GML_INNER_BOUNDARY_IS, null, writer); - writeLinearRing((LinearRing) p.getInteriorRingN(t), writer, level + 2); + writeLinearRing(p.getInteriorRingN(t), writer, level + 2); startLine(level + 1, writer); endGeomTag(GMLConstants.GML_INNER_BOUNDARY_IS, writer); diff --git a/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLWriter.java b/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLWriter.java index 4f9c15bd34..358c8a2ca7 100644 --- a/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLWriter.java +++ b/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLWriter.java @@ -299,12 +299,12 @@ private void writePolygon(Polygon p, String attributes, int level, writeModifiers(level, buf); startLine(" \n", level, buf); - writeLinearRing((LinearRing) p.getExteriorRing(), null, false, level + 1, buf); + writeLinearRing(p.getExteriorRing(), null, false, level + 1, buf); startLine(" \n", level, buf); for (int t = 0; t < p.getNumInteriorRing(); t++) { startLine(" \n", level, buf); - writeLinearRing((LinearRing) p.getInteriorRingN(t), null, false, level + 1, buf); + writeLinearRing(p.getInteriorRingN(t), null, false, level + 1, buf); startLine(" \n", level, buf); } diff --git a/modules/core/src/main/java/org/locationtech/jts/operation/buffer/OffsetCurveSetBuilder.java b/modules/core/src/main/java/org/locationtech/jts/operation/buffer/OffsetCurveSetBuilder.java index 938f4816a2..e50dce208c 100644 --- a/modules/core/src/main/java/org/locationtech/jts/operation/buffer/OffsetCurveSetBuilder.java +++ b/modules/core/src/main/java/org/locationtech/jts/operation/buffer/OffsetCurveSetBuilder.java @@ -156,7 +156,7 @@ private void addPolygon(Polygon p) offsetSide = Position.RIGHT; } - LinearRing shell = (LinearRing) p.getExteriorRing(); + LinearRing shell = p.getExteriorRing(); Coordinate[] shellCoord = CoordinateArrays.removeRepeatedPoints(shell.getCoordinates()); // optimization - don't bother computing buffer // if the polygon would be completely eroded @@ -175,7 +175,7 @@ private void addPolygon(Polygon p) for (int i = 0; i < p.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) p.getInteriorRingN(i); + LinearRing hole = p.getInteriorRingN(i); Coordinate[] holeCoord = CoordinateArrays.removeRepeatedPoints(hole.getCoordinates()); // optimization - don't bother computing buffer for this hole diff --git a/modules/core/src/main/java/org/locationtech/jts/operation/valid/IsValidOp.java b/modules/core/src/main/java/org/locationtech/jts/operation/valid/IsValidOp.java index ee1a2ef91e..5ab0509199 100644 --- a/modules/core/src/main/java/org/locationtech/jts/operation/valid/IsValidOp.java +++ b/modules/core/src/main/java/org/locationtech/jts/operation/valid/IsValidOp.java @@ -326,10 +326,10 @@ private void checkInvalidCoordinates(Polygon poly) private void checkClosedRings(Polygon poly) { - checkClosedRing((LinearRing) poly.getExteriorRing()); + checkClosedRing(poly.getExteriorRing()); if (validErr != null) return; for (int i = 0; i < poly.getNumInteriorRing(); i++) { - checkClosedRing((LinearRing) poly.getInteriorRingN(i)); + checkClosedRing(poly.getInteriorRingN(i)); if (validErr != null) return; } } @@ -439,7 +439,7 @@ private void checkNoSelfIntersectingRing(EdgeIntersectionList eiList) */ private void checkHolesInShell(Polygon p, GeometryGraph graph) { - LinearRing shell = (LinearRing) p.getExteriorRing(); + LinearRing shell = p.getExteriorRing(); //PointInRing pir = new SimplePointInRing(shell); //PointInRing pir = new SIRtreePointInRing(shell); @@ -448,7 +448,7 @@ private void checkHolesInShell(Polygon p, GeometryGraph graph) for (int i = 0; i < p.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) p.getInteriorRingN(i); + LinearRing hole = p.getInteriorRingN(i); Coordinate holePt = findPtNotNode(hole.getCoordinates(), shell, graph); /** * If no non-node hole vertex can be found, the hole must @@ -486,7 +486,7 @@ private void checkHolesNotNested(Polygon p, GeometryGraph graph) //SweeplineNestedRingTester nestedTester = new SweeplineNestedRingTester(arg[0]); for (int i = 0; i < p.getNumInteriorRing(); i++) { - LinearRing innerHole = (LinearRing) p.getInteriorRingN(i); + LinearRing innerHole = p.getInteriorRingN(i); nestedTester.add(innerHole); } boolean isNonNested = nestedTester.isNonNested(); @@ -513,7 +513,7 @@ private void checkShellsNotNested(MultiPolygon mp, GeometryGraph graph) { for (int i = 0; i < mp.getNumGeometries(); i++) { Polygon p = (Polygon) mp.getGeometryN(i); - LinearRing shell = (LinearRing) p.getExteriorRing(); + LinearRing shell = p.getExteriorRing(); for (int j = 0; j < mp.getNumGeometries(); j++) { if (i == j) continue; Polygon p2 = (Polygon) mp.getGeometryN(j); @@ -536,7 +536,7 @@ private void checkShellNotNested(LinearRing shell, Polygon p, GeometryGraph grap { Coordinate[] shellPts = shell.getCoordinates(); // test if shell is inside polygon shell - LinearRing polyShell = (LinearRing) p.getExteriorRing(); + LinearRing polyShell = p.getExteriorRing(); Coordinate[] polyPts = polyShell.getCoordinates(); Coordinate shellPt = findPtNotNode(shellPts, polyShell, graph); // if no point could be found, we can assume that the shell is outside the polygon @@ -561,7 +561,7 @@ private void checkShellNotNested(LinearRing shell, Polygon p, GeometryGraph grap */ Coordinate badNestedPt = null; for (int i = 0; i < p.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) p.getInteriorRingN(i); + LinearRing hole = p.getInteriorRingN(i); badNestedPt = checkShellInsideHole(shell, hole, graph); if (badNestedPt == null) return; diff --git a/modules/core/src/main/java/org/locationtech/jts/shape/fractal/SierpinskiCarpetBuilder.java b/modules/core/src/main/java/org/locationtech/jts/shape/fractal/SierpinskiCarpetBuilder.java index 73dbd7ce9a..c9d4075bf7 100644 --- a/modules/core/src/main/java/org/locationtech/jts/shape/fractal/SierpinskiCarpetBuilder.java +++ b/modules/core/src/main/java/org/locationtech/jts/shape/fractal/SierpinskiCarpetBuilder.java @@ -48,7 +48,7 @@ public Geometry getGeometry() LineSegment baseLine = getSquareBaseLine(); Coordinate origin = baseLine.getCoordinate(0); LinearRing[] holes = getHoles(level, origin.x, origin.y, getDiameter()); - LinearRing shell = (LinearRing) ((Polygon) geomFactory.toGeometry(getSquareExtent())).getExteriorRing(); + LinearRing shell = ((Polygon) geomFactory.toGeometry(getSquareExtent())).getExteriorRing(); return geomFactory.createPolygon( shell, holes); } diff --git a/modules/core/src/test/java/org/locationtech/jts/operation/valid/ValidClosedRingTest.java b/modules/core/src/test/java/org/locationtech/jts/operation/valid/ValidClosedRingTest.java index 2ae1d65ac1..17984974cf 100644 --- a/modules/core/src/test/java/org/locationtech/jts/operation/valid/ValidClosedRingTest.java +++ b/modules/core/src/test/java/org/locationtech/jts/operation/valid/ValidClosedRingTest.java @@ -58,14 +58,14 @@ public void testGoodLinearRing() public void testBadPolygonShell() { Polygon poly = (Polygon) fromWKT("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0))"); - updateNonClosedRing((LinearRing) poly.getExteriorRing()); + updateNonClosedRing(poly.getExteriorRing()); checkIsValid(poly, false); } public void testBadPolygonHole() { Polygon poly = (Polygon) fromWKT("POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1) ))"); - updateNonClosedRing((LinearRing) poly.getInteriorRingN(0)); + updateNonClosedRing(poly.getInteriorRingN(0)); checkIsValid(poly, false); } @@ -79,7 +79,7 @@ public void testBadGeometryCollection() { GeometryCollection gc = (GeometryCollection) fromWKT("GEOMETRYCOLLECTION ( POLYGON ((0 0, 0 10, 10 10, 10 0, 0 0), (1 1, 2 1, 2 2, 1 2, 1 1) )), POINT(0 0) )"); Polygon poly = (Polygon) gc.getGeometryN(0); - updateNonClosedRing((LinearRing) poly.getInteriorRingN(0)); + updateNonClosedRing(poly.getInteriorRingN(0)); checkIsValid(poly, false); } diff --git a/modules/lab/src/main/java/org/locationtech/jtslab/clean/HoleRemover.java b/modules/lab/src/main/java/org/locationtech/jtslab/clean/HoleRemover.java index 276a182c20..14767c47eb 100644 --- a/modules/lab/src/main/java/org/locationtech/jtslab/clean/HoleRemover.java +++ b/modules/lab/src/main/java/org/locationtech/jtslab/clean/HoleRemover.java @@ -82,11 +82,11 @@ public PolygonHoleRemover(Polygon poly, Predicate isRemoved) { public Polygon getResult() { GeometryFactory gf = poly.getFactory(); - Polygon shell = gf.createPolygon((LinearRing) poly.getExteriorRing()); + Polygon shell = gf.createPolygon(poly.getExteriorRing()); List holes = new ArrayList(); for (int i = 0; i < poly.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) poly.getInteriorRingN(i); + LinearRing hole = poly.getInteriorRingN(i); if (! isRemoved.value(hole)) { holes.add(hole); } @@ -96,7 +96,7 @@ public Polygon getResult() return poly; // return new polygon with covered holes only - Polygon result = gf.createPolygon((LinearRing) poly.getExteriorRing(), + Polygon result = gf.createPolygon(poly.getExteriorRing(), GeometryFactory.toLinearRingArray(holes)); return result; } diff --git a/modules/lab/src/main/java/org/locationtech/jtslab/clean/InvalidHoleRemover.java b/modules/lab/src/main/java/org/locationtech/jtslab/clean/InvalidHoleRemover.java index 6e9c79f803..c32118d088 100644 --- a/modules/lab/src/main/java/org/locationtech/jtslab/clean/InvalidHoleRemover.java +++ b/modules/lab/src/main/java/org/locationtech/jtslab/clean/InvalidHoleRemover.java @@ -95,12 +95,12 @@ public PolygonInvalidHoleRemover(Polygon poly) { public Polygon getResult() { GeometryFactory gf = poly.getFactory(); - Polygon shell = gf.createPolygon((LinearRing) poly.getExteriorRing()); + Polygon shell = gf.createPolygon(poly.getExteriorRing()); PreparedGeometry shellPrep = PreparedGeometryFactory.prepare(shell); List holes = new ArrayList(); for (int i = 0; i < poly.getNumInteriorRing(); i++) { - LinearRing hole = (LinearRing) poly.getInteriorRingN(i); + LinearRing hole = poly.getInteriorRingN(i); if (shellPrep.covers(hole)) { holes.add(hole); } @@ -110,7 +110,7 @@ public Polygon getResult() return poly; // return new polygon with covered holes only - Polygon result = gf.createPolygon((LinearRing) poly.getExteriorRing(), + Polygon result = gf.createPolygon(poly.getExteriorRing(), GeometryFactory.toLinearRingArray(holes)); return result; }