From ba4c1ef0148c15f8d973daf144349848fda6cb8e Mon Sep 17 00:00:00 2001 From: Quincy Morgan <2046746+quincylvania@users.noreply.github.com> Date: Thu, 29 Oct 2020 14:28:21 -0400 Subject: [PATCH] Account for instances where d3.geoCentroid returns incorrect results --- modules/actions/extract.js | 5 ++++- modules/ui/panels/measurement.js | 4 +++- package.json | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/actions/extract.js b/modules/actions/extract.js index 0db93c174..d283a42fb 100644 --- a/modules/actions/extract.js +++ b/modules/actions/extract.js @@ -1,5 +1,7 @@ import { geoCentroid as d3_geoCentroid } from 'd3-geo'; +import geojsonRewind from '@mapbox/geojson-rewind'; + import { osmNode } from '../osm/node'; export function actionExtract(entityID) { @@ -45,7 +47,8 @@ export function actionExtract(entityID) { var keysToRetain = ['area']; var buildingKeysToRetain = ['architect', 'building', 'height', 'layer']; - var extractedLoc = d3_geoCentroid(entity.asGeoJSON(graph)); + // d3_geoCentroid is wrong for counterclockwise-wound polygons, so wind them clockwise + var extractedLoc = d3_geoCentroid(geojsonRewind(Object.assign({}, entity.asGeoJSON(graph)), true)); if (!extractedLoc || !isFinite(extractedLoc[0]) || !isFinite(extractedLoc[1])) { extractedLoc = entity.extent(graph).center(); } diff --git a/modules/ui/panels/measurement.js b/modules/ui/panels/measurement.js index 20be05d66..cf5965290 100644 --- a/modules/ui/panels/measurement.js +++ b/modules/ui/panels/measurement.js @@ -2,6 +2,7 @@ import { geoLength as d3_geoLength, geoCentroid as d3_geoCentroid } from 'd3-geo'; +import geojsonRewind from '@mapbox/geojson-rewind'; import { t, localizer } from '../../core/localizer'; import { displayArea, displayLength, decimalCoordinatePair, dmsCoordinatePair } from '../../util/units'; @@ -78,7 +79,8 @@ export function uiPanelMeasurement(context) { closed = (entity.type === 'relation') || (entity.isClosed() && !entity.isDegenerate()); var feature = entity.asGeoJSON(graph); length += radiansToMeters(d3_geoLength(toLineString(feature))); - centroid = d3_geoCentroid(feature); + // d3_geoCentroid is wrong for counterclockwise-wound polygons, so wind them clockwise + centroid = d3_geoCentroid(geojsonRewind(Object.assign({}, feature), true)); if (closed) { area += steradiansToSqmeters(entity.area(graph)); } diff --git a/package.json b/package.json index 901a6ea01..ad1d57fc5 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "dependencies": { "@ideditor/country-coder": "^3.2.0", "@ideditor/location-conflation": "~0.5.0", + "@mapbox/geojson-rewind": "^0.5.0", "@mapbox/sexagesimal": "1.2.0", "@mapbox/togeojson": "0.16.0", "@mapbox/vector-tile": "^1.3.1",