Spaces:
Runtime error
Runtime error
Commit
·
66f237b
1
Parent(s):
59275a8
Fix geodesicArea calculator
Browse filesSomething weird was going on, a huge 5M sq meter offset for area was there. Got rid of my function and used something more standnard, which gives accurate results. I approximated India with 30 points, with area of 2.8 sqkm, which is fair enough
- frontend/src/utils/mapUtils.js +16 -16
frontend/src/utils/mapUtils.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import
|
2 |
// Haversine-based geodesic interpolator
|
3 |
function generateGeodesicPoints(lat1, lon1, lat2, lon2, numPoints = 512) {
|
4 |
/**
|
@@ -46,27 +46,27 @@ function generateGeodesicPoints(lat1, lon1, lat2, lon2, numPoints = 512) {
|
|
46 |
}
|
47 |
|
48 |
|
49 |
-
|
50 |
function calculatePolygonArea(coords) {
|
51 |
-
|
52 |
-
* @param {Array<Array<number>>} coords - Array of [lat, lon] pairs.
|
53 |
-
* @returns {number} Area in square meters.
|
54 |
-
*/
|
55 |
-
// console.log(coords); // Lifesaver
|
56 |
-
const geod = geodesic.Geodesic.WGS84;
|
57 |
-
const poly = geod.Polygon(false); // false = polygon, not polyline
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
}
|
62 |
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
}
|
69 |
|
|
|
70 |
function getPolygonCentroid(points) {
|
71 |
// Simple centroid calculation for small polygons
|
72 |
let x = 0, y = 0, n = points.length;
|
|
|
1 |
+
import LatLon from 'geodesy/latlon-spherical.js';
|
2 |
// Haversine-based geodesic interpolator
|
3 |
function generateGeodesicPoints(lat1, lon1, lat2, lon2, numPoints = 512) {
|
4 |
/**
|
|
|
46 |
}
|
47 |
|
48 |
|
|
|
49 |
function calculatePolygonArea(coords) {
|
50 |
+
if (!coords || coords.length < 3) return { area: 0, perimeter: 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
let area = 0;
|
53 |
+
let perimeter = 0;
|
|
|
54 |
|
55 |
+
const latlonPoints = coords.map(c => new LatLon(c[0], c[1]));
|
56 |
|
57 |
+
for (let i = 0; i < latlonPoints.length; i++) {
|
58 |
+
const p1 = latlonPoints[i];
|
59 |
+
const p2 = latlonPoints[(i + 1) % latlonPoints.length];
|
60 |
+
|
61 |
+
perimeter += p1.distanceTo(p2); // in meters
|
62 |
+
}
|
63 |
+
|
64 |
+
area = LatLon.areaOf(latlonPoints); // in square meters
|
65 |
+
|
66 |
+
return { "area":area, "perimeter": perimeter };
|
67 |
}
|
68 |
|
69 |
+
|
70 |
function getPolygonCentroid(points) {
|
71 |
// Simple centroid calculation for small polygons
|
72 |
let x = 0, y = 0, n = points.length;
|