iteration test for tolerance value

This commit is contained in:
The Ripper 2020-07-07 16:44:18 +02:00
parent fb39962100
commit 3f7e0fa66a

View file

@ -8,6 +8,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
_maxTrackPoints: 200, _maxTrackPoints: 200,
_closeCanceled: true, _closeCanceled: true,
_currentGeoJSON: {}, _currentGeoJSON: {},
_counter: 0,
_options: { _options: {
format: undefined, format: undefined,
showTrackLayer: true, showTrackLayer: true,
@ -37,20 +38,22 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
}); });
}, },
getSimplifiedLatLngs: function() { getSimplifiedCoords: function(tolerance) {
var simplifiedLine = turf.simplify(this._trackPoints.geometry, { var simplifiedLine = turf.simplify(this._trackPoints.geometry, {
tolerance: this._options.simplifyTolerance, tolerance: tolerance,
highQuality: true highQuality: true
}); });
return simplifiedLine.coordinates;
return L.GeoJSON.coordsToLatLngs(simplifiedLine.coordinates); // return L.GeoJSON.coordsToLatLngs(simplifiedLine.coordinates);
}, },
refreshTestLayer: function() { refreshTestLayer: function() {
this.onBusyChanged(true); this.onBusyChanged(true);
this._testLayer.clearLayers(); this._testLayer.clearLayers();
var simplifiedLatLngs = this.getSimplifiedLatLngs(); var simplifiedLatLngs = L.GeoJSON.coordsToLatLngs(
this.getSimplifiedCoords(this._options.simplifyTolerance)
);
if (simplifiedLatLngs.length > this._maxTrackPoints) { if (simplifiedLatLngs.length > this._maxTrackPoints) {
this.onBusyChanged(false); this.onBusyChanged(false);
return false; return false;
@ -94,6 +97,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
setSliderRange: function() { setSliderRange: function() {
$slider = $('#simplify_tolerance'); $slider = $('#simplify_tolerance');
$slider.prop('min', -500);
var guessedTolerance = this.guessSimplifyTolerance(this._trackPoints); var guessedTolerance = this.guessSimplifyTolerance(this._trackPoints);
$slider.data('guess', guessedTolerance.toFixed(20)); $slider.data('guess', guessedTolerance.toFixed(20));
$slider.val(0); $slider.val(0);
@ -111,8 +115,18 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
else this._options.simplifyTolerance = Math.abs(guess + Math.pow(f, 3) * (guess / Math.pow(frac, 3))); else this._options.simplifyTolerance = Math.abs(guess + Math.pow(f, 3) * (guess / Math.pow(frac, 3)));
if (!this.refreshTestLayer()) { if (!this.refreshTestLayer()) {
var iterate = this.findLowestTolerance(
parseInt(e.target.value),
parseInt($(e.target).data('lastknowngood')),
guess,
frac
);
$(e.target).prop('min', iterate);
$(e.target).data('lastknowngood', iterate);
e.target.value = $(e.target).data('lastknowngood'); e.target.value = $(e.target).data('lastknowngood');
this._options.simplifyTolerance = this._options.simplifyLastKnownGood; this._options.simplifyTolerance = this._options.simplifyLastKnownGood = Math.abs(
guess + Math.pow(iterate, 3) * (guess / Math.pow(frac, 3))
);
this.refreshTestLayer(); this.refreshTestLayer();
} else { } else {
this._options.simplifyLastKnownGood = this._options.simplifyTolerance; this._options.simplifyLastKnownGood = this._options.simplifyTolerance;
@ -120,6 +134,18 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
} }
}, },
findLowestTolerance: function(min, max, guess, frac) {
if (Math.abs(max - min) <= 2) return max;
var meridian = Math.round((max + min) / 2);
var tolerance = Math.abs(guess + Math.pow(meridian, 3) * (guess / Math.pow(frac, 3)));
var simplifiedCoords = this.getSimplifiedCoords(tolerance);
if (simplifiedCoords.length > this._maxTrackPoints)
return this.findLowestTolerance(meridian, max, guess, frac);
else return this.findLowestTolerance(min, meridian, guess, frac);
},
onBusyChanged: function(isBusy) { onBusyChanged: function(isBusy) {
if (typeof isBusy === undefined) { if (typeof isBusy === undefined) {
isBusy = false; isBusy = false;
@ -177,7 +203,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
); );
}, this); }, this);
L.DomUtil.get('simplify_tolerance').onchange = L.bind(this.onToleranceSlider, this); L.DomUtil.get('simplify_tolerance').oninput = L.bind(this.onToleranceSlider, this);
L.DomUtil.get('loadedittrackFile').onchange = L.bind(this.onFileChanged, this); L.DomUtil.get('loadedittrackFile').onchange = L.bind(this.onFileChanged, this);
this.onFileChanged({ target: L.DomUtil.get('loadedittrackFile') }); this.onFileChanged({ target: L.DomUtil.get('loadedittrackFile') });
@ -294,7 +320,9 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
this._options.simplifyTolerance = this.guessSimplifyTolerance(this._trackPoints); this._options.simplifyTolerance = this.guessSimplifyTolerance(this._trackPoints);
var routingPoints = []; var routingPoints = [];
var simplifiedLatLngs = this.getSimplifiedLatLngs(); var simplifiedLatLngs = L.GeoJSON.coordsToLatLngs(
this.getSimplifiedCoords(this._options.simplifyTolerance)
);
for (var i = 0; i < simplifiedLatLngs.length; i++) { for (var i = 0; i < simplifiedLatLngs.length; i++) {
routingPoints.push(simplifiedLatLngs[i]); routingPoints.push(simplifiedLatLngs[i]);