From 1ddac9361a17601c903b74f15f0b540b5b5de05d Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Sun, 26 Oct 2014 21:00:56 +0100 Subject: [PATCH] Fix array of array of LatLngs toGeoJSON edge case --- spec/suites/layer/GeoJSONSpec.js | 51 +++++++++++++++++++++++++++++--- src/layer/GeoJSON.js | 4 --- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/spec/suites/layer/GeoJSONSpec.js b/spec/suites/layer/GeoJSONSpec.js index ec02fbf5..29be03fd 100644 --- a/spec/suites/layer/GeoJSONSpec.js +++ b/spec/suites/layer/GeoJSONSpec.js @@ -120,7 +120,7 @@ describe("L.Polyline (multi) #toGeoJSON", function () { }); describe("L.Polygon#toGeoJSON", function () { - it("returns a 2D Polygon object (no holes)", function () { + it("returns a 2D Polygon object (no holes) from a flat LatLngs array", function () { var polygon = new L.Polygon([[1, 2], [3, 4], [5, 6]]); expect(polygon.toGeoJSON().geometry).to.eql({ type: 'Polygon', @@ -128,7 +128,7 @@ describe("L.Polygon#toGeoJSON", function () { }); }); - it("returns a 3D Polygon object (no holes)", function () { + it("returns a 3D Polygon object (no holes) from a flat LatLngs array", function () { var polygon = new L.Polygon([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); expect(polygon.toGeoJSON().geometry).to.eql({ type: 'Polygon', @@ -136,6 +136,26 @@ describe("L.Polygon#toGeoJSON", function () { }); }); + it("returns a 2D Polygon object from a simple GeoJSON like input", function () { + var multiPolygon = new L.Polygon([[[1, 2], [3, 4], [5, 6]]]); + expect(multiPolygon.toGeoJSON().geometry).to.eql({ + type: 'Polygon', + coordinates: [ + [[2, 1], [4, 3], [6, 5], [2, 1]] + ] + }); + }); + + it("returns a 3D MultiPolygon object from a simple GeoJSON like input", function () { + var multiPolygon = new L.Polygon([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]); + expect(multiPolygon.toGeoJSON().geometry).to.eql({ + type: 'Polygon', + coordinates: [ + [[2, 1, 3], [5, 4, 6], [8, 7, 9], [2, 1, 3]] + ] + }); + }); + it("returns a 2D Polygon object (with holes)", function () { var polygon = new L.Polygon([[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]); expect(polygon.toGeoJSON().geometry).to.eql({ @@ -157,11 +177,12 @@ describe("L.Polygon#toGeoJSON", function () { ] }); }); + }); describe("L.Polygon (multi) #toGeoJSON", function () { it("returns a 2D MultiPolygon object", function () { - var multiPolygon = new L.Polygon([[[1, 2], [3, 4], [5, 6]]]); + var multiPolygon = new L.Polygon([[[[1, 2], [3, 4], [5, 6]]]]); expect(multiPolygon.toGeoJSON().geometry).to.eql({ type: 'MultiPolygon', coordinates: [ @@ -171,7 +192,7 @@ describe("L.Polygon (multi) #toGeoJSON", function () { }); it("returns a 3D MultiPolygon object", function () { - var multiPolygon = new L.Polygon([[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]); + var multiPolygon = new L.Polygon([[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]); expect(multiPolygon.toGeoJSON().geometry).to.eql({ type: 'MultiPolygon', coordinates: [ @@ -179,6 +200,28 @@ describe("L.Polygon (multi) #toGeoJSON", function () { ] }); }); + + it("returns a 2D MultiPolygon object with two polygons", function () { + var multiPolygon = new L.Polygon([[[[1, 2], [3, 4], [5, 6]]], [[[7, 8], [9, 10], [11, 12]]]]); + expect(multiPolygon.toGeoJSON().geometry).to.eql({ + type: 'MultiPolygon', + coordinates: [ + [[[2, 1], [4, 3], [6, 5], [2, 1]]], + [[[8, 7], [10, 9], [12, 11], [8, 7]]] + ] + }); + }); + + it("returns a 2D MultiPolygon object with polygon having a hole", function () { + var multiPolygon = new L.Polygon([[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]]); + expect(multiPolygon.toGeoJSON().geometry).to.eql({ + type: 'MultiPolygon', + coordinates: [ + [[[2, 1], [4, 3], [6, 5], [2, 1]], [[8, 7], [10, 9], [12, 11], [8, 7]]] + ] + }); + }); + }); describe("L.LayerGroup#toGeoJSON", function () { diff --git a/src/layer/GeoJSON.js b/src/layer/GeoJSON.js index 1f08729f..d8700a8a 100644 --- a/src/layer/GeoJSON.js +++ b/src/layer/GeoJSON.js @@ -206,10 +206,6 @@ L.Polygon.prototype.toGeoJSON = function () { var coords = L.GeoJSON.latLngsToCoords(this._latlngs, multi ? 2 : holes ? 1 : 0, true); - if (holes && this._latlngs.length === 1) { - multi = true; - coords = [coords]; - } if (!holes) { coords = [coords]; }