Skip to content

Commit

Permalink
Merge pull request Turfjs#1413 from Turfjs/center-of-mass-fix
Browse files Browse the repository at this point in the history
Center-of-mass fix. Resolves Turfjs#1385
  • Loading branch information
rowanwins authored Jun 16, 2018
2 parents b069f15 + 2d13846 commit 085f562
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/turf-center-of-mass/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import convex from '@turf/convex';
import centroid from '@turf/centroid';
import { point, Properties, AllGeoJSON, Feature, Point } from '@turf/helpers';
import { getType } from '@turf/invariant';
import { getType, getCoord } from '@turf/invariant';
import { coordEach } from '@turf/meta';

/**
Expand All @@ -25,7 +25,7 @@ function centerOfMass<P = Properties>(geojson: any, options: {
} = {}): Feature<Point, P> {
switch (getType(geojson)) {
case 'Point':
return geojson;
return point(getCoord(geojson), options.properties);
case 'Polygon':
var coords = [];
coordEach(geojson, function (coord) {
Expand Down
14 changes: 14 additions & 0 deletions packages/turf-center-of-mass/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ test('center of mass -- no area', t => {
t.end();
});

test('center of mass -- point', t => {
const p = point([0, 0]);
const pt = centerOfMass(p);
t.deepEqual(pt, point([0, 0]), 'point returns pt');
t.end();
});

test('center of mass -- point geom', t => {
const geomPoint = {type: 'Point', coordinates: [0, 0]};
const pt = centerOfMass(geomPoint);
t.deepEqual(pt, point([0, 0]), 'point geom returns pt');
t.end();
});

test('center of mass -- properties', t => {
const line = lineString([[0, 0], [1, 1]]);
const pt = centerOfMass(line, {properties: {foo: 'bar'}});
Expand Down

0 comments on commit 085f562

Please sign in to comment.