153 lines
3.8 KiB
JavaScript
153 lines
3.8 KiB
JavaScript
/*
|
|
* L.GeoJSON turns any GeoJSON data into a Leaflet layer.
|
|
*/
|
|
|
|
L.GeoJSON = L.FeatureGroup.extend({
|
|
|
|
initialize: function (geojson, options) {
|
|
L.setOptions(this, options);
|
|
|
|
this._layers = {};
|
|
|
|
if (geojson) {
|
|
this.addData(geojson);
|
|
}
|
|
},
|
|
|
|
addData: function (geojson) {
|
|
var features = L.Util.isArray(geojson) ? geojson : geojson.features,
|
|
i, len;
|
|
|
|
if (features) {
|
|
for (i = 0, len = features.length; i < len; i++) {
|
|
// Only add this if geometry or geometries are set and not null
|
|
if (features[i].geometries || features[i].geometry || features[i].features) {
|
|
this.addData(features[i]);
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
|
|
var options = this.options;
|
|
|
|
if (options.filter && !options.filter(geojson)) { return; }
|
|
|
|
var layer = L.GeoJSON.geometryToLayer(geojson, options.pointToLayer);
|
|
layer.feature = geojson;
|
|
|
|
layer.defaultOptions = layer.options;
|
|
this.resetStyle(layer);
|
|
|
|
if (options.onEachFeature) {
|
|
options.onEachFeature(geojson, layer);
|
|
}
|
|
|
|
return this.addLayer(layer);
|
|
},
|
|
|
|
resetStyle: function (layer) {
|
|
var style = this.options.style;
|
|
if (style) {
|
|
// reset any custom styles
|
|
L.Util.extend(layer.options, layer.defaultOptions);
|
|
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
|
|
L.extend(L.GeoJSON, {
|
|
geometryToLayer: function (geojson, pointToLayer) {
|
|
var geometry = geojson.type === 'Feature' ? geojson.geometry : geojson,
|
|
coords = geometry.coordinates,
|
|
layers = [],
|
|
latlng, latlngs, i, len, layer;
|
|
|
|
switch (geometry.type) {
|
|
case 'Point':
|
|
latlng = this.coordsToLatLng(coords);
|
|
return pointToLayer ? pointToLayer(geojson, latlng) : new L.Marker(latlng);
|
|
|
|
case 'MultiPoint':
|
|
for (i = 0, len = coords.length; i < len; i++) {
|
|
latlng = this.coordsToLatLng(coords[i]);
|
|
layer = pointToLayer ? pointToLayer(geojson, 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++) {
|
|
layer = this.geometryToLayer({
|
|
geometry: geometry.geometries[i],
|
|
type: 'Feature',
|
|
properties: geojson.properties
|
|
}, pointToLayer);
|
|
layers.push(layer);
|
|
}
|
|
return new L.FeatureGroup(layers);
|
|
|
|
default:
|
|
throw new Error('Invalid GeoJSON object.');
|
|
}
|
|
},
|
|
|
|
coordsToLatLng: function (coords, reverse) { // (Array, Boolean) -> LatLng
|
|
var lat = parseFloat(coords[reverse ? 0 : 1]),
|
|
lng = parseFloat(coords[reverse ? 1 : 0]);
|
|
|
|
return new L.LatLng(lat, lng);
|
|
},
|
|
|
|
coordsToLatLngs: function (coords, levelsDeep, reverse) { // (Array, Number, Boolean) -> Array
|
|
var latlng,
|
|
latlngs = [],
|
|
i, len;
|
|
|
|
for (i = 0, len = coords.length; i < len; i++) {
|
|
latlng = levelsDeep ?
|
|
this.coordsToLatLngs(coords[i], levelsDeep - 1, reverse) :
|
|
this.coordsToLatLng(coords[i], reverse);
|
|
|
|
latlngs.push(latlng);
|
|
}
|
|
|
|
return latlngs;
|
|
}
|
|
});
|
|
|
|
L.geoJson = function (geojson, options) {
|
|
return new L.GeoJSON(geojson, options);
|
|
};
|