From 8283346aa128bae89fc6ae8c49ec13a9567860d3 Mon Sep 17 00:00:00 2001 From: javi Date: Thu, 14 Mar 2013 13:22:40 +0100 Subject: [PATCH] fixed polygon and multipolygon from geojson --- src/layer/GeoJSON.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/layer/GeoJSON.js b/src/layer/GeoJSON.js index 0a154a7e..71f1d758 100644 --- a/src/layer/GeoJSON.js +++ b/src/layer/GeoJSON.js @@ -97,6 +97,7 @@ L.extend(L.GeoJSON, { case 'Polygon': latlngs = this.coordsToLatLngs(coords, 1); + latlngs = this.removeLastPoint(latlngs, 0); return new L.Polygon(latlngs); case 'MultiLineString': @@ -105,6 +106,9 @@ L.extend(L.GeoJSON, { case 'MultiPolygon': latlngs = this.coordsToLatLngs(coords, 2); + // geojson closes the polygons added the frist + // coordinate at the end + latlngs = this.removeLastPoint(latlngs, 1); return new L.MultiPolygon(latlngs); case 'GeometryCollection': @@ -123,6 +127,21 @@ L.extend(L.GeoJSON, { } }, + // removes the last point from each array. It allows to remove the + // duplicated point GeoJSON uses to close Polygons + removeLastPoint: function (coords, levelsDeep) { + var latlng, + latlngs = [], + i, len; + for (i = 0, len = coords.length; i < len; i++) { + latlng = levelsDeep ? + this.removeLastPoint(coords[i], levelsDeep - 1) : + coords[i].slice(0, coords[i].length - 1); + latlngs.push(latlng); + } + return latlngs; + }, + coordsToLatLng: function (coords, reverse) { // (Array, Boolean) -> LatLng var lat = parseFloat(coords[reverse ? 0 : 1]), lng = parseFloat(coords[reverse ? 1 : 0]);