From d7ad3bc39e712d1236bc99355b4cde63beec1af4 Mon Sep 17 00:00:00 2001 From: Martin Raifer Date: Mon, 6 Feb 2017 18:04:20 +0100 Subject: [PATCH] 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 --- modules/svg/gpx.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/svg/gpx.js b/modules/svg/gpx.js index e0a9b171f..759870302 100644 --- a/modules/svg/gpx.js +++ b/modules/svg/gpx.js @@ -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)); }