Skip to content

Instantly share code, notes, and snippets.

@morganherlocker
Created February 5, 2015 21:47
Show Gist options
  • Select an option

  • Save morganherlocker/4c0c64a7fe0652dffbf1 to your computer and use it in GitHub Desktop.

Select an option

Save morganherlocker/4c0c64a7fe0652dffbf1 to your computer and use it in GitHub Desktop.

turf-centroid

build status

turf centroid module

turf.centroid(features)

Takes a Feature or FeatureCollection of any type and calculates the centroid using the arithmetic mean of all vertices. This lessens the effect of small islands and artifacts when calculating the centroid of a set of polygons.

Parameters

parameter type description
features GeoJSON a Feature or FeatureCollection of any type

Example

var poly = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [[
      [105.818939,21.004714],
      [105.818939,21.061754],
      [105.890007,21.061754],
      [105.890007,21.004714],
      [105.818939,21.004714]
    ]]
  }
};

var centroidPt = turf.centroid(poly);

var result = {
  "type": "FeatureCollection",
  "features": [poly, centroidPt]
};

//=result

Installation

Requires nodejs.

$ npm install turf-centroid

Tests

$ npm test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment