From 7e1392bb60fbb6aa840e75276a71efda7a58eab8 Mon Sep 17 00:00:00 2001 From: JustineFricou Date: Wed, 24 Apr 2024 18:19:05 +0200 Subject: [PATCH] Commenting code that is no longer useful --- .../static/core/geotrek.forms.topology.js | 21 - geotrek/core/static/core/multipath.js | 74 +-- geotrek/core/static/core/topology_helper.js | 572 +++++++++--------- 3 files changed, 316 insertions(+), 351 deletions(-) diff --git a/geotrek/core/static/core/geotrek.forms.topology.js b/geotrek/core/static/core/geotrek.forms.topology.js index 2150efdc01..f60262c8f9 100644 --- a/geotrek/core/static/core/geotrek.forms.topology.js +++ b/geotrek/core/static/core/geotrek.forms.topology.js @@ -123,27 +123,6 @@ MapEntity.GeometryField.TopologyField = MapEntity.GeometryField.extend({ this.load(); return; } - - // Path layer is ready, load graph ! - this._pathsLayer.fire('data:loading'); - var url = window.SETTINGS.urls.path_graph; - $.getJSON(url, this._onGraphLoaded.bind(this)) - .error(graphError.bind(this)); - - function graphError(jqXHR, textStatus, errorThrown) { - this._pathsLayer.fire('data:loaded'); - $(this._map._container).addClass('map-error'); - console.error("Could not load url '" + window.SETTINGS.urls.path_graph + "': " + textStatus); - console.error(errorThrown); - } - }, - - _onGraphLoaded: function (graph) { - // Load graph - this._lineControl.setGraph(graph); - this.load(); - // Stop spinning ! - this._pathsLayer.fire('data:loaded'); }, load: function () { diff --git a/geotrek/core/static/core/multipath.js b/geotrek/core/static/core/multipath.js index 0ed80c6826..0ba54f1bc3 100644 --- a/geotrek/core/static/core/multipath.js +++ b/geotrek/core/static/core/multipath.js @@ -170,18 +170,11 @@ L.Control.LineTopology = L.Control.extend({ }, initialize: function (map, guidesLayer, field, options) { + console.log("guidesLayer", guidesLayer) L.Control.prototype.initialize.call(this, options); this.handler = new L.Handler.MultiPath(map, guidesLayer, options); }, - setGraph: function (graph) { - /** - * Set the Dikjstra graph - */ - this.handler.setGraph(graph); - this.activable(true); - }, - onAdd: function (map) { this._container = L.DomUtil.create('div', 'leaflet-draw leaflet-control leaflet-bar leaflet-control-zoom'); var link = L.DomUtil.create('a', 'leaflet-control-zoom-out linetopology-control', this._container); @@ -192,7 +185,7 @@ L.Control.LineTopology = L.Control.extend({ .addListener(link, 'click', L.DomEvent.preventDefault) .addListener(link, 'click', this.toggle, this); - // Control is not activable until paths and graph are loaded + // Control is not activable until paths are loaded this.activable(false); return this._container; @@ -243,8 +236,6 @@ L.Handler.MultiPath = L.Handler.extend({ this._guidesLayer = guidesLayer; this.options = options; - this.graph = null; - // markers this.markersFactory = this.getMarkers(); @@ -273,6 +264,7 @@ L.Handler.MultiPath = L.Handler.extend({ var self = this; (function() { function dragstart(e) { + //var next_step_idx = self.draggable_marker.group_layer.step_idx + 1; self.addViaStep(self.draggable_marker, next_step_idx); } @@ -295,10 +287,6 @@ L.Handler.MultiPath = L.Handler.extend({ this.on('fetched_route', this.onFetchedRoute, this); }, - setGraph: function (graph) { - this.graph = graph; - }, - setState: function(state, autocompute) { autocompute = autocompute === undefined ? true : autocompute; var self = this; @@ -336,8 +324,6 @@ L.Handler.MultiPath = L.Handler.extend({ // reset state this.steps = []; - this.computed_paths = []; - this.all_edges = []; this.marker_source = this.marker_dest = null; }, @@ -376,7 +362,7 @@ L.Handler.MultiPath = L.Handler.extend({ }, - // On click on a layer with the graph + // On click on a layer _onClick: function(e) { if (this.steps.length >= 2) return; var self = this; @@ -477,13 +463,13 @@ L.Handler.MultiPath = L.Handler.extend({ return this.steps.indexOf(step); }, - getStepIdxFromMarker: function(marker) { - for (var i = 0; i < this.steps.length; i++) { - if (this.steps[i].marker === marker) - return i; - } - return -1; - }, + // getStepIdxFromMarker: function(marker) { + // for (var i = 0; i < this.steps.length; i++) { + // if (this.steps[i].marker === marker) + // return i; + // } + // return -1; + // }, getCookie: function(name) { @@ -768,24 +754,24 @@ L.Handler.MultiPath = L.Handler.extend({ } } - /* var MIN_DIST = 30; - - var layerPoint = a.layerPoint - , min_dist = Number.MAX_VALUE - , closest_point = null - , matching_group_layer = null; - - topology && topology.eachLayer(function(group_layer) { - console.log(group_layer) - group_layer.eachLayer(function(layer) { - var p = layer.closestLayerPoint(layerPoint); - if (p && p.distance < min_dist && p.distance < MIN_DIST) { - min_dist = p.distance; - closest_point = p; - matching_group_layer = group_layer; - } - }); - }); */ + // var MIN_DIST = 30; + + // var layerPoint = a.layerPoint + // , min_dist = Number.MAX_VALUE + // , closest_point = null + // , matching_group_layer = null; + + // topology && topology.eachLayer(function(group_layer) { + // console.log(group_layer) + // group_layer.eachLayer(function(layer) { + // var p = layer.closestLayerPoint(layerPoint); + // if (p && p.distance < min_dist && p.distance < MIN_DIST) { + // min_dist = p.distance; + // closest_point = p; + // matching_group_layer = group_layer; + // } + // }); + // }); if (this.pathLayer) { var closest_point = this.pathLayer.closestLayerPoint(a.layerPoint); @@ -861,6 +847,6 @@ Geotrek.PointOnPolyline.prototype.toggleActivate = function(activate) { marker[method]('unsnap', markerEvents.unsnap, this); }; -Geotrek.PointOnPolyline.prototype.isValid = function(graph) { +Geotrek.PointOnPolyline.prototype.isValid = function() { return (this.ll && this.polyline); }; diff --git a/geotrek/core/static/core/topology_helper.js b/geotrek/core/static/core/topology_helper.js index b1b23e01b7..42e2196f01 100644 --- a/geotrek/core/static/core/topology_helper.js +++ b/geotrek/core/static/core/topology_helper.js @@ -1,311 +1,311 @@ -var Geotrek = Geotrek || {}; +// var Geotrek = Geotrek || {}; -Geotrek.TopologyHelper = (function() { +// Geotrek.TopologyHelper = (function() { - /** - * This static function takes a list of Dijkstra results, and returns - * a serialized topology, as expected by form widget, as well as a - * multiline geometry for highlight the result. - */ - function buildSubTopology(paths, polylines, ll_start, ll_end, offset) { - var polyline_start = polylines[0] - , polyline_end = polylines[polylines.length - 1] - , single_path = paths.length == 1 - , cleanup = true - , positions = {}; +// /** +// * This static function takes a list of Dijkstra results, and returns +// * a serialized topology, as expected by form widget, as well as a +// * multiline geometry for highlight the result. +// */ +// function buildSubTopology(paths, polylines, ll_start, ll_end, offset) { +// var polyline_start = polylines[0] +// , polyline_end = polylines[polylines.length - 1] +// , single_path = paths.length == 1 +// , cleanup = true +// , positions = {}; - if (!polyline_start || !polyline_end) { - console.error("Could not compute distances without polylines."); - return null; // TODO: clean-up before give-up ? - } +// if (!polyline_start || !polyline_end) { +// console.error("Could not compute distances without polylines."); +// return null; // TODO: clean-up before give-up ? +// } - var pk_start = L.GeometryUtil.locateOnLine(polyline_start._map, polyline_start, ll_start), - pk_end = L.GeometryUtil.locateOnLine(polyline_end._map, polyline_end, ll_end); - console.debug('Start on layer ' + polyline_start.properties.id + ' ' + pk_start + ' ' + ll_start.toString()); - console.debug('End on layer ' + polyline_end.properties.id + ' ' + pk_end + ' ' + ll_end.toString()); +// var pk_start = L.GeometryUtil.locateOnLine(polyline_start._map, polyline_start, ll_start), +// pk_end = L.GeometryUtil.locateOnLine(polyline_end._map, polyline_end, ll_end); +// console.debug('Start on layer ' + polyline_start.properties.id + ' ' + pk_start + ' ' + ll_start.toString()); +// console.debug('End on layer ' + polyline_end.properties.id + ' ' + pk_end + ' ' + ll_end.toString()); - if (single_path) { - var path_pk = paths[0], - lls = polyline_start.getLatLngs(); +// if (single_path) { +// var path_pk = paths[0], +// lls = polyline_start.getLatLngs(); - single_path_loop = lls[0].equals(lls[lls.length-1]); - if (single_path_loop) - cleanup = false; +// single_path_loop = lls[0].equals(lls[lls.length-1]); +// if (single_path_loop) +// cleanup = false; - if (single_path_loop && Math.abs(pk_end - pk_start) > 0.5) { - /* - * A - * //=|---+ - * +// | It is shorter to go through - * \\ | extremeties than the whole loop - * \\=|---+ - * B - */ - if (pk_end - pk_start > 0.5) { - paths = [path_pk, path_pk]; - positions[0] = [pk_start, 0.0]; - positions[1] = [1.0, pk_end]; - } - else if (pk_end - pk_start < -0.5) { - paths = [path_pk, path_pk]; - positions[0] = [pk_end, 0.0]; - positions[1] = [1.0, pk_start]; - } - } - else { - /* A B - * +----|=====|----> - * - * B A - * +----|=====|----> - */ - paths = $.unique(paths); - positions[0] = [pk_start, pk_end]; - } - } - else if (paths.length == 3 && polyline_start == polyline_end) { - var start_lls = polylines[0].getLatLngs() - , mid_lls = polylines[1].getLatLngs(); - cleanup = false; - if (pk_start < pk_end) { - positions[0] = [pk_start, 0.0]; - if (start_lls[0].equals(mid_lls[0])) - positions[1] = [0.0, 1.0]; - else - positions[1] = [1.0, 0.0]; - positions[2] = [1.0, pk_end]; - } - else { - positions[0] = [pk_start, 1.0]; - if (start_lls[0].equals(mid_lls[0])) - positions[1] = [1.0, 0.0]; - else - positions[1] = [0.0, 1.0]; - positions[2] = [0.0, pk_end]; - } - } - else { - /* - * Add first portion of line - */ - var start_lls = polyline_start.getLatLngs(), - first_end = start_lls[start_lls.length-1], - start_on_loop = start_lls[0].equals(first_end); +// if (single_path_loop && Math.abs(pk_end - pk_start) > 0.5) { +// /* +// * A +// * //=|---+ +// * +// | It is shorter to go through +// * \\ | extremeties than the whole loop +// * \\=|---+ +// * B +// */ +// if (pk_end - pk_start > 0.5) { +// paths = [path_pk, path_pk]; +// positions[0] = [pk_start, 0.0]; +// positions[1] = [1.0, pk_end]; +// } +// else if (pk_end - pk_start < -0.5) { +// paths = [path_pk, path_pk]; +// positions[0] = [pk_end, 0.0]; +// positions[1] = [1.0, pk_start]; +// } +// } +// else { +// /* A B +// * +----|=====|----> +// * +// * B A +// * +----|=====|----> +// */ +// paths = $.unique(paths); +// positions[0] = [pk_start, pk_end]; +// } +// } +// else if (paths.length == 3 && polyline_start == polyline_end) { +// var start_lls = polylines[0].getLatLngs() +// , mid_lls = polylines[1].getLatLngs(); +// cleanup = false; +// if (pk_start < pk_end) { +// positions[0] = [pk_start, 0.0]; +// if (start_lls[0].equals(mid_lls[0])) +// positions[1] = [0.0, 1.0]; +// else +// positions[1] = [1.0, 0.0]; +// positions[2] = [1.0, pk_end]; +// } +// else { +// positions[0] = [pk_start, 1.0]; +// if (start_lls[0].equals(mid_lls[0])) +// positions[1] = [1.0, 0.0]; +// else +// positions[1] = [0.0, 1.0]; +// positions[2] = [0.0, pk_end]; +// } +// } +// else { +// /* +// * Add first portion of line +// */ +// var start_lls = polyline_start.getLatLngs(), +// first_end = start_lls[start_lls.length-1], +// start_on_loop = start_lls[0].equals(first_end); - if (L.GeometryUtil.startsAtExtremity(polyline_start, polylines[1])) { - var next_lls = polylines[1].getLatLngs(), - next_end = next_lls[next_lls.length-1], - share_end = first_end.equals(next_end), - two_paths_loop = first_end.equals(next_lls[0]); - if ((start_on_loop && pk_start > 0.5) || - (share_end && (pk_start + pk_end) >= 1) || - (two_paths_loop && (pk_start - pk_end) > 0)) { - /* - * A - * /--|===+ B - * +/ \\+==|--- - * \ / - * \-----+ - * - * A B - * +----|------><-------|---- - * - * +----|=====|><=======|---- - * - */ - positions[0] = [pk_start, 1.0]; - } - else { - /* - * A B - * <----|------++-------|---- - * - * <----|=====|++=======|---- - * - */ - positions[0] = [pk_start, 0.0]; - } - } else { - /* - * A B - * +----|------>+-------|---- - * - * +----|=====|>+=======|---- - * - */ - positions[0] = [pk_start, 1.0]; - } +// if (L.GeometryUtil.startsAtExtremity(polyline_start, polylines[1])) { +// var next_lls = polylines[1].getLatLngs(), +// next_end = next_lls[next_lls.length-1], +// share_end = first_end.equals(next_end), +// two_paths_loop = first_end.equals(next_lls[0]); +// if ((start_on_loop && pk_start > 0.5) || +// (share_end && (pk_start + pk_end) >= 1) || +// (two_paths_loop && (pk_start - pk_end) > 0)) { +// /* +// * A +// * /--|===+ B +// * +/ \\+==|--- +// * \ / +// * \-----+ +// * +// * A B +// * +----|------><-------|---- +// * +// * +----|=====|><=======|---- +// * +// */ +// positions[0] = [pk_start, 1.0]; +// } +// else { +// /* +// * A B +// * <----|------++-------|---- +// * +// * <----|=====|++=======|---- +// * +// */ +// positions[0] = [pk_start, 0.0]; +// } +// } else { +// /* +// * A B +// * +----|------>+-------|---- +// * +// * +----|=====|>+=======|---- +// * +// */ +// positions[0] = [pk_start, 1.0]; +// } - /* - * Add all intermediary lines - */ - for (var i=1; i 0.5) || - (share_end && (pk_start + pk_end) >= 1) || - (two_paths_loop && (pk_start - pk_end) <= 0)) { - /* - * B - * A //==|-+ - * ---|==+// | - * \ | - * \-----+ - * - * A B - * -----|------><-------|----+ - * - * -----|======>|+======>----> - */ - positions[polylines.length - 1] = [1.0, pk_end]; - } - else { - /* - * A B - * -----|------++-------|----> - * - * -----|======+|=======>----> - */ - positions[polylines.length - 1] = [0.0, pk_end]; - } - } else { - /* - * A B - * -----|------+<-------|----+ - * - * -----|=====|+<=======|----+ - */ - positions[polylines.length - 1] = [1.0, pk_end]; - } - } +// if (L.GeometryUtil.startsAtExtremity(polyline_end, polylines[polylines.length - 2])) { +// var previous_lls = polylines[polylines.length - 2].getLatLngs(), +// previous_end = previous_lls[previous_lls.length-1], +// share_end = last_end.equals(previous_end), +// two_paths_loop = last_end.equals(previous_lls[0]); +// if ((end_on_loop && pk_end > 0.5) || +// (share_end && (pk_start + pk_end) >= 1) || +// (two_paths_loop && (pk_start - pk_end) <= 0)) { +// /* +// * B +// * A //==|-+ +// * ---|==+// | +// * \ | +// * \-----+ +// * +// * A B +// * -----|------><-------|----+ +// * +// * -----|======>|+======>----> +// */ +// positions[polylines.length - 1] = [1.0, pk_end]; +// } +// else { +// /* +// * A B +// * -----|------++-------|----> +// * +// * -----|======+|=======>----> +// */ +// positions[polylines.length - 1] = [0.0, pk_end]; +// } +// } else { +// /* +// * A B +// * -----|------+<-------|----+ +// * +// * -----|=====|+<=======|----+ +// */ +// positions[polylines.length - 1] = [1.0, pk_end]; +// } +// } - // Clean-up : - // We basically remove all points where position is [x,x] - // This can happen at extremity points... +// // Clean-up : +// // We basically remove all points where position is [x,x] +// // This can happen at extremity points... - if (cleanup) { - var cleanpaths = [], - cleanpositions = {}; - for (var i=0; i < paths.length; i++) { - var path = paths[i]; - if (i in positions) { - if (positions[i][0] != positions[i][1] && cleanpaths.indexOf(path) == -1) { - cleanpaths.push(path); - cleanpositions[i] = positions[i]; - } - } - else { - cleanpaths.push(path); - } - } - paths = cleanpaths; - positions = cleanpositions; - } +// if (cleanup) { +// var cleanpaths = [], +// cleanpositions = {}; +// for (var i=0; i < paths.length; i++) { +// var path = paths[i]; +// if (i in positions) { +// if (positions[i][0] != positions[i][1] && cleanpaths.indexOf(path) == -1) { +// cleanpaths.push(path); +// cleanpositions[i] = positions[i]; +// } +// } +// else { +// cleanpaths.push(path); +// } +// } +// paths = cleanpaths; +// positions = cleanpositions; +// } - // Safety warning. - if (paths.length === 0) - console.error('Empty topology. Expect problems. (' + JSON.stringify({positions:positions, paths:paths}) + ')'); +// // Safety warning. +// if (paths.length === 0) +// console.error('Empty topology. Expect problems. (' + JSON.stringify({positions:positions, paths:paths}) + ')'); - return { - offset: offset, // Float for offset - positions: positions, // Positions on paths - paths: paths // List of pks - }; - } +// return { +// offset: offset, // Float for offset +// positions: positions, // Positions on paths +// paths: paths // List of pks +// }; +// } - /** - * @param topology {Object} with ``offset``, ``positions`` and ``paths`` as returned by buildSubTopology() - * @param idToLayer {function} callback to obtain layer from id - * @returns L.multiPolyline - */ - function buildGeometryFromTopology(topology, idToLayer) { - var latlngs = []; - for (var i=0; i < topology.paths.length; i++) { - var path = topology.paths[i], - positions = topology.positions[i], - polyline = idToLayer(path); - if (positions) { - latlngs.push(L.GeometryUtil.extract(polyline._map, polyline, positions[0], positions[1])); - } - else { - console.warn('Topology problem: ' + i + ' not in ' + JSON.stringify(topology.positions)); - } - } - return L.multiPolyline(latlngs); - } +// /** +// * @param topology {Object} with ``offset``, ``positions`` and ``paths`` as returned by buildSubTopology() +// * @param idToLayer {function} callback to obtain layer from id +// * @returns L.multiPolyline +// */ +// function buildGeometryFromTopology(topology, idToLayer) { +// var latlngs = []; +// for (var i=0; i < topology.paths.length; i++) { +// var path = topology.paths[i], +// positions = topology.positions[i], +// polyline = idToLayer(path); +// if (positions) { +// latlngs.push(L.GeometryUtil.extract(polyline._map, polyline, positions[0], positions[1])); +// } +// else { +// console.warn('Topology problem: ' + i + ' not in ' + JSON.stringify(topology.positions)); +// } +// } +// return L.multiPolyline(latlngs); +// } - /** - * @param idToLayer : callback to obtain a layer object from a pk/id. - * @param data : computed_path - */ - function buildTopologyFromComputedPath(idToLayer, data) { - if (!data.computed_paths) { - return { - layer: null, - serialized: null - } - } +// /** +// * @param idToLayer : callback to obtain a layer object from a pk/id. +// * @param data : computed_path +// */ +// function buildTopologyFromComputedPath(idToLayer, data) { +// if (!data.computed_paths) { +// return { +// layer: null, +// serialized: null +// } +// } - var computed_paths = data['computed_paths'] - , edges = data['new_edges'] - , offset = 0.0 // TODO: input for offset - , data = [] - , layer = L.featureGroup(); +// var computed_paths = data['computed_paths'] +// , edges = data['new_edges'] +// , offset = 0.0 // TODO: input for offset +// , data = [] +// , layer = L.featureGroup(); - console.debug('----'); - console.debug('Topology has ' + computed_paths.length + ' sub-topologies.'); +// console.debug('----'); +// console.debug('Topology has ' + computed_paths.length + ' sub-topologies.'); - for (var i = 0; i < computed_paths.length; i++ ) { - var cpath = computed_paths[i], - paths = $.map(edges[i], function(edge) { return edge.id; }), - polylines = $.map(edges[i], function(edge) { return idToLayer(edge.id); }); +// for (var i = 0; i < computed_paths.length; i++ ) { +// var cpath = computed_paths[i], +// paths = $.map(edges[i], function(edge) { return edge.id; }), +// polylines = $.map(edges[i], function(edge) { return idToLayer(edge.id); }); - var topo = buildSubTopology(paths, - polylines, - cpath.from_pop.ll, - cpath.to_pop.ll, - offset); - if (topo === null) break; +// var topo = buildSubTopology(paths, +// polylines, +// cpath.from_pop.ll, +// cpath.to_pop.ll, +// offset); +// if (topo === null) break; - data.push(topo); - console.debug('subtopo[' + i + '] : ' + JSON.stringify(topo)); +// data.push(topo); +// console.debug('subtopo[' + i + '] : ' + JSON.stringify(topo)); - // Geometry for each sub-topology - var group_layer = buildGeometryFromTopology(topo, idToLayer); - group_layer.from_pop = cpath.from_pop; - group_layer.to_pop = cpath.to_pop; - group_layer.step_idx = i; - layer.addLayer(group_layer); - } - console.debug('----'); +// // Geometry for each sub-topology +// var group_layer = buildGeometryFromTopology(topo, idToLayer); +// group_layer.from_pop = cpath.from_pop; +// group_layer.to_pop = cpath.to_pop; +// group_layer.step_idx = i; +// layer.addLayer(group_layer); +// } +// console.debug('----'); - return { - layer: layer, - serialized: data - }; - } +// return { +// layer: layer, +// serialized: data +// }; +// } - return { - buildTopologyFromComputedPath: buildTopologyFromComputedPath - }; -})(); +// return { +// buildTopologyFromComputedPath: buildTopologyFromComputedPath +// }; +// })();