Use TopoJSON for smaller files

This commit is contained in:
Norbert Renner 2021-01-22 11:50:27 +01:00
parent dc8302479e
commit 3037fb8f17
8 changed files with 64 additions and 24 deletions

View file

@ -30,6 +30,38 @@ BR.Util = {
return new Error(msg);
},
getJson: function (url, context, cb) {
BR.Util.get(url, function (err, data) {
if (err) {
BR.message.showError('Error getting ' + context + ': ' + err);
return cb(err);
}
try {
var json = JSON.parse(data);
cb(null, json);
} catch (err) {
BR.message.showError('Error parsing ' + context + ': ' + err);
console.error(err);
cb(err);
}
});
},
getGeoJson: function (url, context, cb) {
BR.Util.getJson(url, context, function (err, data) {
if (err) return cb(err);
var geoJson = data;
if (data && data.type && data.type === 'Topology') {
var key = Object.keys(data.objects)[0];
geoJson = topojson.feature(data, data.objects[key]);
}
cb(null, geoJson);
});
},
// check if localStorage is available, especially for catching SecurityError
// when cookie settings are blocking access (Chrome, Pale Moon, older Firefox)
//

View file

@ -7,7 +7,7 @@ BR.CircleGoArea = L.Control.extend({
options: {
radius: 1000, // in meters
stateRules: false,
statesUrl: BR.conf.statesUrl || 'dist/boundaries/germany-states.geojson',
statesUrl: BR.conf.statesUrl || 'dist/boundaries/germany-states.topo.json',
overpassBaseUrl: BR.conf.overpassBaseUrl || 'https://overpass-api.de/api/interpreter?data=',
shortcut: {
draw: {
@ -276,28 +276,21 @@ BR.CircleGoArea = L.Control.extend({
},
_loadStates: function (cb) {
BR.Util.get(
BR.Util.getGeoJson(
this.options.statesUrl,
'states',
L.bind(function (err, data) {
if (err) {
BR.message.showError('Error getting states: ' + err);
return;
}
// TODO spinner aus?, mit ringgo parameter testen
if (err) return;
try {
this.states = JSON.parse(data);
this.states = data;
// debugging
//this._logStates(this.states);
//this._addStatesLayer(this.states);
// debugging
//this._logStates(this.states);
//this._addStatesLayer(this.states);
this.fire('states:loaded');
cb();
} catch (err) {
BR.message.showError('Error parsing states: ' + err);
console.error(err);
}
this.fire('states:loaded');
cb();
}, this)
);
},