Determine allowed zone from admin boundaries (#359)

This commit is contained in:
Norbert Renner 2021-01-15 22:29:20 +01:00 committed by GitHub
parent 5df0765d51
commit a5f04dd9cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 1925 additions and 1171 deletions

View file

@ -107,4 +107,17 @@ BR.Util = {
temp.textContent = str;
return temp.innerHTML;
},
isCountry: function (country, language) {
// de-DE | fr-FR
var lang = i18next.languages[0].split('-');
if (lang.length > 1) {
// if available only test country, to avoid e.g. de-CH to match
return lang[1] === country;
}
// fallback when country not available
return lang[0] === language;
},
};

View file

@ -321,26 +321,14 @@
nogos.addTo(map);
var circlegoRadius = null;
var lang = i18next.languages.length && i18next.languages[0];
if (lang.startsWith('fr')) {
circlegoRadius = 20000;
}
if (lang.startsWith('de')) {
circlegoRadius = 15000;
}
if (circlegoRadius != null) {
circlego = new BR.CircleGoArea(routing, nogos, pois);
circlego.options.radius = circlegoRadius;
circlego = BR.circleGoArea(routing, nogos, pois);
if (circlego != null) {
pois.circlego = circlego;
circlego.addTo(map);
}
var buttons = [drawButton, reverseRouteButton, nogos.getButton()];
if (circlegoRadius) buttons.push(circlego.getButton());
if (circlego) buttons.push(circlego.getButton());
buttons.push(deletePointButton, deleteRouteButton);
L.easyBar(buttons).addTo(map);
@ -411,8 +399,7 @@
pois.setMarkers(opts.pois);
}
if (circlego && opts.circlego) {
circlego.options.radius = opts.circlego[2];
circlego.setCircle([opts.circlego[0], opts.circlego[1]], opts.polylines != null);
circlego.setOptions(opts);
}
};

View file

@ -1,9 +1,14 @@
BR.CircleGoArea = L.Control.extend({
circleLayer: null,
boundaryLayer: null,
outsideAreaRenderer: L.svg({ padding: 1 }),
states: null,
options: {
radius: 1000, // in meters
stateRules: false,
statesUrl: BR.conf.statesUrl || 'dist/boundaries/germany-states.geojson',
overpassBaseUrl: BR.conf.overpassBaseUrl || 'https://overpass-api.de/api/interpreter?data=',
shortcut: {
draw: {
enable: 73, // char code for 'i'
@ -11,10 +16,12 @@ BR.CircleGoArea = L.Control.extend({
},
},
},
initialize: function (routing, nogos, pois) {
initialize: function (routing, nogos, pois, options) {
this.routing = routing;
this.nogos = nogos;
this.pois = pois;
L.setOptions(this, options);
},
onAdd: function (map) {
@ -51,6 +58,18 @@ BR.CircleGoArea = L.Control.extend({
],
});
if (this.options.stateRules) {
this.drawButton.disable();
this._loadStates(
L.bind(function () {
this.drawButton.enable();
if (this.marker && !this.marker.dragging.enabled()) {
this.marker.dragging.enable();
}
}, this)
);
}
map.on('routing:draw-start', function () {
self.draw(false);
});
@ -67,9 +86,11 @@ BR.CircleGoArea = L.Control.extend({
this.routing.draw(false);
this.pois.draw(false);
this.map.on('click', this.onMapClick, this);
this._unlockOutsideArea();
L.DomUtil.addClass(this.map.getContainer(), 'circlego-draw-enabled');
} else {
this.map.off('click', this.onMapClick, this);
this._lockOutsideArea();
L.DomUtil.removeClass(this.map.getContainer(), 'circlego-draw-enabled');
}
},
@ -85,91 +106,360 @@ BR.CircleGoArea = L.Control.extend({
}
},
setNogoCircle: function (center) {
if (center) {
var polygon = this.circleToPolygon(center, this.options.radius);
var geoJson = JSON.stringify(polygon);
$('#nogoJSON').val(geoJson);
$('#nogoBuffer').val(0);
this.nogos.uploadNogos();
_getBoundary: function (center, adminLevel, adminLevelFallback) {
adminLevel = adminLevel || 8;
var query =
'[out:json]; is_in(' +
center[1] +
', ' +
center[0] +
')->.a;' +
'(area.a[admin_level="' +
adminLevel +
'"];)->.p; relation(pivot.p); out geom;';
var polygonClone = JSON.parse(geoJson);
this.setOutsideArea(polygonClone);
if (adminLevelFallback) {
// order is important (separate out), so fallback entry always comes last in result
query += '(area.a[admin_level="' + adminLevelFallback + '"];)->.p; relation(pivot.p); out geom;';
}
var url = this.options.overpassBaseUrl + encodeURIComponent(query);
this.marker.setIcon(this.iconSpinner);
BR.Util.get(
url,
L.bind(function (err, data) {
if (err) {
BR.message.showError('Error getting boundary for coordinate "' + center + '": ' + err);
return;
}
try {
var osmJson = JSON.parse(data);
if (osmJson.elements.length === 0) {
if (adminLevel === 8 || adminLevel === 7) {
// admin_level 6 (kreisfreie Stadt)
this._getBoundary(center, 6);
} else {
BR.message.showError('No boundary found for coordinate "' + center + '"');
}
return;
}
var geoJson = osmtogeojson(osmJson);
this._setBoundary(geoJson);
this.marker.setIcon(this.icon);
} catch (err) {
BR.message.showError('Error parsing boundary: ' + err);
console.error(err);
} finally {
this.marker.setIcon(this.icon);
}
}, this)
);
},
_setBoundary: function (geoJson) {
// drop admin_centre nodes
geoJson.features = geoJson.features.filter(function (feature) {
return feature.geometry.type !== 'Point';
});
var boundaryLine = turf.polygonToLine(geoJson.features[0]);
this.boundaryLayer = L.geoJson(boundaryLine, {
style: function (feature) {
return {
weight: 1,
color: 'black',
opacity: 0.6,
interactive: false,
};
},
}).addTo(this.map);
var buffer = turf.buffer(geoJson, this.options.radius, { units: 'meters' });
var ring = turf.polygonToLine(buffer.features[0]);
if (ring.type !== 'FeatureCollection') {
ring = turf.featureCollection([ring]);
}
/* hack: it seems there is a bug when using a single closed ring line,
cf. https://github.com/nrenner/brouter-web/issues/349#issue-755514458
so instead cut into smaller chunks of about half the circumference that worked for circles */
var split = turf.lineChunk(ring, 62, { units: 'kilometers' });
this._setNogo(split);
this.setOutsideArea(ring);
},
_getState: function (center) {
var state = null;
var point = turf.point(center);
var features = this.states.features;
for (var i = 0; i < features.length; i++) {
var feature = features[i];
var inside = turf.booleanPointInPolygon(point, feature);
if (inside) {
state = feature;
break;
}
}
return state;
},
_applyStateRules: function (center) {
var state = this._getState(center);
if (state) {
var ref = state.properties.all_tags.ref;
if (['MV', 'NDS', 'SL', 'SN'].indexOf(ref) !== -1) {
// Address
this._setNogoCircle(center);
} else if (ref === 'ST') {
// admin_level 7 + 8 (Gemeinde oder Verbandsgemeinde)
// Also select AL 8 (Gemeinde) as fallback where there is no AL 7.
// AL 7 first, so we can always take first entry.
this._getBoundary(center, 7, 8);
} else if (ref === 'BB') {
// admin_level 6 (Landkreis oder kreisfreie Stadt)
this._getBoundary(center, 6);
} else if (ref === 'HH' || state.properties.id === -62422) {
// admin_level 4 - (Bundesländer und Stadtstaaten)
// Hamburg and Berlin (AL 4) included in states file (Berlin ref missing)
this._setBoundary(turf.featureCollection([state]));
} else {
console.error('unhandled state: ' + ref + ', id: ' + state.properties.id);
this._getBoundary(center);
}
} else {
this.nogos.clear();
this.map.removeLayer(this.outsideArea);
// admin_level 8 (Gemeinde)
this._getBoundary(center);
}
},
setOutsideArea: function (polygon) {
var inner = polygon.features[0].geometry.coordinates.concat(polygon.features[1].geometry.coordinates);
var world = [
[180, 90],
[-180, 90],
[-180, -90],
[180, -90],
[180, 90],
];
polygon.features[0].geometry.coordinates = [world, inner];
polygon.features[0].geometry.type = 'Polygon';
polygon.features.pop();
if (this.outsideArea) {
this.map.removeLayer(this.outsideArea);
// debugging
_logStates: function (states) {
for (var i = 0; i < states.features.length; i++) {
var state = states.features[i];
console.log(
state.properties.all_tags.ref + ', ' + state.properties.all_tags.name + ', ' + state.properties.id
);
}
},
this.outsideArea = L.geoJson(polygon, {
// debugging
_addStatesLayer: function (states) {
// delay, otherwise triggers premature hash update through mapmove
setTimeout(
L.bind(function () {
L.geoJson(states, {
style: function (feature) {
return {
weight: 1,
color: 'navy',
opacity: 0.8,
fill: false,
interactive: false,
};
},
}).addTo(this.map);
}, this),
100
);
},
_loadStates: function (cb) {
BR.Util.get(
this.options.statesUrl,
L.bind(function (err, data) {
if (err) {
BR.message.showError('Error getting states: ' + err);
return;
}
try {
this.states = JSON.parse(data);
// 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)
);
},
_setNogo: function (ring) {
this.nogoPolylines = L.geoJson(ring, BR.NogoAreas.prototype.polylineOptions);
this.nogos.addNogos(null, this.nogoPolylines.getLayers(), null);
},
_removeNogo: function () {
if (this.nogoPolylines) {
this.nogos.removeNogos(null, this.nogoPolylines.getLayers(), null);
this.nogoPolylines = null;
}
},
_setNogoCircle: function (center) {
var polygon = this.circleToPolygon(center, this.options.radius);
this._setNogo(polygon);
this.setOutsideArea(polygon);
},
setNogoRing: function (center) {
this._clearLayers();
this._removeNogo();
if (center) {
if (this.options.stateRules) {
if (!this.states) {
// wait for states to be loaded (when circlego hash parameter without polylines)
this.marker.setIcon(this.iconSpinner);
this.once(
'states:loaded',
function () {
this._applyStateRules(center);
this.marker.setIcon(this.icon);
},
this
);
} else {
this._applyStateRules(center);
}
} else {
this._setNogoCircle(center);
}
}
},
_lockOutsideArea: function () {
if (this.outsideArea) {
this.outsideArea.eachLayer(function (layer) {
layer._path.classList.add('circlego-outside');
});
this.outsideArea.on('click', L.DomEvent.stop);
}
},
_unlockOutsideArea: function () {
if (this.outsideArea) {
this.outsideArea.eachLayer(function (layer) {
layer._path.classList.remove('circlego-outside');
});
this.outsideArea.off('click', L.DomEvent.stop);
}
},
setOutsideArea: function (ring) {
var mask = turf.mask(turf.polygonize(ring));
this.outsideArea = L.geoJson(mask, {
renderer: this.outsideAreaRenderer,
style: function (feature) {
return {
weight: 2,
weight: 4,
color: 'black',
opacity: 0.4,
fillColor: 'black',
fillOpacity: 0.4,
className: 'circlego-outside',
};
},
})
.on('click', L.DomEvent.stop)
.addTo(this.map);
smoothFactor: 0.5,
}).addTo(this.map);
this._lockOutsideArea();
},
onMapClick: function (e) {
this.setCircle([e.latlng.lng, e.latlng.lat], false);
this.setCircle([e.latlng.lng, e.latlng.lat]);
},
setCircle: function (center, skipNogo) {
setOptions: function (opts) {
this.options.radius = opts.circlego[2];
if (opts.polylines) {
this.nogoPolylines = L.featureGroup(opts.polylines, BR.NogoAreas.prototype.polylineOptions);
}
this.setCircle([opts.circlego[0], opts.circlego[1]], opts.polylines);
},
setCircle: function (center, polylines) {
var self = this;
var icon = L.VectorMarkers.icon({
var icon = (this.icon = L.VectorMarkers.icon({
icon: 'home',
markerColor: BR.conf.markerColors.circlego,
}));
this.iconSpinner = L.VectorMarkers.icon({
icon: 'spinner',
spin: true,
markerColor: BR.conf.markerColors.circlego,
});
var marker = L.marker([center[1], center[0]], { icon: icon, draggable: true, name: name })
var marker = (this.marker = L.marker([center[1], center[0]], {
icon: icon,
draggable: true,
// prevent being on top of route markers
zIndexOffset: -500,
})
.on('dragend', function (e) {
self.setNogoCircle([e.target.getLatLng().lng, e.target.getLatLng().lat]);
self.setNogoRing([e.target.getLatLng().lng, e.target.getLatLng().lat]);
})
.on('click', function () {
var drawing = self.drawButton.state() == 'deactivate-circlego';
if (drawing) {
self.circleLayer.removeLayer(marker);
self.setNogoCircle(undefined);
self.setNogoRing(undefined);
}
});
}));
this.clear();
marker.addTo(this.circleLayer);
if (!skipNogo) {
this.setNogoCircle(center);
if (this.options.stateRules && !this.states) {
// prevent editing (when called by hash) until states are loaded, see _loadStates call in onAdd
marker.dragging.disable();
}
if (!polylines) {
this.setNogoRing(center);
} else {
var polygon = this.circleToPolygon(center, this.options.radius);
this.setOutsideArea(polygon);
var features = [];
for (var i = 0; i < polylines.length; i++) {
features.push(polylines[i].toGeoJSON());
}
var ring = turf.featureCollection(features);
this.setOutsideArea(ring);
}
this.draw(false);
},
_clearLayers: function () {
if (this.outsideArea) {
this.map.removeLayer(this.outsideArea);
this.outsideArea = null;
}
if (this.boundaryLayer) {
this.map.removeLayer(this.boundaryLayer);
this.boundaryLayer = null;
}
},
clear: function () {
this.circleLayer.clearLayers();
this._clearLayers();
},
getButton: function () {
@ -210,7 +500,7 @@ BR.CircleGoArea = L.Control.extend({
},
circleToPolygon: function (center, radius, numberOfSegments) {
var n = numberOfSegments ? numberOfSegments : 32;
var n = numberOfSegments ? numberOfSegments : 64;
var inner = [];
for (var i = 0; i < n; ++i) {
@ -229,7 +519,7 @@ BR.CircleGoArea = L.Control.extend({
properties: {},
geometry: {
type: 'LineString',
coordinates: inner.slice(n / 2 - 1),
coordinates: inner.slice(n / 2),
},
},
{
@ -246,3 +536,21 @@ BR.CircleGoArea = L.Control.extend({
});
BR.CircleGoArea.include(L.Evented.prototype);
BR.circleGoArea = function (routing, nogos, pois) {
var circleGo = null;
var options = {};
if (BR.Util.isCountry('FR', 'fr')) {
options.radius = 20000;
} else if (BR.Util.isCountry('DE', 'de')) {
options.radius = 15000;
options.stateRules = true;
}
if (options) {
circleGo = new BR.CircleGoArea(routing, nogos, pois, options);
}
return circleGo;
};

View file

@ -35,6 +35,10 @@ BR.NogoAreas = L.Control.extend({
fillOpacity: 0.1,
},
polylineOptions: {
smoothFactor: 0.5,
},
initialize: function () {
this._wasRouteDrawing = false;
},
@ -233,6 +237,9 @@ BR.NogoAreas = L.Control.extend({
var geoJSON = L.geoJson(turf.featureCollection(cleanedGeoJSONFeatures), {
onEachFeature: function (feature, layer) {
layer.options.nogoWeight = feature.properties.nogoWeight || nogoWeight;
if (feature.geometry.type === 'LineString') {
L.setOptions(layer, self.polylineOptions);
}
},
});
var nogosPoints = geoJSON.getLayers().filter(function (e) {
@ -308,6 +315,15 @@ BR.NogoAreas = L.Control.extend({
var polylines = options.polylines;
var polygons = options.polygons;
this._clear();
this._addNogos(nogos, polylines, polygons);
},
addNogos: function (nogos, polylines, polygons) {
this._addNogos(nogos, polylines, polygons);
this._fireUpdate();
},
_addNogos: function (nogos, polylines, polygons) {
if (nogos) {
for (var i = 0; i < nogos.length; i++) {
nogos[i].setStyle(this.style);
@ -328,6 +344,26 @@ BR.NogoAreas = L.Control.extend({
}
},
removeNogos: function (nogos, polylines, polygons) {
if (nogos) {
for (var i = 0; i < nogos.length; i++) {
this.drawnItems.removeLayer(nogos[i]);
}
}
if (polylines) {
for (var i = 0; i < polylines.length; i++) {
this.drawnItems.removeLayer(polylines[i]);
}
}
if (polygons) {
for (var i = 0; i < polygons.length; i++) {
this.drawnItems.removeLayer(polygons[i]);
}
}
this._fireUpdate();
},
_clear: function () {
this.drawnItems.clearLayers();
},

View file

@ -102,8 +102,9 @@ L.BRouter = L.Class.extend({
if (params.pois) {
opts.pois = this._parseLonLatNames(params.pois);
}
if (params.circlego) {
var circlego = params.circlego.split(',');
if (params.ringgo || params.circlego) {
var paramRinggo = params.ringgo || params.circlego;
var circlego = paramRinggo.split(',');
if (circlego.length == 3) {
circlego = [
Number.parseFloat(circlego[0]),
@ -122,7 +123,7 @@ L.BRouter = L.Class.extend({
if (urlParams.lonlats != null && urlParams.lonlats.length > 0)
args.push(L.Util.template('lonlats={lonlats}', urlParams));
if (urlParams.pois != null && urlParams.pois.length > 0) args.push(L.Util.template('pois={pois}', urlParams));
if (urlParams.circlego != null) args.push(L.Util.template('circlego={circlego}', urlParams));
if (urlParams.circlego != null) args.push(L.Util.template('ringgo={circlego}', urlParams));
if (urlParams.nogos != null) args.push(L.Util.template('nogos={nogos}', urlParams));
if (urlParams.polylines != null) args.push(L.Util.template('polylines={polylines}', urlParams));
if (urlParams.polygons != null) args.push(L.Util.template('polygons={polygons}', urlParams));
@ -433,7 +434,8 @@ L.BRouter = L.Class.extend({
if (j < numbers.length) {
nogoWeight = Number.parseFloat(numbers[j++]);
}
nogos.push(L.polyline(latlngs, { nogoWeight: nogoWeight }));
var options = L.extend(BR.NogoAreas.prototype.polylineOptions, { nogoWeight: nogoWeight });
nogos.push(L.polyline(latlngs, options));
}
}
return nogos;