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);
|
2012-02-15 19:17:25 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
this._layers = {};
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
if (geojson) {
|
2012-07-09 19:42:44 +08:00
|
|
|
this.addData(geojson);
|
2011-06-10 19:26:16 +08:00
|
|
|
}
|
|
|
|
},
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2012-07-09 19:42:44 +08:00
|
|
|
addData: function (geojson) {
|
2012-07-12 20:37:43 +08:00
|
|
|
var features = geojson instanceof Array ? geojson : geojson.features,
|
2012-02-23 17:01:03 +08:00
|
|
|
i, len;
|
2012-02-15 19:17:25 +08:00
|
|
|
|
|
|
|
if (features) {
|
|
|
|
for (i = 0, len = features.length; i < len; i++) {
|
2012-07-09 19:42:44 +08:00
|
|
|
this.addData(features[i]);
|
2011-06-10 19:26:16 +08:00
|
|
|
}
|
2012-07-09 19:42:44 +08:00
|
|
|
return this;
|
2011-06-10 19:26:16 +08:00
|
|
|
}
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2012-07-09 19:42:44 +08:00
|
|
|
var options = this.options,
|
|
|
|
style = options.style;
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2012-07-09 19:42:44 +08:00
|
|
|
if (options.filter && !options.filter(geojson)) { return; }
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2012-07-09 19:42:44 +08:00
|
|
|
var layer = L.GeoJSON.geometryToLayer(geojson, options.pointToLayer);
|
|
|
|
|
|
|
|
if (style) {
|
|
|
|
if (typeof style === 'function') {
|
|
|
|
style = style(geojson);
|
|
|
|
}
|
|
|
|
if (layer.setStyle) {
|
|
|
|
layer.setStyle(style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.onEachFeature) {
|
|
|
|
options.onEachFeature(geojson, layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.addLayer(layer);
|
2011-06-10 19:26:16 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
L.Util.extend(L.GeoJSON, {
|
2012-07-09 19:42:44 +08:00
|
|
|
geometryToLayer: function (geojson, pointToLayer) {
|
|
|
|
var geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
|
|
|
|
coords = geometry.coordinates,
|
2012-02-23 17:01:03 +08:00
|
|
|
layers = [],
|
|
|
|
latlng, latlngs, i, len, layer;
|
2011-06-10 19:26:16 +08:00
|
|
|
|
|
|
|
switch (geometry.type) {
|
2011-12-08 23:53:17 +08:00
|
|
|
case 'Point':
|
|
|
|
latlng = this.coordsToLatLng(coords);
|
2012-07-09 19:42:44 +08:00
|
|
|
return pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
|
2011-12-08 23:53:17 +08:00
|
|
|
|
|
|
|
case 'MultiPoint':
|
|
|
|
for (i = 0, len = coords.length; i < len; i++) {
|
|
|
|
latlng = this.coordsToLatLng(coords[i]);
|
2012-07-09 19:42:44 +08:00
|
|
|
layer = pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
|
2011-12-08 23:53:17 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-02-15 19:17:25 +08:00
|
|
|
coordsToLatLng: function (coords, reverse) { // (Array, Boolean) -> LatLng
|
2011-06-17 21:46:03 +08:00
|
|
|
var lat = parseFloat(coords[reverse ? 0 : 1]),
|
2012-02-23 17:01:03 +08:00
|
|
|
lng = parseFloat(coords[reverse ? 1 : 0]);
|
2012-02-15 19:17:25 +08:00
|
|
|
|
2011-12-15 18:07:35 +08:00
|
|
|
return new L.LatLng(lat, lng, true);
|
2011-06-10 19:26:16 +08:00
|
|
|
},
|
|
|
|
|
2012-02-15 19:17:25 +08:00
|
|
|
coordsToLatLngs: function (coords, levelsDeep, reverse) { // (Array, Number, Boolean) -> Array
|
|
|
|
var latlng,
|
2012-02-23 17:01:03 +08:00
|
|
|
latlngs = [],
|
|
|
|
i, len;
|
2011-12-08 23:53:17 +08:00
|
|
|
|
2012-02-15 19:17:25 +08:00
|
|
|
for (i = 0, len = coords.length; 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);
|
2012-07-09 19:42:44 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
latlngs.push(latlng);
|
|
|
|
}
|
2012-02-15 19:17:25 +08:00
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
return latlngs;
|
|
|
|
}
|
2011-12-08 23:53:17 +08:00
|
|
|
});
|
2012-07-05 19:45:06 +08:00
|
|
|
|
|
|
|
L.geoJson = function (geojson, options) {
|
2012-07-09 19:42:44 +08:00
|
|
|
return new L.GeoJSON(geojson, options);
|
2012-07-05 19:45:06 +08:00
|
|
|
};
|