improve autozooming on non-gpx background data layers

old implementation only worked for points and linestrings, but geojson and kml can contain any geometry.

aso, d3 is a bit special[1] when working with geojson polygons: it does require clockwise winding, but typical geojson is either counterclockwise winding (RFC7946) or "winding doesn't matter" (old geojson.org spec). When inputing such geojson (or kml) input, iD would zoom out all the way, which is not the intended behavior.

[1] https://github.com/d3/d3-geo#d3-geo
This commit is contained in:
Martin Raifer
2017-02-06 18:04:20 +01:00
parent ff4bf2597d
commit d7ad3bc39e

View File

@@ -203,11 +203,25 @@ export function svgGpx(projection, context, dispatch) {
viewport = map.trimmedExtent().polygon(),
coords = _.reduce(geojson.features, function(coords, feature) {
var c = feature.geometry.coordinates;
return _.union(coords, feature.geometry.type === 'Point' ? [c] : c);
switch (feature.geometry.type) {
case 'Point':
c = [c];
case 'MultiPoint':
case 'LineString':
break;
case 'MultiPolygon':
c = _.flatten(c);
case 'Polygon':
case 'MultiLineString':
c = _.flatten(c);
break;
}
return _.union(coords, c);
}, []);
if (!geoPolygonIntersectsPolygon(viewport, coords, true)) {
var extent = geoExtent(d3.geoBounds(geojson));
var extent = geoExtent(d3.geoBounds({ type: 'LineString', coordinates: coords }));
map.centerZoom(extent.center(), map.trimmedExtentZoom(extent));
}