Installed leaflet in extlib
[mediagoblin.git] / extlib / leaflet / src / layer / GeoJSON.js
diff --git a/extlib/leaflet/src/layer/GeoJSON.js b/extlib/leaflet/src/layer/GeoJSON.js
new file mode 100644 (file)
index 0000000..6cbd419
--- /dev/null
@@ -0,0 +1,106 @@
+\r
+L.GeoJSON = L.LayerGroup.extend({\r
+       includes: L.Mixin.Events,\r
+       \r
+       initialize: function(geojson, options) {\r
+               L.Util.setOptions(this, options);\r
+               this._geojson = geojson;\r
+               this._layers = {};\r
+               \r
+               if (geojson) {\r
+                       this.addGeoJSON(geojson);\r
+               }\r
+       },\r
+       \r
+       addGeoJSON: function(geojson) {\r
+               if (geojson.features) {\r
+                       for (var i = 0, len = geojson.features.length; i < len; i++) {\r
+                               this.addGeoJSON(geojson.features[i]);\r
+                       }\r
+                       return;\r
+               }\r
+                               \r
+               var isFeature = (geojson.type == 'Feature'),\r
+                       geometry = (isFeature ? geojson.geometry : geojson),\r
+                       layer = L.GeoJSON.geometryToLayer(geometry, this.options.pointToLayer);\r
+               \r
+               this.fire('featureparse', {\r
+                       layer: layer, \r
+                       properties: geojson.properties,\r
+                       geometryType: geometry.type,\r
+                       bbox: geojson.bbox,\r
+                       id: geojson.id\r
+               });\r
+               \r
+               this.addLayer(layer);\r
+       }\r
+});\r
+\r
+L.Util.extend(L.GeoJSON, {\r
+       geometryToLayer: function(geometry, pointToLayer) {\r
+               var coords = geometry.coordinates, \r
+                       latlng, latlngs, \r
+                       i, len, \r
+                       layer, \r
+                       layers = [];\r
+\r
+               switch (geometry.type) {\r
+                       case 'Point':\r
+                               latlng = this.coordsToLatLng(coords);\r
+                               return pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);\r
+                               \r
+                       case 'MultiPoint':\r
+                               for (i = 0, len = coords.length; i < len; i++) {\r
+                                       latlng = this.coordsToLatLng(coords[i]);\r
+                                       layer = pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);\r
+                                       layers.push(layer);\r
+                               }\r
+                               return new L.FeatureGroup(layers);\r
+                               \r
+                       case 'LineString':\r
+                               latlngs = this.coordsToLatLngs(coords);\r
+                               return new L.Polyline(latlngs);\r
+                               \r
+                       case 'Polygon':\r
+                               latlngs = this.coordsToLatLngs(coords, 1);\r
+                               return new L.Polygon(latlngs);\r
+                               \r
+                       case 'MultiLineString':\r
+                               latlngs = this.coordsToLatLngs(coords, 1);\r
+                               return new L.MultiPolyline(latlngs);\r
+                               \r
+                       case "MultiPolygon":\r
+                               latlngs = this.coordsToLatLngs(coords, 2);\r
+                               return new L.MultiPolygon(latlngs);\r
+                               \r
+                       case "GeometryCollection":\r
+                               for (i = 0, len = geometry.geometries.length; i < len; i++) {\r
+                                       layer = this.geometryToLayer(geometry.geometries[i]);\r
+                                       layers.push(layer);\r
+                               }\r
+                               return new L.FeatureGroup(layers);\r
+                               \r
+                       default:\r
+                               throw new Error('Invalid GeoJSON object.');\r
+               }\r
+       },\r
+\r
+       coordsToLatLng: function(/*Array*/ coords, /*Boolean*/ reverse)/*: LatLng*/ {\r
+               var lat = parseFloat(coords[reverse ? 0 : 1]),\r
+                       lng = parseFloat(coords[reverse ? 1 : 0]);\r
+               return new L.LatLng(lat, lng);\r
+       },\r
+\r
+       coordsToLatLngs: function(/*Array*/ coords, /*Number*/ levelsDeep, /*Boolean*/ reverse)/*: Array*/ {\r
+               var latlng, latlngs = [],\r
+                       i, len = coords.length;\r
+               \r
+               for (i = 0; i < len; i++) {\r
+                       latlng = levelsDeep ? \r
+                                       this.coordsToLatLngs(coords[i], levelsDeep - 1, reverse) : \r
+                                       this.coordsToLatLng(coords[i], reverse);\r
+                       latlngs.push(latlng);\r
+               }\r
+               return latlngs;\r
+       }\r
+});
\ No newline at end of file