This is an automated email from the ASF dual-hosted git repository. jiayu pushed a commit to branch SEDONA-589 in repository https://gitbox.apache.org/repos/asf/sedona.git
commit 717a242cf7d584a410614845a90185e451f16c0e Author: Furqaan Khan <[email protected]> AuthorDate: Fri Apr 26 15:06:38 2024 -0400 fix: bug of ST_LongestLine (#173) * temp commit * Revert "temp commit" This reverts commit 8cd8ecc6b3ae533379b779ba08c23b12df47e2fc. * feat: Add ST_LongestLine * fix: bug that would cause null pointer exception --- .../java/org/apache/sedona/common/Functions.java | 18 +++++++++++ .../org/apache/sedona/common/FunctionsTest.java | 37 ++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/common/src/main/java/org/apache/sedona/common/Functions.java b/common/src/main/java/org/apache/sedona/common/Functions.java index ac88ce04f..7787013cd 100644 --- a/common/src/main/java/org/apache/sedona/common/Functions.java +++ b/common/src/main/java/org/apache/sedona/common/Functions.java @@ -1062,6 +1062,24 @@ public class Functions { return isExteriorRingCCW && isInteriorRingCCW; } + public static Geometry longestLine(Geometry geom1, Geometry geom2) { + double maxLength = - Double.MAX_VALUE; + Coordinate longestStart = null; + Coordinate longestEnd = null; + + for (Coordinate coord1: geom1.getCoordinates()) { + for (Coordinate coord2: geom2.getCoordinates()) { + double length = coord1.distance(coord2); + if (length > maxLength) { + maxLength = length; + longestStart = coord1; + longestEnd = coord2; + } + } + } + return geom1.getFactory().createLineString(new Coordinate[] {longestStart, longestEnd}); + } + public static Geometry difference(Geometry leftGeometry, Geometry rightGeometry) { boolean isIntersects = leftGeometry.intersects(rightGeometry); if (!isIntersects) { diff --git a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java index 017f65e64..e50e34192 100644 --- a/common/src/test/java/org/apache/sedona/common/FunctionsTest.java +++ b/common/src/test/java/org/apache/sedona/common/FunctionsTest.java @@ -1012,6 +1012,43 @@ public class FunctionsTest extends TestBase { assertEquals("Median failed to converge within 1.0E-06 after 5 iterations.", e.getMessage()); } + @Test + public void longestLine() throws ParseException { + Geometry geom1 = Constructors.geomFromWKT("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))", 0); + Geometry geom2 = Functions.buffer(Constructors.geomFromWKT("POINT (10.123456 -20.654321)", 0), 30); + String actual = Functions.asWKT(Functions.longestLine(geom1, geom2)); + String expected = "LINESTRING (40 40, -1.3570469709526929 -48.37070697533861)"; + assertEquals(expected, actual); + + geom1 = Constructors.geomFromWKT("POLYGON ((190 150, 20 10, 160 70, 190 150))", 0); + geom2 = Constructors.geomFromWKT("POINT(80 160)", 0); + actual = Functions.asWKT(Functions.reducePrecision(Functions.longestLine(geom1, Functions.buffer(geom2, 30)), 8)); + expected = "LINESTRING (20 10, 91.48050297 187.71638598)"; + assertEquals(expected, actual); + + geom1 = Constructors.geomFromWKT("POINT (160 40)", 0); + geom2 = Constructors.geomFromWKT("LINESTRING (10 30, 50 50, 30 110, 70 90, 180 140, 130 190)", 0); + actual = Functions.asWKT(Functions.longestLine(geom1, geom2)); + expected = "LINESTRING (160 40, 130 190)"; + assertEquals(expected, actual); + + geom1 = Constructors.geomFromWKT("POLYGON ((40 180, 110 160, 180 180, 180 120, 140 90, 160 40, 80 10, 70 40, 20 50, 40 180),(60 140, 99 77.5, 90 140, 60 140))", 0); + actual = Functions.asWKT(Functions.normalize(Functions.longestLine(geom1, geom1))); + expected = "LINESTRING (20 50, 180 180)"; + assertEquals(expected, actual); + + geom1 = Constructors.geomFromWKT("POINT Z (10 20 5)", 0); + geom2 = Constructors.geomFromWKT("POLYGON Z ((30 40 10, 40 50 15, 50 60 20, 30 40 10))", 0); + actual = Functions.asWKT(Functions.longestLine(geom1, geom2)); + expected = "LINESTRING Z(10 20 5, 50 60 20)"; + assertEquals(expected, actual); + + geom1 = Constructors.geomFromWKT("POINT (0 0)", 0); + actual = Functions.asWKT(Functions.longestLine(geom1, geom1)); + expected = "LINESTRING (0 0, 0 0)"; + assertEquals(expected, actual); + } + @Test public void makepolygonWithSRID() { Geometry lineString1 = GEOMETRY_FACTORY.createLineString(coordArray(0, 0, 1, 1, 1, 0, 0, 0));
