Commit | Line | Data |
---|---|---|
c5ba5b04 JW |
1 | \r |
2 | L.GeoJSON = L.LayerGroup.extend({\r | |
3 | includes: L.Mixin.Events,\r | |
4 | \r | |
5 | initialize: function(geojson, options) {\r | |
6 | L.Util.setOptions(this, options);\r | |
7 | this._geojson = geojson;\r | |
8 | this._layers = {};\r | |
9 | \r | |
10 | if (geojson) {\r | |
11 | this.addGeoJSON(geojson);\r | |
12 | }\r | |
13 | },\r | |
14 | \r | |
15 | addGeoJSON: function(geojson) {\r | |
16 | if (geojson.features) {\r | |
17 | for (var i = 0, len = geojson.features.length; i < len; i++) {\r | |
18 | this.addGeoJSON(geojson.features[i]);\r | |
19 | }\r | |
20 | return;\r | |
21 | }\r | |
22 | \r | |
23 | var isFeature = (geojson.type == 'Feature'),\r | |
24 | geometry = (isFeature ? geojson.geometry : geojson),\r | |
25 | layer = L.GeoJSON.geometryToLayer(geometry, this.options.pointToLayer);\r | |
26 | \r | |
27 | this.fire('featureparse', {\r | |
28 | layer: layer, \r | |
29 | properties: geojson.properties,\r | |
30 | geometryType: geometry.type,\r | |
31 | bbox: geojson.bbox,\r | |
32 | id: geojson.id\r | |
33 | });\r | |
34 | \r | |
35 | this.addLayer(layer);\r | |
36 | }\r | |
37 | });\r | |
38 | \r | |
39 | L.Util.extend(L.GeoJSON, {\r | |
40 | geometryToLayer: function(geometry, pointToLayer) {\r | |
41 | var coords = geometry.coordinates, \r | |
42 | latlng, latlngs, \r | |
43 | i, len, \r | |
44 | layer, \r | |
45 | layers = [];\r | |
46 | \r | |
47 | switch (geometry.type) {\r | |
48 | case 'Point':\r | |
49 | latlng = this.coordsToLatLng(coords);\r | |
50 | return pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);\r | |
51 | \r | |
52 | case 'MultiPoint':\r | |
53 | for (i = 0, len = coords.length; i < len; i++) {\r | |
54 | latlng = this.coordsToLatLng(coords[i]);\r | |
55 | layer = pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);\r | |
56 | layers.push(layer);\r | |
57 | }\r | |
58 | return new L.FeatureGroup(layers);\r | |
59 | \r | |
60 | case 'LineString':\r | |
61 | latlngs = this.coordsToLatLngs(coords);\r | |
62 | return new L.Polyline(latlngs);\r | |
63 | \r | |
64 | case 'Polygon':\r | |
65 | latlngs = this.coordsToLatLngs(coords, 1);\r | |
66 | return new L.Polygon(latlngs);\r | |
67 | \r | |
68 | case 'MultiLineString':\r | |
69 | latlngs = this.coordsToLatLngs(coords, 1);\r | |
70 | return new L.MultiPolyline(latlngs);\r | |
71 | \r | |
72 | case "MultiPolygon":\r | |
73 | latlngs = this.coordsToLatLngs(coords, 2);\r | |
74 | return new L.MultiPolygon(latlngs);\r | |
75 | \r | |
76 | case "GeometryCollection":\r | |
77 | for (i = 0, len = geometry.geometries.length; i < len; i++) {\r | |
78 | layer = this.geometryToLayer(geometry.geometries[i]);\r | |
79 | layers.push(layer);\r | |
80 | }\r | |
81 | return new L.FeatureGroup(layers);\r | |
82 | \r | |
83 | default:\r | |
84 | throw new Error('Invalid GeoJSON object.');\r | |
85 | }\r | |
86 | },\r | |
87 | \r | |
88 | coordsToLatLng: function(/*Array*/ coords, /*Boolean*/ reverse)/*: LatLng*/ {\r | |
89 | var lat = parseFloat(coords[reverse ? 0 : 1]),\r | |
90 | lng = parseFloat(coords[reverse ? 1 : 0]);\r | |
91 | return new L.LatLng(lat, lng);\r | |
92 | },\r | |
93 | \r | |
94 | coordsToLatLngs: function(/*Array*/ coords, /*Number*/ levelsDeep, /*Boolean*/ reverse)/*: Array*/ {\r | |
95 | var latlng, latlngs = [],\r | |
96 | i, len = coords.length;\r | |
97 | \r | |
98 | for (i = 0; i < len; i++) {\r | |
99 | latlng = levelsDeep ? \r | |
100 | this.coordsToLatLngs(coords[i], levelsDeep - 1, reverse) : \r | |
101 | this.coordsToLatLng(coords[i], reverse);\r | |
102 | latlngs.push(latlng);\r | |
103 | }\r | |
104 | return latlngs;\r | |
105 | }\r | |
106 | }); |