|
15 | 15 | import org.locationtech.jts.geom.Geometry; |
16 | 16 | import org.locationtech.jts.geom.GeometryCollection; |
17 | 17 | import org.locationtech.jts.geom.GeometryFactory; |
| 18 | +import org.locationtech.jts.geom.LineString; |
| 19 | +import org.locationtech.jts.geom.LinearRing; |
18 | 20 | import org.locationtech.jts.geom.Polygon; |
19 | 21 | import org.locationtech.jts.geom.PrecisionModel; |
20 | 22 |
|
@@ -533,18 +535,18 @@ public void testBowtiePolygonHoleLargestAreaRetained() { |
533 | 535 |
|
534 | 536 | public void testPolygon4NegBufferEmpty() { |
535 | 537 | String wkt = "POLYGON ((666360.09 429614.71, 666344.4 429597.12, 666358.47 429584.52, 666374.5 429602.33, 666360.09 429614.71))"; |
536 | | - checkBufferEmpty(wkt, -9, false); |
537 | | - checkBufferEmpty(wkt, -10, true); |
538 | | - checkBufferEmpty(wkt, -15, true); |
539 | | - checkBufferEmpty(wkt, -18, true); |
| 538 | + checkBufferPolygonEmpty(wkt, -9, false); |
| 539 | + checkBufferPolygonEmpty(wkt, -10, true); |
| 540 | + checkBufferPolygonEmpty(wkt, -15, true); |
| 541 | + checkBufferPolygonEmpty(wkt, -18, true); |
540 | 542 | } |
541 | 543 |
|
542 | 544 | public void testPolygon5NegBufferEmpty() { |
543 | 545 | String wkt = "POLYGON ((6 20, 16 20, 21 9, 9 0, 0 10, 6 20))"; |
544 | | - checkBufferEmpty(wkt, -8, false); |
545 | | - checkBufferEmpty(wkt, -8.6, true); |
546 | | - checkBufferEmpty(wkt, -9.6, true); |
547 | | - checkBufferEmpty(wkt, -11, true); |
| 546 | + checkBufferPolygonEmpty(wkt, -8, false); |
| 547 | + checkBufferPolygonEmpty(wkt, -8.6, true); |
| 548 | + checkBufferPolygonEmpty(wkt, -9.6, true); |
| 549 | + checkBufferPolygonEmpty(wkt, -11, true); |
548 | 550 | } |
549 | 551 |
|
550 | 552 | public void testPolygonHole5BufferNoHole() { |
@@ -682,8 +684,54 @@ public void testArtifactsRemovedFromLineBufferFlatEnd() { |
682 | 684 | assertEquals(1, buf.getNumGeometries()); |
683 | 685 | } |
684 | 686 |
|
| 687 | + //-------------------------------- |
| 688 | + |
| 689 | + public void testInvalidCoordPoint() { |
| 690 | + // works for Inf ordinates as well |
| 691 | + Geometry geom = read("POINT (NaN NaN)"); |
| 692 | + checkBufferPolygonEmpty(geom, 1, true); |
| 693 | + } |
| 694 | + |
| 695 | + public void testInvalidCoordsLine() { |
| 696 | + // works for Inf ordinates as well |
| 697 | + Geometry geom = read("LINESTRING (NaN NaN, NaN NaN)"); |
| 698 | + checkBufferPolygonEmpty(geom, 1, true); |
| 699 | + } |
| 700 | + |
| 701 | + public void testInvalidCoordShell() { |
| 702 | + // using Inf ordinates creates a valid ring with equal endpoints |
| 703 | + // this would be simpler if JTS WKT supported Inf |
| 704 | + Geometry geom = getGeometryFactory().createPolygon( infCoords(5) ); |
| 705 | + checkBufferPolygonEmpty(geom, 1, true); |
| 706 | + } |
| 707 | + |
| 708 | + public void testInvalidCoordHole() { |
| 709 | + // using Inf ordinates creates a valid ring with equal endpoints |
| 710 | + // this would be simpler if JTS WKT supported Inf |
| 711 | + Polygon poly = (Polygon) read("POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9), (3 7, 7 7, 7 3, 3 3, 3 7))"); |
| 712 | + |
| 713 | + LinearRing shell = poly.getExteriorRing(); |
| 714 | + LinearRing hole = poly.getInteriorRingN(0); |
| 715 | + LinearRing infHole = getGeometryFactory().createLinearRing( infCoords(5) ); |
| 716 | + Geometry polyInfHole = getGeometryFactory().createPolygon( |
| 717 | + shell, new LinearRing[] { hole, infHole } ); |
| 718 | + |
| 719 | + Geometry bufferOrig = poly.buffer(1); |
| 720 | + Geometry bufferInf = polyInfHole.buffer(1); |
| 721 | + // buffers should be same since inf hole is skipped |
| 722 | + checkEqual(bufferOrig, bufferInf); |
| 723 | + } |
| 724 | + |
685 | 725 | //=================================================== |
686 | 726 |
|
| 727 | + private static Coordinate[] infCoords(int size) { |
| 728 | + Coordinate[] coords = new Coordinate[size]; |
| 729 | + for (int i = 0; i < size; i++) { |
| 730 | + coords[i] = new Coordinate(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); |
| 731 | + } |
| 732 | + return coords; |
| 733 | + } |
| 734 | + |
687 | 735 | private static BufferParameters bufParamRoundMitre(double mitreLimit) { |
688 | 736 | BufferParameters param = new BufferParameters(); |
689 | 737 | param.setJoinStyle(BufferParameters.JOIN_MITRE); |
@@ -711,9 +759,14 @@ private void checkBuffer(String wkt, double dist, String wktExpected) { |
711 | 759 | checkEqual(expected, result, 0.01); |
712 | 760 | } |
713 | 761 |
|
714 | | - private void checkBufferEmpty(String wkt, double dist, boolean isEmptyExpected) { |
| 762 | + private void checkBufferPolygonEmpty(String wkt, double dist, boolean isEmptyExpected) { |
715 | 763 | Geometry a = read(wkt); |
716 | | - Geometry result = a.buffer(dist); |
| 764 | + checkBufferPolygonEmpty(a, dist, isEmptyExpected); |
| 765 | + } |
| 766 | + |
| 767 | + private void checkBufferPolygonEmpty(Geometry geom, double dist, boolean isEmptyExpected) { |
| 768 | + Geometry result = geom.buffer(dist); |
| 769 | + assertTrue(result instanceof Polygon); |
717 | 770 | assertTrue(isEmptyExpected == result.isEmpty()); |
718 | 771 | } |
719 | 772 |
|
|
0 commit comments