Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
@turf/center-of-mass
Advanced tools
@turf/center-of-mass is a module from the Turf.js library that calculates the center of mass (centroid) of a given GeoJSON feature. This is particularly useful for geographic data analysis and visualization, where determining the central point of a polygon or multi-polygon is required.
Calculate Center of Mass for a Polygon
This feature calculates the center of mass for a given polygon. The code sample demonstrates how to create a polygon and then use the @turf/center-of-mass module to find its centroid.
const turf = require('@turf/turf');
const polygon = turf.polygon([[
[-70.603637, -33.399918],
[-70.614624, -33.395332],
[-70.639343, -33.392466],
[-70.659942, -33.394759],
[-70.683975, -33.404504],
[-70.697021, -33.419406],
[-70.701141, -33.434306],
[-70.700454, -33.446339],
[-70.694274, -33.458369],
[-70.682601, -33.465816],
[-70.668869, -33.472117],
[-70.646209, -33.473835],
[-70.624923, -33.472117],
[-70.609817, -33.468107],
[-70.595397, -33.458369],
[-70.587158, -33.442901],
[-70.587158, -33.426283],
[-70.590591, -33.414248],
[-70.594711, -33.406224],
[-70.603637, -33.399918]
]]);
const centerOfMass = turf.centerOfMass(polygon);
console.log(centerOfMass);
Calculate Center of Mass for a MultiPolygon
This feature calculates the center of mass for a given multi-polygon. The code sample demonstrates how to create a multi-polygon and then use the @turf/center-of-mass module to find its centroid.
const turf = require('@turf/turf');
const multiPolygon = turf.multiPolygon([
[[
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681]
]],
[[
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681],
[-73.981149, 40.7681]
]]
]);
const centerOfMass = turf.centerOfMass(multiPolygon);
console.log(centerOfMass);
Geolib is a library to provide basic geospatial operations like distance calculation, bounding boxes, and more. It can calculate the centroid of a set of points, but it does not specifically focus on the center of mass for polygons or multi-polygons like @turf/center-of-mass.
JSTS is a JavaScript library of spatial predicates and functions for processing geometry. It includes functions for calculating centroids, but it is more complex and offers a broader range of spatial operations compared to the focused functionality of @turf/center-of-mass.
Takes any Feature or a FeatureCollection and returns its center of mass using this formula: Centroid of Polygon.
geojson
GeoJSON GeoJSON to be centered
options
Object Optional Parameters (optional, default {}
)
options.properties
Object Translate Properties to Feature (optional, default {}
)var polygon = turf.polygon([[[-81, 41], [-88, 36], [-84, 31], [-80, 33], [-77, 39], [-81, 41]]]);
var center = turf.centerOfMass(polygon);
//addToMap
var addToMap = [polygon, center]
Returns Feature<Point> the center of mass
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 single module individually:
$ npm install @turf/center-of-mass
Or install the all-encompassing @turf/turf module that includes all modules as functions:
$ npm install @turf/turf
FAQs
turf center-of-mass module
We found that @turf/center-of-mass 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
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.