Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix centroid calculation in measurement panel. #8341

Merged
merged 1 commit into from
Jun 21, 2021
Merged
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
10 changes: 6 additions & 4 deletions modules/ui/panels/measurement.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
geoLength as d3_geoLength,
geoCentroid as d3_geoCentroid
geoPath as d3_geoPath
} from 'd3-geo';
import geojsonRewind from '@mapbox/geojson-rewind';

import { t, localizer } from '../../core/localizer';
import { displayArea, displayLength, decimalCoordinatePair, dmsCoordinatePair } from '../../util/units';
Expand Down Expand Up @@ -79,8 +78,11 @@ export function uiPanelMeasurement(context) {
closed = (entity.type === 'relation') || (entity.isClosed() && !entity.isDegenerate());
var feature = entity.asGeoJSON(graph);
length += radiansToMeters(d3_geoLength(toLineString(feature)));
// d3_geoCentroid is wrong for counterclockwise-wound polygons, so wind them clockwise
centroid = d3_geoCentroid(geojsonRewind(Object.assign({}, feature), true));
centroid = d3_geoPath(context.projection).centroid(entity.asGeoJSON(graph));
centroid = centroid && context.projection.invert(centroid);
if (!centroid || !isFinite(centroid[0]) || !isFinite(centroid[1])) {
centroid = entity.extent(graph).center();
}
if (closed) {
area += steradiansToSqmeters(entity.area(graph));
}
Expand Down