Fix array of array of LatLngs toGeoJSON edge case

This commit is contained in:
Yohan Boniface 2014-10-26 21:00:56 +01:00
parent 72707e3e30
commit 1ddac9361a
2 changed files with 47 additions and 8 deletions

View File

@ -120,7 +120,7 @@ describe("L.Polyline (multi) #toGeoJSON", function () {
}); });
describe("L.Polygon#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]]); var polygon = new L.Polygon([[1, 2], [3, 4], [5, 6]]);
expect(polygon.toGeoJSON().geometry).to.eql({ expect(polygon.toGeoJSON().geometry).to.eql({
type: 'Polygon', 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]]); var polygon = new L.Polygon([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
expect(polygon.toGeoJSON().geometry).to.eql({ expect(polygon.toGeoJSON().geometry).to.eql({
type: 'Polygon', 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 () { 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]]]); var polygon = new L.Polygon([[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]);
expect(polygon.toGeoJSON().geometry).to.eql({ expect(polygon.toGeoJSON().geometry).to.eql({
@ -157,11 +177,12 @@ describe("L.Polygon#toGeoJSON", function () {
] ]
}); });
}); });
}); });
describe("L.Polygon (multi) #toGeoJSON", function () { describe("L.Polygon (multi) #toGeoJSON", function () {
it("returns a 2D MultiPolygon object", 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({ expect(multiPolygon.toGeoJSON().geometry).to.eql({
type: 'MultiPolygon', type: 'MultiPolygon',
coordinates: [ coordinates: [
@ -171,7 +192,7 @@ describe("L.Polygon (multi) #toGeoJSON", function () {
}); });
it("returns a 3D MultiPolygon object", 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({ expect(multiPolygon.toGeoJSON().geometry).to.eql({
type: 'MultiPolygon', type: 'MultiPolygon',
coordinates: [ 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 () { describe("L.LayerGroup#toGeoJSON", function () {

View File

@ -206,10 +206,6 @@ L.Polygon.prototype.toGeoJSON = function () {
var coords = L.GeoJSON.latLngsToCoords(this._latlngs, multi ? 2 : holes ? 1 : 0, true); 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) { if (!holes) {
coords = [coords]; coords = [coords];
} }