2011-06-10 19:26:16 +08:00
|
|
|
|
2011-08-30 22:50:12 +08:00
|
|
|
L.GeoJSON = L.FeatureGroup.extend({
|
2011-12-09 22:51:31 +08:00
|
|
|
initialize: function (geojson, options) {
|
2011-06-10 19:26:16 +08:00
|
|
|
L.Util.setOptions(this, options);
|
|
|
|
this._geojson = geojson;
|
|
|
|
this._layers = {};
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
if (geojson) {
|
|
|
|
this.addGeoJSON(geojson);
|
|
|
|
}
|
|
|
|
},
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
addGeoJSON: function (geojson) {
|
2011-06-10 19:26:16 +08:00
|
|
|
if (geojson.features) {
|
|
|
|
for (var i = 0, len = geojson.features.length; i < len; i++) {
|
|
|
|
this.addGeoJSON(geojson.features[i]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2011-12-09 22:35:15 +08:00
|
|
|
var isFeature = (geojson.type === 'Feature'),
|
2011-06-10 19:26:16 +08:00
|
|
|
geometry = (isFeature ? geojson.geometry : geojson),
|
|
|
|
layer = L.GeoJSON.geometryToLayer(geometry, this.options.pointToLayer);
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
this.fire('featureparse', {
|
2011-12-08 23:53:17 +08:00
|
|
|
layer: layer,
|
2011-06-10 19:26:16 +08:00
|
|
|
properties: geojson.properties,
|
|
|
|
geometryType: geometry.type,
|
|
|
|
bbox: geojson.bbox,
|
|
|
|
id: geojson.id
|
|
|
|
});
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
this.addLayer(layer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
L.Util.extend(L.GeoJSON, {
|
2011-12-09 22:51:31 +08:00
|
|
|
geometryToLayer: function (geometry, pointToLayer) {
|
2011-12-08 23:53:17 +08:00
|
|
|
var coords = geometry.coordinates,
|
|
|
|
latlng, latlngs,
|
|
|
|
i, len,
|
|
|
|
layer,
|
2011-06-10 19:26:16 +08:00
|
|
|
layers = [];
|
|
|
|
|
|
|
|
switch (geometry.type) {
|
2011-12-08 23:53:17 +08:00
|
|
|
case 'Point':
|
|
|
|
latlng = this.coordsToLatLng(coords);
|
|
|
|
return pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);
|
|
|
|
|
|
|
|
case 'MultiPoint':
|
|
|
|
for (i = 0, len = coords.length; i < len; i++) {
|
|
|
|
latlng = this.coordsToLatLng(coords[i]);
|
|
|
|
layer = pointToLayer ? pointToLayer(latlng) : new L.Marker(latlng);
|
|
|
|
layers.push(layer);
|
|
|
|
}
|
|
|
|
return new L.FeatureGroup(layers);
|
|
|
|
|
|
|
|
case 'LineString':
|
|
|
|
latlngs = this.coordsToLatLngs(coords);
|
|
|
|
return new L.Polyline(latlngs);
|
|
|
|
|
|
|
|
case 'Polygon':
|
|
|
|
latlngs = this.coordsToLatLngs(coords, 1);
|
|
|
|
return new L.Polygon(latlngs);
|
|
|
|
|
|
|
|
case 'MultiLineString':
|
|
|
|
latlngs = this.coordsToLatLngs(coords, 1);
|
|
|
|
return new L.MultiPolyline(latlngs);
|
|
|
|
|
|
|
|
case "MultiPolygon":
|
|
|
|
latlngs = this.coordsToLatLngs(coords, 2);
|
|
|
|
return new L.MultiPolygon(latlngs);
|
|
|
|
|
|
|
|
case "GeometryCollection":
|
|
|
|
for (i = 0, len = geometry.geometries.length; i < len; i++) {
|
2011-12-10 00:19:58 +08:00
|
|
|
layer = this.geometryToLayer(geometry.geometries[i], pointToLayer);
|
2011-12-08 23:53:17 +08:00
|
|
|
layers.push(layer);
|
|
|
|
}
|
|
|
|
return new L.FeatureGroup(layers);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Error('Invalid GeoJSON object.');
|
2011-06-10 19:26:16 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
coordsToLatLng: function (/*Array*/ coords, /*Boolean*/ reverse)/*: LatLng*/ {
|
2011-06-17 21:46:03 +08:00
|
|
|
var lat = parseFloat(coords[reverse ? 0 : 1]),
|
|
|
|
lng = parseFloat(coords[reverse ? 1 : 0]);
|
2011-12-15 18:07:35 +08:00
|
|
|
return new L.LatLng(lat, lng, true);
|
2011-06-10 19:26:16 +08:00
|
|
|
},
|
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
coordsToLatLngs: function (/*Array*/ coords, /*Number*/ levelsDeep, /*Boolean*/ reverse)/*: Array*/ {
|
2011-06-10 19:26:16 +08:00
|
|
|
var latlng, latlngs = [],
|
|
|
|
i, len = coords.length;
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
for (i = 0; i < len; i++) {
|
2011-12-08 23:53:17 +08:00
|
|
|
latlng = levelsDeep ?
|
|
|
|
this.coordsToLatLngs(coords[i], levelsDeep - 1, reverse) :
|
2011-06-16 18:56:39 +08:00
|
|
|
this.coordsToLatLng(coords[i], reverse);
|
2011-06-10 19:26:16 +08:00
|
|
|
latlngs.push(latlng);
|
|
|
|
}
|
|
|
|
return latlngs;
|
|
|
|
}
|
2011-12-08 23:53:17 +08:00
|
|
|
});
|