Skip to content

Commit ba07e4f

Browse files
committed
fix: bug fix for ST_LengthSpheroid
1 parent 9706369 commit ba07e4f

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

common/src/main/java/org/apache/sedona/common/sphere/Spheroid.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.locationtech.jts.geom.Coordinate;
2828
import org.locationtech.jts.geom.Envelope;
2929
import org.locationtech.jts.geom.Geometry;
30+
import org.locationtech.jts.geom.Polygon;
3031

3132
public class Spheroid {
3233
// Standard EPSG Codes
@@ -79,7 +80,8 @@ public static double distance(Geometry geom1, Geometry geom2) {
7980
*/
8081
public static double length(Geometry geom) {
8182
String geomType = geom.getGeometryType();
82-
if (geomType.equals("Polygon") || geomType.equals("LineString")) {
83+
if (geomType.equals(Geometry.TYPENAME_LINEARRING)
84+
|| geomType.equals(Geometry.TYPENAME_LINESTRING)) {
8385
PolygonArea p = new PolygonArea(Geodesic.WGS84, true);
8486
Coordinate[] coordinates = geom.getCoordinates();
8587
for (int i = 0; i < coordinates.length; i++) {
@@ -89,9 +91,20 @@ public static double length(Geometry geom) {
8991
}
9092
PolygonResult compute = p.Compute();
9193
return compute.perimeter;
92-
} else if (geomType.equals("MultiPolygon")
93-
|| geomType.equals("MultiLineString")
94-
|| geomType.equals("GeometryCollection")) {
94+
} else if (geomType.equals(Geometry.TYPENAME_POLYGON)) {
95+
Polygon poly = (Polygon) geom;
96+
double length = length(poly.getExteriorRing());
97+
98+
if (poly.getNumInteriorRing() > 0) {
99+
for (int i = 0; i < poly.getNumInteriorRing(); i++) {
100+
length += length(poly.getInteriorRingN(i));
101+
}
102+
}
103+
104+
return length;
105+
} else if (geomType.equals(Geometry.TYPENAME_MULTIPOLYGON)
106+
|| geomType.equals(Geometry.TYPENAME_MULTILINESTRING)
107+
|| geomType.equals(Geometry.TYPENAME_GEOMETRYCOLLECTION)) {
95108
double length = 0.0;
96109
for (int i = 0; i < geom.getNumGeometries(); i++) {
97110
length += length(geom.getGeometryN(i));

common/src/test/java/org/apache/sedona/common/FunctionsTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,30 +1614,36 @@ public void delaunayTriangles() throws ParseException {
16141614
}
16151615

16161616
@Test
1617-
public void spheroidLength() {
1617+
public void spheroidLength() throws ParseException {
16181618
Point point = GEOMETRY_FACTORY.createPoint(new Coordinate(90, 0));
1619-
assertEquals(0, Spheroid.length(point), 0.1);
1619+
assertEquals(0, Spheroid.length(point), FP_TOLERANCE2);
16201620

16211621
LineString line = GEOMETRY_FACTORY.createLineString(coordArray(0, 0, 90, 0));
1622-
assertEquals(1.0018754171394622E7, Spheroid.length(line), 0.1);
1622+
assertEquals(1.0018754171394622E7, Spheroid.length(line), FP_TOLERANCE2);
16231623

16241624
Polygon polygon = GEOMETRY_FACTORY.createPolygon(coordArray(0, 0, 90, 0, 0, 0));
1625-
assertEquals(2.0037508342789244E7, Spheroid.length(polygon), 0.1);
1625+
assertEquals(2.0037508342789244E7, Spheroid.length(polygon), FP_TOLERANCE2);
16261626

16271627
MultiPoint multiPoint = GEOMETRY_FACTORY.createMultiPoint(new Point[] {point, point});
1628-
assertEquals(0, Spheroid.length(multiPoint), 0.1);
1628+
assertEquals(0, Spheroid.length(multiPoint), FP_TOLERANCE2);
16291629

16301630
MultiLineString multiLineString =
16311631
GEOMETRY_FACTORY.createMultiLineString(new LineString[] {line, line});
1632-
assertEquals(2.0037508342789244E7, Spheroid.length(multiLineString), 0.1);
1632+
assertEquals(2.0037508342789244E7, Spheroid.length(multiLineString), FP_TOLERANCE2);
16331633

16341634
MultiPolygon multiPolygon =
16351635
GEOMETRY_FACTORY.createMultiPolygon(new Polygon[] {polygon, polygon});
1636-
assertEquals(4.007501668557849E7, Spheroid.length(multiPolygon), 0.1);
1636+
assertEquals(4.007501668557849E7, Spheroid.length(multiPolygon), FP_TOLERANCE2);
16371637

16381638
GeometryCollection geometryCollection =
16391639
GEOMETRY_FACTORY.createGeometryCollection(new Geometry[] {point, line, multiLineString});
1640-
assertEquals(3.0056262514183864E7, Spheroid.length(geometryCollection), 0.1);
1640+
assertEquals(3.0056262514183864E7, Spheroid.length(geometryCollection), FP_TOLERANCE2);
1641+
1642+
Geometry polygonWithHole =
1643+
Constructors.geomFromWKT(
1644+
"POLYGON((-122.33 47.61, -122.32 47.62, -122.31 47.61, -122.30 47.62, -122.29 47.61, -122.30 47.60, -122.31 47.59, -122.32 47.60, -122.33 47.61), (-122.315 47.605, -122.305 47.615, -122.295 47.605, -122.305 47.595, -122.315 47.605))",
1645+
4326);
1646+
assertEquals(16106.506409488933, Spheroid.length(polygonWithHole), FP_TOLERANCE2);
16411647
}
16421648

16431649
@Test

0 commit comments

Comments
 (0)