Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/turf-center-of-mass/test/out/linestring.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [4.860076904296875, 45.75919915723537]
"coordinates": [4.8600768820512945, 45.75919915730767]
}
},
{
Expand Down
38 changes: 34 additions & 4 deletions packages/turf-centroid/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Feature, GeoJsonProperties, Point } from "geojson";
import { point, AllGeoJSON } from "@turf/helpers";
import {
point,
degreesToRadians,
radiansToDegrees,
earthRadius,
AllGeoJSON,
} from "@turf/helpers";
import { coordEach } from "@turf/meta";

/**
Expand All @@ -26,17 +32,41 @@ function centroid<P extends GeoJsonProperties = GeoJsonProperties>(
): Feature<Point, P> {
let xSum = 0;
let ySum = 0;
let zSum = 0;
let len = 0;
coordEach(
geojson,
function (coord) {
xSum += coord[0];
ySum += coord[1];
xSum +=
Math.cos(degreesToRadians(coord[0])) *
Math.cos(degreesToRadians(coord[1])) *
earthRadius;
ySum +=
Math.sin(degreesToRadians(coord[0])) *
Math.cos(degreesToRadians(coord[1])) *
earthRadius;
zSum += Math.sin(degreesToRadians(coord[1])) * earthRadius;
len++;
},
true
);
return point([xSum / len, ySum / len], options.properties);
return point(
[
radiansToDegrees(Math.atan2(ySum / len, xSum / len)),
radiansToDegrees(
Math.asin(
zSum /
len /
Math.sqrt(
Math.pow(xSum / len, 2) +
Math.pow(ySum / len, 2) +
Math.pow(zSum / len, 2)
)
)
),
],
options.properties
);
}

export { centroid };
Expand Down
1 change: 1 addition & 0 deletions packages/turf-centroid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"test:types": "tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts"
},
"devDependencies": {
"@turf/circle": "workspace:^",
"@types/benchmark": "^2.1.5",
"@types/tape": "^4.13.4",
"benchmark": "^2.1.4",
Expand Down
15 changes: 15 additions & 0 deletions packages/turf-centroid/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { writeJsonFileSync } from "write-json-file";
import { featureEach } from "@turf/meta";
import { featureCollection, lineString } from "@turf/helpers";
import { centroid } from "./index.js";
import { circle } from "@turf/circle";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -43,6 +44,20 @@ test("centroid", (t) => {
t.end();
});

test("centroid -- circle consistency", (t) => {
const circ = circle([4.832, 45.7578], 4000);
const center = centroid(circ);
t.true(
Math.abs(center.geometry.coordinates[0] - 4.832) < 0.0000001,
"lon equality"
);
t.true(
Math.abs(center.geometry.coordinates[1] - 45.7578) < 0.0000001,
"lat equality"
);
t.end();
});

test("centroid -- properties", (t) => {
const line = lineString([
[0, 0],
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-centroid/test/out/feature-collection.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"geometry": {
"type": "Point",
"coordinates": [4.8336222767829895, 45.76051644154402]
"coordinates": [4.833622276817487, 45.76051644217321]
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-centroid/test/out/imbalanced-polygon.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"geometry": {
"type": "Point",
"coordinates": [4.851791984156558, 45.78143055383553]
"coordinates": [4.851791902704642, 45.78143064842905]
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-centroid/test/out/linestring.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"geometry": {
"type": "Point",
"coordinates": [4.860076904296875, 45.75919915723537]
"coordinates": [4.8600768820512945, 45.75919915730767]
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-centroid/test/out/point.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"geometry": {
"type": "Point",
"coordinates": [4.831961989402771, 45.75764678012361]
"coordinates": [4.83196198940277, 45.75764678012361]
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-centroid/test/out/polygon.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"geometry": {
"type": "Point",
"coordinates": [4.841194152832031, 45.75807143030368]
"coordinates": [4.841194013099296, 45.758082963072354]
}
},
{
Expand Down
6 changes: 3 additions & 3 deletions packages/turf-distance-weight/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test("turf-distance-weight", (t) => {
p: 2,
alpha: 1,
});
t.equal(result[0][1], 0.5987182558007202, "change p");
t.equal(result[0][1], 0.5987182558007197, "change p");

// test alpha
result = distanceWeight(columbusJson, {
Expand All @@ -70,7 +70,7 @@ test("turf-distance-weight", (t) => {
alpha: 1,
standardization: true,
});
t.equal(result[0][1], 0.5311565480348293, "standardization 1");
t.equal(result[0][1], 0.531156548034828, "standardization 1");

result = distanceWeight(columbusJson, {
threshold: 1,
Expand All @@ -83,7 +83,7 @@ test("turf-distance-weight", (t) => {

// test default
result = distanceWeight(columbusJson);
t.equal(result[0][1], 1.6702346893742355, "default arguments");
t.equal(result[0][1], 1.670234689374237, "default arguments");

t.end();
});
Loading
Loading