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