Extract nogo areas upload in BR.NogoAreas
This commit is contained in:
parent
5a03b43099
commit
ba12f0d609
2 changed files with 117 additions and 118 deletions
118
js/index.js
118
js/index.js
|
|
@ -409,124 +409,6 @@
|
||||||
$(this).collapse('show');
|
$(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.onerror = function() {
|
|
||||||
$('#nogoError').text(
|
|
||||||
'Cannot read file: ' + reader.error.message
|
|
||||||
);
|
|
||||||
$('#nogoError').css('display', 'block');
|
|
||||||
};
|
|
||||||
|
|
||||||
reader.readAsText(nogoFile);
|
|
||||||
}).then(function(response) {
|
|
||||||
return JSON.parse(response);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
$('#nogoError').text('Missing file or URL.');
|
|
||||||
$('#nogoError').css('display', 'block');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var nogoWeight = parseFloat($('#nogoWeight').val());
|
|
||||||
if (isNaN(nogoWeight)) {
|
|
||||||
$('#nogoError').text('Missing default nogo weight.');
|
|
||||||
$('#nogoError').css('display', 'block');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var nogoRadius = parseFloat($('#nogoRadius').val());
|
|
||||||
if (isNaN(nogoRadius) || nogoRadius < 0) {
|
|
||||||
$('#nogoError').text('Invalid default nogo radius.');
|
|
||||||
$('#nogoError').css('display', 'block');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
var nogoBuffer = parseFloat($('#nogoBuffer').val());
|
|
||||||
if (isNaN(nogoBuffer)) {
|
|
||||||
$('#nogoError').text('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.flattenEach(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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (cleanedGeoJSONFeatures.length === 0) {
|
|
||||||
$('#nogoError').text(
|
|
||||||
'No valid area found in provided input.'
|
|
||||||
);
|
|
||||||
$('#nogoError').css('display', 'block');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
var geoJSON = L.geoJson(
|
|
||||||
turf.featureCollection(cleanedGeoJSONFeatures),
|
|
||||||
{
|
|
||||||
onEachFeature: function(feature, layer) {
|
|
||||||
layer.options.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
|
i18next
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,8 @@ BR.NogoAreas = L.Control.extend({
|
||||||
onAdd: function(map) {
|
onAdd: function(map) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
|
$('#submitNogos').on('click', L.bind(this.uploadNogos, this));
|
||||||
|
|
||||||
this.drawnItems = new L.FeatureGroup().addTo(map);
|
this.drawnItems = new L.FeatureGroup().addTo(map);
|
||||||
this.drawnItems.on('click', function(e) {
|
this.drawnItems.on('click', function(e) {
|
||||||
L.DomEvent.stop(e);
|
L.DomEvent.stop(e);
|
||||||
|
|
@ -118,6 +120,121 @@ BR.NogoAreas = L.Control.extend({
|
||||||
return L.DomUtil.create('div');
|
return L.DomUtil.create('div');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
uploadNogos: function() {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
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.onerror = function() {
|
||||||
|
$('#nogoError').text(
|
||||||
|
'Could not load file: ' + reader.error.message
|
||||||
|
);
|
||||||
|
$('#nogoError').css('display', 'block');
|
||||||
|
};
|
||||||
|
|
||||||
|
reader.readAsText(nogoFile);
|
||||||
|
}).then(function(response) {
|
||||||
|
return JSON.parse(response);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$('#nogoError').text('Missing file or URL.');
|
||||||
|
$('#nogoError').css('display', 'block');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var nogoWeight = parseFloat($('#nogoWeight').val());
|
||||||
|
if (isNaN(nogoWeight)) {
|
||||||
|
$('#nogoError').text('Missing default nogo weight.');
|
||||||
|
$('#nogoError').css('display', 'block');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var nogoRadius = parseFloat($('#nogoRadius').val());
|
||||||
|
if (isNaN(nogoRadius) || nogoRadius < 0) {
|
||||||
|
$('#nogoError').text('Invalid default nogo radius.');
|
||||||
|
$('#nogoError').css('display', 'block');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var nogoBuffer = parseFloat($('#nogoBuffer').val());
|
||||||
|
if (isNaN(nogoBuffer)) {
|
||||||
|
$('#nogoError').text('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.flattenEach(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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (cleanedGeoJSONFeatures.length === 0) {
|
||||||
|
$('#nogoError').text('No valid area found in provided input.');
|
||||||
|
$('#nogoError').css('display', 'block');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var geoJSON = L.geoJson(
|
||||||
|
turf.featureCollection(cleanedGeoJSONFeatures),
|
||||||
|
{
|
||||||
|
onEachFeature: function(feature, layer) {
|
||||||
|
layer.options.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;
|
||||||
|
});
|
||||||
|
self.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';
|
||||||
|
})
|
||||||
|
});
|
||||||
|
self._fireUpdate();
|
||||||
|
$('#nogoError').text('');
|
||||||
|
$('#nogoError').css('display', 'none');
|
||||||
|
$('#loadNogos').modal('hide');
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
// prevent route waypoint added after circle create (map click after up)
|
// prevent route waypoint added after circle create (map click after up)
|
||||||
preventRoutePointOnCreate: function(routing) {
|
preventRoutePointOnCreate: function(routing) {
|
||||||
this.editTools.on(
|
this.editTools.on(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue