[Pkg-javascript-commits] [leaflet] 79/301: add GeoJSON rountripping of GeometryCollection and MultiPoint, close #1956

Jonas Smedegaard js at moszumanska.debian.org
Mon Jan 27 22:22:43 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository leaflet.

commit 5e30c51f8556903d8077e8dc0154dae0f5867513
Author: Vladimir Agafonkin <agafonkin at gmail.com>
Date:   Wed Aug 28 12:43:04 2013 +0300

    add GeoJSON rountripping of GeometryCollection and MultiPoint, close #1956
---
 spec/suites/layer/GeoJSONSpec.js | 45 +++++++++++++++++++++
 src/layer/GeoJSON.js             | 84 +++++++++++++++++++++++-----------------
 2 files changed, 93 insertions(+), 36 deletions(-)

diff --git a/spec/suites/layer/GeoJSONSpec.js b/spec/suites/layer/GeoJSONSpec.js
index 3ffc990..55a26b1 100644
--- a/spec/suites/layer/GeoJSONSpec.js
+++ b/spec/suites/layer/GeoJSONSpec.js
@@ -144,6 +144,51 @@ describe("L.LayerGroup#toGeoJSON", function () {
 		});
 	});
 
+	it('roundtrips GeometryCollection features', function () {
+		var json = {
+			"type": "FeatureCollection",
+			"features": [{
+				"type": "Feature",
+				"geometry": {
+					"type": "GeometryCollection",
+					"geometries": [{
+						"type": "LineString",
+						"coordinates": [[-122.4425587930444, 37.80666418607323], [-122.4428379594768, 37.80663578323093]]
+					}, {
+						"type": "LineString",
+						"coordinates": [
+							[-122.4425509770566, 37.80662588061205],
+							[-122.4428340530617, 37.8065999493009]
+						]
+					}]
+				},
+				"properties": {
+					"name": "SF Marina Harbor Master"
+				}
+			}]
+		};
+
+		expect(L.geoJson(json).toGeoJSON()).to.eql(json);
+	});
+
+	it('roundtrips MiltiPoint features', function () {
+		var json = {
+			"type": "FeatureCollection",
+			"features": [{
+				"type": "Feature",
+				"geometry": {
+					"type": "MultiPoint",
+					"coordinates": [[-122.4425587930444, 37.80666418607323], [-122.4428379594768, 37.80663578323093]]
+				},
+				"properties": {
+					"name": "Test MultiPoints"
+				}
+			}]
+		};
+
+		expect(L.geoJson(json).toGeoJSON()).to.eql(json);
+	});
+
 	it("omits layers which do not implement toGeoJSON", function () {
 		var tileLayer = new L.TileLayer(),
 		    layerGroup = new L.LayerGroup([tileLayer]);
diff --git a/src/layer/GeoJSON.js b/src/layer/GeoJSON.js
index 290665d..7ee3016 100644
--- a/src/layer/GeoJSON.js
+++ b/src/layer/GeoJSON.js
@@ -77,7 +77,7 @@ L.extend(L.GeoJSON, {
 		var geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
 		    coords = geometry.coordinates,
 		    layers = [],
-		    latlng, latlngs, i, len, layer;
+		    latlng, latlngs, i, len;
 
 		coordsToLatLng = coordsToLatLng || this.coordsToLatLng;
 
@@ -89,8 +89,7 @@ L.extend(L.GeoJSON, {
 		case 'MultiPoint':
 			for (i = 0, len = coords.length; i < len; i++) {
 				latlng = coordsToLatLng(coords[i]);
-				layer = pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
-				layers.push(layer);
+				layers.push(pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng));
 			}
 			return new L.FeatureGroup(layers);
 
@@ -116,13 +115,11 @@ L.extend(L.GeoJSON, {
 		case 'GeometryCollection':
 			for (i = 0, len = geometry.geometries.length; i < len; i++) {
 
-				layer = this.geometryToLayer({
+				layers.push(this.geometryToLayer({
 					geometry: geometry.geometries[i],
 					type: 'Feature',
 					properties: geojson.properties
-				}, pointToLayer, coordsToLatLng);
-
-				layers.push(layer);
+				}, pointToLayer, coordsToLatLng));
 			}
 			return new L.FeatureGroup(layers);
 
@@ -226,43 +223,58 @@ L.Polygon.include({
 });
 
 (function () {
-	function includeMulti(Klass, type) {
-		Klass.include({
-			toGeoJSON: function () {
-				var coords = [];
+	function multiToGeoJSON(type) {
+		return function () {
+			var coords = [];
+
+			this.eachLayer(function (layer) {
+				coords.push(layer.toGeoJSON().geometry.coordinates);
+			});
+
+			return L.GeoJSON.getFeature(this, {
+				type: type,
+				coordinates: coords
+			});
+		};
+	}
 
-				this.eachLayer(function (layer) {
-					coords.push(layer.toGeoJSON().geometry.coordinates);
-				});
+	L.MultiPolyline.include({toGeoJSON: multiToGeoJSON('MultiLineString')});
+	L.MultiPolygon.include({toGeoJSON: multiToGeoJSON('MultiPolygon')});
 
-				return L.GeoJSON.getFeature(this, {
-					type: type,
-					coordinates: coords
-				});
+	L.LayerGroup.include({
+		toGeoJSON: function () {
+
+			var geometry = this.feature && this.feature.geometry,
+				jsons = [],
+				json;
+
+			if (geometry && geometry.type === 'MultiPoint') {
+				return multiToGeoJSON('MultiPoint').call(this);
 			}
-		});
-	}
 
-	includeMulti(L.MultiPolyline, 'MultiLineString');
-	includeMulti(L.MultiPolygon, 'MultiPolygon');
-}());
+			var isGeometryCollection = geometry && geometry.type === 'GeometryCollection';
 
-L.LayerGroup.include({
-	toGeoJSON: function () {
-		var features = [];
+			this.eachLayer(function (layer) {
+				if (layer.toGeoJSON) {
+					json = layer.toGeoJSON();
+					jsons.push(isGeometryCollection ? json.geometry : L.GeoJSON.asFeature(json));
+				}
+			});
 
-		this.eachLayer(function (layer) {
-			if (layer.toGeoJSON) {
-				features.push(L.GeoJSON.asFeature(layer.toGeoJSON()));
+			if (isGeometryCollection) {
+				return L.GeoJSON.getFeature(this, {
+					geometries: jsons,
+					type: 'GeometryCollection'
+				});
 			}
-		});
 
-		return {
-			type: 'FeatureCollection',
-			features: features
-		};
-	}
-});
+			return {
+				type: 'FeatureCollection',
+				features: jsons
+			};
+		}
+	});
+}());
 
 L.geoJson = function (geojson, options) {
 	return new L.GeoJSON(geojson, options);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet.git



More information about the Pkg-javascript-commits mailing list