What is @turf/center?
@turf/center is a module from the Turf.js library that calculates the center of a given GeoJSON feature or set of features. It is useful for geographic data analysis and visualization, allowing developers to easily find the central point of various geometries such as polygons, lines, and multi-point collections.
What are @turf/center's main functionalities?
Calculate Center of a Polygon
This feature calculates the center point of a polygon. The code sample demonstrates how to create a polygon and then find its center using the @turf/center package.
const turf = require('@turf/center');
const polygon = turf.polygon([[
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41],
[-81, 41]
]]);
const center = turf(polygon);
console.log(center);
Calculate Center of a MultiPoint Collection
This feature calculates the center point of a collection of points (MultiPoint). The code sample shows how to create a MultiPoint collection and find its center using the @turf/center package.
const turf = require('@turf/center');
const multiPoint = turf.multiPoint([
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41]
]);
const center = turf(multiPoint);
console.log(center);
Calculate Center of a LineString
This feature calculates the center point of a LineString. The code sample demonstrates how to create a LineString and find its center using the @turf/center package.
const turf = require('@turf/center');
const lineString = turf.lineString([
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41]
]);
const center = turf(lineString);
console.log(center);
Other packages similar to @turf/center
geolib
Geolib is a library for geospatial calculations. It provides functions to calculate distances, areas, and center points of various geometries. Compared to @turf/center, geolib offers a broader range of geospatial utilities but may not be as specialized in handling GeoJSON data.
leaflet
Leaflet is a popular open-source JavaScript library for interactive maps. It includes functionalities to calculate the center of a map view or a set of coordinates. While Leaflet is primarily focused on map rendering and interaction, it can also be used for basic geospatial calculations similar to @turf/center.
geodesy
Geodesy is a library for geodesic calculations, including finding the center of a set of points. It is more focused on precise geodesic computations and may offer more accuracy for certain types of geospatial data compared to @turf/center.
@turf/center
center
Takes a Feature or FeatureCollection and returns the absolute center point of all features.
Parameters
geojson
GeoJSON GeoJSON to be centeredproperties
[Object] an Object that is used as the Feature's properties
Examples
var features = turf.featureCollection([
turf.point( [-97.522259, 35.4691]),
turf.point( [-97.502754, 35.463455]),
turf.point( [-97.508269, 35.463245])
]);
var center = turf.center(features);
var addToMap = [features, center]
center.properties['marker-size'] = 'large';
center.properties['marker-color'] = '#000';
Returns Feature<Point> a Point feature at the absolute 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
Or install the Turf module that includes it as a function:
$ npm install @turf/turf
4.7.0
🚀 New Modules
[@turf/projection
][projection]
- toMercator: Converts a WGS84 GeoJSON object into Mercator (EPSG:900913) projection
- toWgs84: Converts a Mercator (EPSG:900913) GeoJSON object into WGS84 projection
(PR https://github.com/Turfjs/turf/pull/927 - Author @stebogit)
[@turf/point-to-line-distance
][point-to-line-distance]
Returns the minimum distance between a {@link Point} and a {@link LineString}, being the distance from a line the minimum distance between the point and any segment of the LineString
.
(PR https://github.com/Turfjs/turf/pull/925 - Author @stebogit)
[@turf/boolean-within
][boolean-within]
Boolean-within returns true if the first geometry is completely within the second geometry. The interiors of both geometries must intersect and, the interior and boundary of the primary (geometry a) must not intersect the exterior of the secondary (geometry b). Boolean-within returns the exact opposite result of the [@turf/boolean-contains
][boolean-contains].
(PR https://github.com/Turfjs/turf/pull/924 - Author @rowanwins)
🏅 New Features/Enhancements
- Updates [
@turf/unkink-polygon
][unkink-polygon] testing & added flattenEach
instead of using flatten
.
(PR https://github.com/Turfjs/turf/pull/889) - [
@turf/concave
][concave] refactoring, replacing [@turf/union
][union] with geojson-dissolve
to increase speed and added support to null
geometries
(PR https://github.com/Turfjs/turf/pull/907 - Contributor @stebogit @DenisCarriere) - Adds doc note on [
@turf/polygonize
][polygonize] about "Edges must be correctly noded"
(PR https://github.com/Turfjs/turf/pull/898 - Contributor @stebogit @NickCis) - Adds support to foreign Members to [
@turf/clone
][clone]
(PR https://github.com/Turfjs/turf/pull/904 - Contributor @DenisCarriere) - Extends support of any
GeoJSON
to [@turf/simplify
][simplify]
(PR https://github.com/Turfjs/turf/pull/903 - Contributor @DenisCarriere @stebogit) - Adds new
isNumber
function and improves type checking for few [@turf/helpers
][helpers] methods
(PR https://github.com/Turfjs/turf/pull/920 - Contributor @DenisCarriere @stebogit) - Adds throw errors to invalid
MultiPolygons
for [@turf/simplify
][simplify]
(PR https://github.com/Turfjs/turf/pull/922 - Contributor @DenisCarriere)
🐛 Bug Fixes
- Fixes
@turf/bbox-clip
point intersection handling, adding sanity-checks the lineclip output
(PR https://github.com/Turfjs/turf/pull/886) - Fixes [
@turf/line-split
][line-split] endpoint case handling applying [@turf/truncate
][truncate] on splitter
(PR https://github.com/Turfjs/turf/pull/892 and https://github.com/Turfjs/turf/pull/855 - Contributor @stebogit) - Fixes [
@turf/intersect
][intersect] throwing "uncaught exception", adding [@turf/truncate
][truncate] to inputs and returning Feature<null>
if no geometry
(PR https://github.com/Turfjs/turf/pull/890 - Contributor @stebogit @vicvolk) - Fixes [
@turf/hex-grid
][hex-grid] not properly cloning the last ring vertex
(PR https://github.com/Turfjs/turf/pull/897 - Contributor @stebogit @DenisCarriere) - Fixes [
@turf/boolean-disjoint
][boolean-disjoint] incorrect behaviour where a poly was completely contained within another poly
(PR https://github.com/Turfjs/turf/pull/908 - Contributor @rowanwins) - Fixes [
@turf/simplify
][simplify] process pending on particular geometries, applying [@turf/clean-coords
][clean-coords] to input.
(PR https://github.com/Turfjs/turf/pull/903 - Contributor @stebogit @DenisCarriere) - Fixes
boolean
properties not being translated with [@turf/clone
][clone]
(PR https://github.com/Turfjs/turf/pull/909 - Contributor @DenisCarriere) - Fixes [
@turf/boolean-contains
][boolean-contains] incorrect output when checking two polygons
(PR https://github.com/Turfjs/turf/pull/923 - Contributor @rowanwins @DenisCarriere)