Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@turf/centroid
Advanced tools
@turf/centroid is a module within the Turf.js library that calculates the centroid of a given GeoJSON feature. This is useful for finding the geometric center of various shapes such as polygons, lines, and multi-geometries.
Calculate Centroid of a Polygon
This feature calculates the centroid of a polygon. The code sample demonstrates how to create a polygon and then calculate its centroid using the @turf/centroid package.
const turf = require('@turf/centroid');
const polygon = turf.polygon([[
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41],
[-81, 41]
]]);
const centroid = turf.centroid(polygon);
console.log(centroid);
Calculate Centroid of a LineString
This feature calculates the centroid of a LineString. The code sample demonstrates how to create a LineString and then calculate its centroid using the @turf/centroid package.
const turf = require('@turf/centroid');
const line = turf.lineString([
[-83, 30],
[-84, 36],
[-78, 41]
]);
const centroid = turf.centroid(line);
console.log(centroid);
Calculate Centroid of a MultiPolygon
This feature calculates the centroid of a MultiPolygon. The code sample demonstrates how to create a MultiPolygon and then calculate its centroid using the @turf/centroid package.
const turf = require('@turf/centroid');
const multiPolygon = turf.multiPolygon([[
[[-81, 41], [-81, 47], [-72, 47], [-72, 41], [-81, 41]]
], [
[[-73, 39], [-73, 45], [-64, 45], [-64, 39], [-73, 39]]
]]);
const centroid = turf.centroid(multiPolygon);
console.log(centroid);
Geolib is a library for geospatial calculations. It provides functions to calculate distances, areas, and other geometric properties. While it offers centroid calculations, it is more general-purpose compared to @turf/centroid, which is specifically designed for GeoJSON features.
JSTS (JavaScript Topology Suite) is a library for performing various geometric operations. It includes functions for calculating centroids, but it is more complex and offers a broader range of geometric operations compared to @turf/centroid.
Takes one or more features and calculates the centroid using the mean of all vertices. This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons.
Parameters
features
(Feature | FeatureCollection) input featuresExamples
var polygon = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [[[-81, 41], [-88, 36], [-84, 31], [-80, 33], [-77, 39], [-81, 41]]]
}
};
var centroid = turf.centroid(polygon);
//addToMap
var addToMap = [polygon, centroid]
Returns Feature<Point> the centroid of the input features
This module is part of the Turfjs project, an open source module collection dedicated to geographic algorithms. It is maintained in the Turfjs/turf repository, where you can create PRs and issues.
Install this module individually:
$ npm install @turf/centroid
Or install the Turf module that includes it as a function:
$ npm install @turf/turf
4.3.0
@turf/polygon-tangents
Finds the tangents of a (Multi)Polygon from a Point. (https://github.com/Turfjs/turf/pull/708)@turf/rewind
Rewind LineString or Polygon outer ring ~clockwise~ counter-clockwise and inner rings ~counterclockwise~ clockwise (https://github.com/Turfjs/turf/pull/700 & https://github.com/Turfjs/turf/issues/66)@turf/isobands
- Takes a grid FeatureCollection of Point features with z-values and an array of value breaks and generates filled contour isobands. (https://github.com/Turfjs/turf/pull/619)@turf/helpers
(https://github.com/Turfjs/turf/pull/732)@turf/inside
(https://github.com/Turfjs/turf/pull/706)@turf/meta
. (https://github.com/Turfjs/turf/pull/712 & https://github.com/Turfjs/turf/issues/692)getGeom
& getGeomType
in @turf/invariant
(https://github.com/Turfjs/turf/pull/720)round()
, radians2degrees()
and degrees2radians()
to turf-helpers
(https://github.com/Turfjs/turf/pull/715)@turf/rewind
(https://github.com/Turfjs/turf/pull/741)@turf/circle
translate properties (https://github.com/Turfjs/turf/pull/717)@turf/buffer
to polygons (https://github.com/Turfjs/turf/issues/660 & https://github.com/Turfjs/turf/pull/718)@turf/difference
(https://github.com/Turfjs/turf/pull/725)@turf/line-intersect
(https://github.com/Turfjs/turf/pull/731)@turf/line-chunk
(https://github.com/Turfjs/turf/pull/726)@turf/line-segment
(https://github.com/Turfjs/turf/pull/727 & https://github.com/Turfjs/turf/pull/711)@turf/line-overlap
(https://github.com/Turfjs/turf/pull/711)@turf/rewind
(https://github.com/Turfjs/turf/pull/741)@turf/buffer
(https://github.com/Turfjs/turf/pull/739)radius
to @turf/buffer
(https://github.com/Turfjs/turf/pull/736)FAQs
turf centroid module
The npm package @turf/centroid receives a total of 750,698 weekly downloads. As such, @turf/centroid popularity was classified as popular.
We found that @turf/centroid demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.