@@ -299,7 +299,7 @@ class GeoGeometry {
299
299
* double.
300
300
*
301
301
* @param d a double
302
- * @param decimals the number of desired decimals after the .
302
+ * @param decimals the number of desired decimals after the dot
303
303
* @return d rounded to the specified precision
304
304
*/
305
305
fun roundToDecimals (d : Double , decimals : Int ): Double {
@@ -589,6 +589,23 @@ class GeoGeometry {
589
589
return distance(p1[1 ], p1[0 ], p2[1 ], p2[0 ])
590
590
}
591
591
592
+ /* *
593
+ * Cheap but not very precise over larger distances
594
+ */
595
+ fun equirectangularDistance (
596
+ lat1 : Double ,
597
+ lon1 : Double ,
598
+ lat2 : Double ,
599
+ lon2 : Double
600
+ ): Double {
601
+ val x = toRadians(lon2 - lon1) * cos(toRadians((lat1 + lat2) / 2 ))
602
+ val y = toRadians(lat2 - lat1)
603
+ return sqrt(x * x + y * y) * EARTH_RADIUS_METERS
604
+ }
605
+
606
+ fun equirectangularDistance (p1 : PointCoordinates , p2 : PointCoordinates ): Double =
607
+ equirectangularDistance(p1[1 ], p1[0 ], p2[1 ], p2[0 ])
608
+
592
609
/* *
593
610
* Calculate distance in meters using the Vicenty algorithm.
594
611
*
@@ -675,7 +692,7 @@ class GeoGeometry {
675
692
* @param p point
676
693
* @return the distance of the point to the line
677
694
*/
678
- fun distance (l1 : PointCoordinates , l2 : PointCoordinates , p : PointCoordinates ): Double {
695
+ fun distanceToLine (l1 : PointCoordinates , l2 : PointCoordinates , p : PointCoordinates ): Double {
679
696
return distance(l1[1 ], l1[0 ], l2[1 ], l2[0 ], p[1 ], p[0 ])
680
697
}
681
698
@@ -695,7 +712,7 @@ class GeoGeometry {
695
712
var last = lineStringCoordinates[0 ]
696
713
for (i in 1 until lineStringCoordinates.size) {
697
714
val current = lineStringCoordinates[i]
698
- val distance = distance (last, current, pointCoordinates)
715
+ val distance = distanceToLine (last, current, pointCoordinates)
699
716
minDistance = min(minDistance, distance)
700
717
last = current
701
718
}
@@ -1356,11 +1373,11 @@ class GeoGeometry {
1356
1373
var dmax = 0.0
1357
1374
var index = 0
1358
1375
if (points.size == 3 ) {
1359
- dmax = distance (points[0 ], points[points.size - 1 ], points[1 ]) // edge case
1376
+ dmax = distanceToLine (points[0 ], points[points.size - 1 ], points[1 ]) // edge case
1360
1377
}
1361
1378
1362
1379
for (i in 2 until points.size - 1 ) {
1363
- val d = distance (points[0 ], points[points.size - 1 ], points[i])
1380
+ val d = distanceToLine (points[0 ], points[points.size - 1 ], points[i])
1364
1381
if (d > dmax) {
1365
1382
index = i
1366
1383
dmax = d
0 commit comments