From ab47db4feef29f7f5739a21988c40a83755359f5 Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Wed, 23 Feb 2022 13:51:44 +0100 Subject: [PATCH] LUCENE-10437: Improve error message in the Tessellator for polygon with all points collinear (#703) Polygon tessellator throws a more informative error message when the provided polygon does not contain enough no-collinear points. --- lucene/CHANGES.txt | 3 +++ .../core/src/java/org/apache/lucene/geo/Tessellator.java | 6 ++++++ .../src/test/org/apache/lucene/geo/TestTessellator.java | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index cc89bc29fb6..50f8b18db8d 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -301,6 +301,9 @@ Other * LUCENE-10413: Make Ukrainian default stop words list available as a public getter. (Alan Woodward) +* LUCENE-10437: Polygon tessellator throws a more informative error message when the provided polygon + does not contain enough no-collinear points. (Ignacio Vera) + ======================= Lucene 9.0.0 ======================= New Features diff --git a/lucene/core/src/java/org/apache/lucene/geo/Tessellator.java b/lucene/core/src/java/org/apache/lucene/geo/Tessellator.java index 0990dc640b8..2ee19eaba96 100644 --- a/lucene/core/src/java/org/apache/lucene/geo/Tessellator.java +++ b/lucene/core/src/java/org/apache/lucene/geo/Tessellator.java @@ -102,6 +102,9 @@ public final class Tessellator { if (outerNode == null) { throw new IllegalArgumentException("Malformed shape detected in Tessellator!"); } + if (outerNode == outerNode.next || outerNode == outerNode.next.next) { + throw new IllegalArgumentException("at least three non-collinear points required"); + } // Determine if the specified list of points contains holes if (polygon.numHoles() > 0) { @@ -154,6 +157,9 @@ public final class Tessellator { if (outerNode == null) { throw new IllegalArgumentException("Malformed shape detected in Tessellator!"); } + if (outerNode == outerNode.next || outerNode == outerNode.next.next) { + throw new IllegalArgumentException("at least three non-collinear points required"); + } // Determine if the specified list of points contains holes if (polygon.numHoles() > 0) { diff --git a/lucene/core/src/test/org/apache/lucene/geo/TestTessellator.java b/lucene/core/src/test/org/apache/lucene/geo/TestTessellator.java index 7162814ada2..c4d52d9eba7 100644 --- a/lucene/core/src/test/org/apache/lucene/geo/TestTessellator.java +++ b/lucene/core/src/test/org/apache/lucene/geo/TestTessellator.java @@ -169,6 +169,15 @@ public class TestTessellator extends LuceneTestCase { checkPolygon(wkt); } + public void testInvalidPolygonCollinear() throws Exception { + String wkt = + "POLYGON ((18.9401790919516 -33.9681188869036, 18.9401790919516 -33.9681188869036, 18.9401790919517 -33.9681188869036, 18.9401790919517 -33.9681188869036, 18.9401790919516 -33.9681188869036))"; + Polygon polygon = (Polygon) SimpleWKTShapeParser.parse(wkt); + IllegalArgumentException ex = + expectThrows(IllegalArgumentException.class, () -> Tessellator.tessellate(polygon, true)); + assertEquals("at least three non-collinear points required", ex.getMessage()); + } + public void testComplexPolygon01() throws Exception { String wkt = "POLYGON((58.8792517 54.9160937, 58.8762477 54.9154524, 58.8735011 54.9140217, 58.8726428 54.9127389, 58.8731146 54.9122507, 58.8741877 54.9120482, 58.8771918 54.9117028, 58.88011 54.913331, 58.8801175 54.9137036, 58.8805885 54.9143186, 58.8807109 54.9148604, 58.88011 54.915551, 58.8792517 54.9160937), "