Account for instances where d3.geoCentroid returns incorrect results

This commit is contained in:
Quincy Morgan
2020-10-29 14:28:21 -04:00
parent 6b8cbbd12b
commit ba4c1ef014
3 changed files with 8 additions and 2 deletions
+4 -1
View File
@@ -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();
}
+3 -1
View File
@@ -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));
}
+1
View File
@@ -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",