What is @turf/center-mean?
@turf/center-mean is a module from the Turf.js library that calculates the mean center of a set of geographical points. This is useful for finding the average location of a group of points, which can be applied in various geospatial analyses.
What are @turf/center-mean's main functionalities?
Calculate Mean Center
This feature calculates the mean center of a set of points. The code sample creates a collection of points and then calculates their mean center using the `turf.centerMean` function.
const turf = require('@turf/turf');
const points = turf.featureCollection([
turf.point([0, 0]),
turf.point([10, 10]),
turf.point([20, 20])
]);
const meanCenter = turf.centerMean(points);
console.log(meanCenter);
Other packages similar to @turf/center-mean
@turf/center
@turf/center calculates the centroid (geometric center) of a set of geographical points. While @turf/center-mean calculates the mean center, which is the average of the coordinates, @turf/center calculates the centroid, which is the center of mass of the shape formed by the points.
geolib
geolib is a library for geospatial calculations. It provides functions to calculate the center of a set of points, distance between points, and more. Compared to @turf/center-mean, geolib offers a broader range of geospatial functions but may not be as specialized in calculating mean centers.
leaflet
Leaflet is a popular library for interactive maps. It provides various utilities for geospatial data, including functions to calculate the center of a set of points. While Leaflet is primarily focused on map rendering and interaction, it also includes some geospatial analysis features similar to @turf/center-mean.
@turf/center-mean
centerMean
Takes a Feature or FeatureCollection and returns the mean center. Can be weighted.
Parameters
geojson
GeoJSON GeoJSON to be centeredoptions
Object Optional parameters (optional, default {}
)
options.properties
Object Translate GeoJSON Properties to Point (optional, default {}
)options.bbox
Object Translate GeoJSON BBox to Point (optional, default {}
)options.id
Object Translate GeoJSON Id to Point (optional, default {}
)options.weight
string? the property name used to weight the center
Examples
var features = turf.featureCollection([
turf.point([-97.522259, 35.4691], {value: 10}),
turf.point([-97.502754, 35.463455], {value: 3}),
turf.point([-97.508269, 35.463245], {value: 5})
]);
var options = {weight: "value"}
var mean = turf.centerMean(features, options);
var addToMap = [features, mean]
mean.properties['marker-size'] = 'large';
mean.properties['marker-color'] = '#000';
Returns Feature<Point> a Point feature at the mean center point of all 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.
Installation
Install this module individually:
$ npm install @turf/center-mean
Or install the Turf module that includes it as a function:
$ npm install @turf/turf