Socket
Socket
Sign inDemoInstall

@turf/centroid

Package Overview
Dependencies
2
Maintainers
6
Versions
59
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @turf/centroid

turf centroid module


Version published
Maintainers
6
Install size
233 kB
Created

Package description

What is @turf/centroid?

@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.

What are @turf/centroid's main functionalities?

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);

Other packages similar to @turf/centroid

Changelog

Source

6.5.0

🏅 New Features/Enhancements

  • @turf/points-with-polygon Add MultiPoint support (PR https://github.com/Turfjs/turf/pull/2137 - Author @twelch)

🐛 Bug Fixes

  • @turf/dissolve Complete rewrite of the dissolve module to resolve many bugs (PR https://github.com/Turfjs/turf/pull/2110 - Author @rowanwins)

  • @turf/mask Complete rewrite of the mask module to resolve many bugs (PR https://github.com/Turfjs/turf/pull/2130 - Author @rowanwins)

  • @turf/boolean-valid Add missing dependency to (PR https://github.com/Turfjs/turf/pull/2094 - Author @rycgar)

📖 Documentation

  • @turf/boolean-overlap Improve clarity (PR https://github.com/Turfjs/turf/pull/2133 - Author @patrickbrett)

🔔 Misc

  • Enable TS Strict Mode (PR https://github.com/Turfjs/turf/pull/2126 - Author @mfedderly)

Readme

Source

@turf/centroid

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

  • geojson GeoJSON GeoJSON to be centered
  • options Object Optional Parameters (optional, default {})
    • options.properties Object an Object that is used as the Feature's properties (optional, default {})

Examples

var polygon = turf.polygon([[[-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.

Installation

Install this module individually:

$ npm install @turf/centroid

Or install the Turf module that includes it as a function:

$ npm install @turf/turf

Keywords

FAQs

Last updated on 10 Jul 2021

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc