Let user upload Nogos through GeoJSON file.
This commit is contained in:
parent
1f81c043ad
commit
e7db4a7aae
5 changed files with 1518 additions and 19 deletions
96
js/index.js
96
js/index.js
|
|
@ -356,6 +356,102 @@
|
|||
$(this).collapse('show');
|
||||
}
|
||||
});
|
||||
|
||||
$('#submitNogos').on('click', function () {
|
||||
var geoJSONPromise;
|
||||
var nogoURL = $('#nogoURL').val();
|
||||
var nogoFile = $('#nogoFile')[0].files[0];
|
||||
if (nogoURL) {
|
||||
// TODO: Handle {{bbox}}
|
||||
geoJSONPromise = fetch(nogoURL).then(function (response) {
|
||||
response.json();
|
||||
});
|
||||
} else if (nogoFile) {
|
||||
geoJSONPromise = new Promise(function (resolve, reject) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function () {
|
||||
resolve(reader.result);
|
||||
}
|
||||
reader.readAsText(nogoFile);
|
||||
}).then(function (response) { return JSON.parse(response); });
|
||||
}
|
||||
else {
|
||||
$('#nogoError').text('Error: Missing file or URL.');
|
||||
$('#nogoError').css('display', 'block');
|
||||
return false;
|
||||
}
|
||||
var nogoWeight = parseFloat($('#nogoWeight').val());
|
||||
if (isNaN(nogoWeight)) {
|
||||
$('#nogoError').text('Error: Missing default nogo weight.');
|
||||
$('#nogoError').css('display', 'block');
|
||||
return false;
|
||||
}
|
||||
var nogoRadius = parseFloat($('#nogoRadius').val());
|
||||
if (isNaN(nogoRadius) || nogoRadius < 0) {
|
||||
$('#nogoError').text('Error: Invalid default nogo radius.');
|
||||
$('#nogoError').css('display', 'block');
|
||||
return false;
|
||||
}
|
||||
var nogoBuffer = parseFloat($('#nogoBuffer').val());
|
||||
if (isNaN(nogoBuffer)) {
|
||||
$('#nogoError').text('Error: Invalid nogo buffering radius.');
|
||||
$('#nogoError').css('display', 'block');
|
||||
return false;
|
||||
}
|
||||
|
||||
geoJSONPromise.then(function (response) {
|
||||
// Iterate on features in order to discard features without geometry
|
||||
var cleanedGeoJSONFeatures = []
|
||||
turf.featureEach(response, function (feature) {
|
||||
if (turf.getGeom(feature)) {
|
||||
var maybeBufferedFeature = feature;
|
||||
// Eventually buffer GeoJSON
|
||||
if (nogoBuffer != 0) {
|
||||
maybeBufferedFeature = turf.buffer(
|
||||
maybeBufferedFeature, nogoBuffer, { units: 'meters' }
|
||||
);
|
||||
}
|
||||
cleanedGeoJSONFeatures.push(maybeBufferedFeature);
|
||||
}
|
||||
});
|
||||
|
||||
var geoJSON = L.geoJson(turf.featureCollection(cleanedGeoJSONFeatures), {
|
||||
onEachFeature: function (feature, layer) {
|
||||
if (!feature.properties.nogoWeight) {
|
||||
feature.properties.nogoWeight = nogoWeight;
|
||||
}
|
||||
}
|
||||
});
|
||||
var nogosPoints = geoJSON.getLayers().filter(function (e) {
|
||||
return e.feature.geometry.type === 'Point';
|
||||
});
|
||||
nogosPoints = nogosPoints.map(function (item) {
|
||||
var radius = item.feature.properties.radius || nogoRadius;
|
||||
if (radius > 0) {
|
||||
return L.circle(item.getLatLng(), { radius: radius });
|
||||
}
|
||||
return null;
|
||||
});
|
||||
nogosPoints = nogosPoints.filter(function (e) { return e; });
|
||||
nogos.setOptions({
|
||||
nogos: nogosPoints,
|
||||
polygons: geoJSON.getLayers().filter(function (e) {
|
||||
return e.feature.geometry.type === 'Polygon';
|
||||
}),
|
||||
polylines: geoJSON.getLayers().filter(function (e) {
|
||||
return e.feature.geometry.type === 'LineString';
|
||||
}),
|
||||
});
|
||||
updateRoute({
|
||||
options: nogos.getOptions()
|
||||
});
|
||||
urlHash.onMapMove();
|
||||
$('#nogoError').text('');
|
||||
$('#nogoError').css('display', 'none');
|
||||
$('#loadNogos').modal('hide');
|
||||
});
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
i18next
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue