A few functions to get a polygon area, centroid and label location
npm install polygon-property-utilsjavascript
import {
polygonArea,
polygonCentroid,
isPointInPolygon,
polygonInternalLabel,
} from "polygon-property-utils";
`
Functions
$3
Calculates the area of a polygon provided as an array of Position coordinates. The function requires that the polygon is closed, i.e., the last coordinate is the same as the first.
#### Parameters:
- coordinates: Array of [longitude, latitude] pairs.
#### Returns:
- The area of the polygon in square meters (m^2).
$3
Calculates the centroid (geometric center) of a polygon defined by an array of Position coordinates.
#### Parameters:
- coordinates: Array of [longitude, latitude] pairs.
#### Returns:
- A Position array [longitude, latitude] representing the centroid, or null if the polygon is not closed.
$3
Determines whether a given point is inside a polygon.
#### Parameters:
- point: A Position [longitude, latitude].
- coordinates: Array of [longitude, latitude] pairs defining the polygon.
#### Returns:
- true if the point is inside the polygon, false otherwise.
$3
Calculates an ideal point for labeling purposes inside a polygon. (Locates closest boundary from the centroid and draws a line through the polygon to find the next intersection and returns a location 1/3 distance from the first intersection towards the second intersection.)
#### Parameters:
- coordinates: Array of [longitude, latitude] pairs defining the polygon.
#### Returns:
- A Position [longitude, latitude] that is suitable for placing a label inside the polygon, or null if no suitable point can be found.
Types
$3
A tuple representing a geographical coordinate, where the first element is the longitude and the second is the latitude.
`typescript
type Position = [number, number];
``