-
-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathgetAreaOfPolygon.ts
44 lines (36 loc) · 1.33 KB
/
getAreaOfPolygon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import toRad from './toRad';
import getLatitude from './getLatitude';
import getLongitude from './getLongitude';
import { earthRadius } from './constants';
import { GeolibInputCoordinates } from './types';
// Calculates the surface area of a polygon.
const getAreaOfPolygon = (points: GeolibInputCoordinates[]) => {
let area = 0;
if (points.length > 2) {
let lowerIndex;
let middleIndex;
let upperIndex;
for (let i = 0; i < points.length; i++) {
if (i === points.length - 2) {
lowerIndex = points.length - 2;
middleIndex = points.length - 1;
upperIndex = 0;
} else if (i === points.length - 1) {
lowerIndex = points.length - 1;
middleIndex = 0;
upperIndex = 1;
} else {
lowerIndex = i;
middleIndex = i + 1;
upperIndex = i + 2;
}
const p1lon = getLongitude(points[lowerIndex]);
const p2lat = getLatitude(points[middleIndex]);
const p3lon = getLongitude(points[upperIndex]);
area += (toRad(p3lon) - toRad(p1lon)) * Math.sin(toRad(p2lat));
}
area = (area * earthRadius * earthRadius) / 2;
}
return Math.abs(area);
};
export default getAreaOfPolygon;