2012-12-11 23:16:39 +08:00
|
|
|
/*
|
|
|
|
* L.GeoJSON turns any GeoJSON data into a Leaflet layer.
|
|
|
|
*/
|
|
|
|
|
2011-08-30 22:50:12 +08:00
|
|
|
L.GeoJSON = L.FeatureGroup.extend({
|
2012-12-11 23:16:39 +08:00
|
|
|
|
2011-12-09 22:51:31 +08:00
|
|
|
initialize: function (geojson, options) {
|
2012-11-08 02:30:56 +08:00
|
|
|
L.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) {
|
2013-01-21 08:13:43 +08:00
|
|
|
var features = L.Util.isArray(geojson) ? geojson : geojson.features,
|
2012-02-23 17:01:03 +08:00
|
|
|
i, len;
|
2013-01-21 09:18:48 +08:00
|
|
|
|
2012-02-15 19:17:25 +08:00
|
|
|
if (features) {
|
|
|
|
for (i = 0, len = features.length; i < len; i++) {
|
2012-12-27 11:15:08 +08:00
|
|
|
// Only add this if geometry or geometries are set and not null
|
2013-01-21 23:14:42 +08:00
|
|
|
if (features[i].geometries || features[i].geometry || features[i].features) {
|
2012-12-27 11:15:08 +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-08-04 15:56:14 +08:00
|
|
|
var options = this.options;
|
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
|
|
|
|
2013-04-20 20:45:04 +08:00
|
|
|
var layer = L.GeoJSON.geometryToLayer(geojson, options.pointToLayer, options.coordsToLatLng);
|
2013-06-25 01:52:05 +08:00
|
|
|
layer.feature = L.GeoJSON.asFeature(geojson);
|
2012-07-09 19:42:44 +08:00
|
|
|
|
2012-12-12 21:42:21 +08:00
|
|
|
layer.defaultOptions = layer.options;
|
2012-08-04 15:56:14 +08:00
|
|
|
this.resetStyle(layer);
|
2012-07-09 19:42:44 +08:00
|
|
|
|
|
|
|
if (options.onEachFeature) {
|
|
|
|
options.onEachFeature(geojson, layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.addLayer(layer);
|
2012-08-04 15:56:14 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
resetStyle: function (layer) {
|
|
|
|
var style = this.options.style;
|
|
|
|
if (style) {
|
2012-12-11 23:16:39 +08:00
|
|
|
// reset any custom styles
|
2012-12-12 21:42:21 +08:00
|
|
|
L.Util.extend(layer.options, layer.defaultOptions);
|
2012-12-11 23:16:39 +08:00
|
|
|
|
2012-08-04 15:56:14 +08:00
|
|
|
this._setLayerStyle(layer, style);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setStyle: function (style) {
|
|
|
|
this.eachLayer(function (layer) {
|
|
|
|
this._setLayerStyle(layer, style);
|
|
|
|
}, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
_setLayerStyle: function (layer, style) {
|
|
|
|
if (typeof style === 'function') {
|
|
|
|
style = style(layer.feature);
|
|
|
|
}
|
|
|
|
if (layer.setStyle) {
|
|
|
|
layer.setStyle(style);
|
|
|
|
}
|
2011-06-10 19:26:16 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-11-08 02:30:56 +08:00
|
|
|
L.extend(L.GeoJSON, {
|
2013-04-20 20:45:04 +08:00
|
|
|
geometryToLayer: function (geojson, pointToLayer, coordsToLatLng) {
|
2012-07-09 19:42:44 +08:00
|
|
|
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
|
|
|
|
2013-04-20 20:45:04 +08:00
|
|
|
coordsToLatLng = coordsToLatLng || this.coordsToLatLng;
|
|
|
|
|
2011-06-10 19:26:16 +08:00
|
|
|
switch (geometry.type) {
|
2011-12-08 23:53:17 +08:00
|
|
|
case 'Point':
|
2013-04-20 20:45:04 +08:00
|
|
|
latlng = 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++) {
|
2013-04-20 20:45:04 +08:00
|
|
|
latlng = 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':
|
2013-04-20 20:45:04 +08:00
|
|
|
latlngs = this.coordsToLatLngs(coords, 0, coordsToLatLng);
|
2011-12-08 23:53:17 +08:00
|
|
|
return new L.Polyline(latlngs);
|
|
|
|
|
|
|
|
case 'Polygon':
|
2013-04-20 20:45:04 +08:00
|
|
|
latlngs = this.coordsToLatLngs(coords, 1, coordsToLatLng);
|
2011-12-08 23:53:17 +08:00
|
|
|
return new L.Polygon(latlngs);
|
|
|
|
|
|
|
|
case 'MultiLineString':
|
2013-04-20 20:45:04 +08:00
|
|
|
latlngs = this.coordsToLatLngs(coords, 1, coordsToLatLng);
|
2011-12-08 23:53:17 +08:00
|
|
|
return new L.MultiPolyline(latlngs);
|
|
|
|
|
2013-01-14 20:25:34 +08:00
|
|
|
case 'MultiPolygon':
|
2013-04-20 20:45:04 +08:00
|
|
|
latlngs = this.coordsToLatLngs(coords, 2, coordsToLatLng);
|
2011-12-08 23:53:17 +08:00
|
|
|
return new L.MultiPolygon(latlngs);
|
|
|
|
|
2013-01-14 20:25:34 +08:00
|
|
|
case 'GeometryCollection':
|
2011-12-08 23:53:17 +08:00
|
|
|
for (i = 0, len = geometry.geometries.length; i < len; i++) {
|
2013-04-20 20:45:04 +08:00
|
|
|
|
2013-01-14 20:25:34 +08:00
|
|
|
layer = this.geometryToLayer({
|
|
|
|
geometry: geometry.geometries[i],
|
|
|
|
type: 'Feature',
|
|
|
|
properties: geojson.properties
|
2013-06-18 09:31:24 +08:00
|
|
|
}, pointToLayer, coordsToLatLng);
|
2013-04-20 20:45:04 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-04-20 20:45:04 +08:00
|
|
|
coordsToLatLng: function (coords) { // (Array[, Boolean]) -> LatLng
|
|
|
|
return new L.LatLng(coords[1], coords[0]);
|
2011-06-10 19:26:16 +08:00
|
|
|
},
|
|
|
|
|
2013-04-20 20:45:04 +08:00
|
|
|
coordsToLatLngs: function (coords, levelsDeep, coordsToLatLng) { // (Array[, Number, Function]) -> Array
|
|
|
|
var latlng, i, len,
|
|
|
|
latlngs = [];
|
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 ?
|
2013-04-20 20:45:04 +08:00
|
|
|
this.coordsToLatLngs(coords[i], levelsDeep - 1, coordsToLatLng) :
|
2013-04-20 20:49:04 +08:00
|
|
|
(coordsToLatLng || this.coordsToLatLng)(coords[i]);
|
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;
|
2013-04-20 20:45:04 +08:00
|
|
|
},
|
2012-07-05 19:45:06 +08:00
|
|
|
|
2013-02-27 06:54:58 +08:00
|
|
|
latLngToCoords: function (latLng) {
|
|
|
|
return [latLng.lng, latLng.lat];
|
|
|
|
},
|
|
|
|
|
|
|
|
latLngsToCoords: function (latLngs) {
|
2013-04-20 20:45:04 +08:00
|
|
|
var coords = [];
|
2013-02-27 06:54:58 +08:00
|
|
|
|
2013-04-20 20:45:04 +08:00
|
|
|
for (var i = 0, len = latLngs.length; i < len; i++) {
|
2013-02-27 06:54:58 +08:00
|
|
|
coords.push(L.GeoJSON.latLngToCoords(latLngs[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
return coords;
|
2013-04-20 01:16:43 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getFeature: function (layer, newGeometry) {
|
2013-06-25 01:52:05 +08:00
|
|
|
return layer.feature ? L.extend({}, layer.feature, {geometry: newGeometry}) : L.GeoJSON.asFeature(newGeometry);
|
|
|
|
},
|
|
|
|
|
|
|
|
asFeature: function (geoJSON) {
|
|
|
|
if (geoJSON.type === 'Feature') {
|
|
|
|
return geoJSON;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'Feature',
|
|
|
|
properties: {},
|
|
|
|
geometry: geoJSON
|
|
|
|
};
|
2013-02-27 06:54:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
L.Marker.include({
|
|
|
|
toGeoJSON: function () {
|
2013-04-20 01:16:43 +08:00
|
|
|
return L.GeoJSON.getFeature(this, {
|
2013-02-27 06:54:58 +08:00
|
|
|
type: 'Point',
|
|
|
|
coordinates: L.GeoJSON.latLngToCoords(this.getLatLng())
|
2013-04-20 01:16:43 +08:00
|
|
|
});
|
2013-02-27 06:54:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
L.Polyline.include({
|
|
|
|
toGeoJSON: function () {
|
2013-04-20 01:16:43 +08:00
|
|
|
return L.GeoJSON.getFeature(this, {
|
2013-02-27 06:54:58 +08:00
|
|
|
type: 'LineString',
|
|
|
|
coordinates: L.GeoJSON.latLngsToCoords(this.getLatLngs())
|
2013-04-20 01:16:43 +08:00
|
|
|
});
|
2013-02-27 06:54:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
L.Polygon.include({
|
|
|
|
toGeoJSON: function () {
|
|
|
|
var coords = [L.GeoJSON.latLngsToCoords(this.getLatLngs())],
|
|
|
|
i, len, hole;
|
|
|
|
|
|
|
|
coords[0].push(coords[0][0]);
|
|
|
|
|
|
|
|
if (this._holes) {
|
|
|
|
for (i = 0, len = this._holes.length; i < len; i++) {
|
|
|
|
hole = L.GeoJSON.latLngsToCoords(this._holes[i]);
|
|
|
|
hole.push(hole[0]);
|
|
|
|
coords.push(hole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-20 01:16:43 +08:00
|
|
|
return L.GeoJSON.getFeature(this, {
|
2013-02-27 06:54:58 +08:00
|
|
|
type: 'Polygon',
|
|
|
|
coordinates: coords
|
2013-04-20 01:16:43 +08:00
|
|
|
});
|
2013-02-27 06:54:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
function includeMulti(Klass, type) {
|
|
|
|
Klass.include({
|
|
|
|
toGeoJSON: function () {
|
|
|
|
var coords = [];
|
|
|
|
|
|
|
|
this.eachLayer(function (layer) {
|
2013-06-25 01:52:05 +08:00
|
|
|
coords.push(layer.toGeoJSON().geometry.coordinates);
|
2013-02-27 06:54:58 +08:00
|
|
|
});
|
|
|
|
|
2013-04-20 01:16:43 +08:00
|
|
|
return L.GeoJSON.getFeature(this, {
|
2013-02-27 06:54:58 +08:00
|
|
|
type: type,
|
|
|
|
coordinates: coords
|
2013-04-20 01:16:43 +08:00
|
|
|
});
|
2013-02-27 06:54:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
includeMulti(L.MultiPolyline, 'MultiLineString');
|
|
|
|
includeMulti(L.MultiPolygon, 'MultiPolygon');
|
|
|
|
}());
|
|
|
|
|
|
|
|
L.LayerGroup.include({
|
|
|
|
toGeoJSON: function () {
|
2013-06-25 01:52:05 +08:00
|
|
|
var features = [];
|
2013-02-27 06:54:58 +08:00
|
|
|
|
|
|
|
this.eachLayer(function (layer) {
|
|
|
|
if (layer.toGeoJSON) {
|
2013-06-25 01:52:05 +08:00
|
|
|
features.push(L.GeoJSON.asFeature(layer.toGeoJSON()));
|
2013-02-27 06:54:58 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-25 01:52:05 +08:00
|
|
|
return {
|
|
|
|
type: 'FeatureCollection',
|
|
|
|
features: features
|
|
|
|
};
|
2013-02-27 06:54:58 +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-08-04 15:56:14 +08:00
|
|
|
};
|