Skip to content

Commit

Permalink
Merge pull request #5029 from neo-garaix/red-ml-dep/geolocationsurvey…
Browse files Browse the repository at this point in the history
…-module

Reduce mainLizmap dependencies in GeolocationSurvey module
  • Loading branch information
rldhont authored Dec 2, 2024
2 parents dda5088 + 1e802da commit 1f99c93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
37 changes: 25 additions & 12 deletions assets/src/modules/GeolocationSurvey.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @license MPL-2.0
*/

import {mainLizmap, mainEventDispatcher} from '../modules/Globals.js';
import {mainEventDispatcher} from '../modules/Globals.js';
import Edition from './Edition.js';
import Geolocation from './Geolocation.js';
import {transform} from 'ol/proj.js';

/**
Expand All @@ -14,12 +16,22 @@ import {transform} from 'ol/proj.js';
*/
export default class GeolocationSurvey {

constructor() {
/**
* Create a geolocation survey instance
*
* @param {Geolocation} geolocation - The Lizmap geolocation instance
* @param {Edition} edition - The Lizmap edition instance
* @param {object} lizmap3 - The old lizmap object
*/
constructor(geolocation, edition, lizmap3) {

this.distanceLimit = 0;
this.timeLimit = 0;
this.accuracyLimit = 0;
this.averageRecordLimit = 0;
this._geolocation = geolocation;
this._edition = edition;
this._lizmap3 = lizmap3;
this._distanceMode = false;
this._timeMode = false;
this._timePauseMode = false;
Expand All @@ -37,13 +49,13 @@ export default class GeolocationSurvey {

// Private method to insert a point at current or average position
_insertPoint() {
if (mainLizmap.geolocation.isTracking && (!this.accuracyMode || (mainLizmap.geolocation.accuracy <= this.accuracyLimit))) {
if (this._geolocation.isTracking && (!this.accuracyMode || (this._geolocation.accuracy <= this.accuracyLimit))) {

if (this.averageRecordMode && this.positionAverageInMapCRS !== undefined) {
mainLizmap.edition.drawControl.handler.insertXY(this.positionAverageInMapCRS);
this._edition.drawControl.handler.insertXY(this.positionAverageInMapCRS);
} else {
const node = mainLizmap.edition.drawControl.handler.point.geometry;
mainLizmap.edition.drawControl.handler.insertXY(node.x, node.y);
const node = this._edition.drawControl.handler.point.geometry;
this._edition.drawControl.handler.insertXY(node.x, node.y);
}

// Beep
Expand Down Expand Up @@ -83,7 +95,7 @@ export default class GeolocationSurvey {
if (this._distanceModeCallback === undefined) {
this._distanceModeCallback = () => {
// Insert automatically a point when lastSegmentLength >= distanceLimit
if (this._distanceMode && mainLizmap.edition.lastSegmentLength >= this.distanceLimit) {
if (this._distanceMode && this._edition.lastSegmentLength >= this.distanceLimit) {
this._insertPoint();
}
};
Expand Down Expand Up @@ -114,7 +126,7 @@ export default class GeolocationSurvey {
if (this._timeModeCallback === undefined) {
this._timeModeCallback = () => {
// Disable time mode when edition or geolocation end
if (!mainLizmap.edition.drawFeatureActivated || !mainLizmap.geolocation.isTracking) {
if (!this._edition.drawFeatureActivated || !this._geolocation.isTracking) {
this.toggleTimeMode(false);
}
};
Expand All @@ -124,7 +136,7 @@ export default class GeolocationSurvey {
if (this._timeMode) {
this._intervalID = window.setInterval(() => {
// Count taking care of accuracy if mode is active and pause mode
if (!this.timePauseMode && (!this.accuracyMode || (mainLizmap.geolocation.accuracy <= this.accuracyLimit))) {
if (!this.timePauseMode && (!this.accuracyMode || (this._geolocation.accuracy <= this.accuracyLimit))) {
this.timeCount = this.timeCount + 1;

// Insert automatically a point when timeCount >= timeLimit
Expand Down Expand Up @@ -214,8 +226,9 @@ export default class GeolocationSurvey {
count++;
}
}
const qgisProjectProjection = this._lizmap3.map.getProjection();

return transform([sumX / count, sumY / count], 'EPSG:4326', mainLizmap.projection);
return transform([sumX / count, sumY / count], 'EPSG:4326', qgisProjectProjection);
} else {
return undefined;
}
Expand All @@ -240,8 +253,8 @@ export default class GeolocationSurvey {
}

// Record point taking care of accuracy if mode is active
if (!this.accuracyMode || (mainLizmap.geolocation.accuracy <= this.accuracyLimit)) {
this._positionPointsRecord[now] = mainLizmap.geolocation.position;
if (!this.accuracyMode || (this._geolocation.accuracy <= this.accuracyLimit)) {
this._positionPointsRecord[now] = this._geolocation.position;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion assets/src/modules/Lizmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default class Lizmap {
this.edition = new Edition(this._lizmap3);
this.featuresTable = new FeaturesTable(this.initialConfig, this.lizmap3);
this.geolocation = new Geolocation(this.map, this.lizmap3);
this.geolocationSurvey = new GeolocationSurvey();
this.geolocationSurvey = new GeolocationSurvey(this.geolocation, this.edition);
this.selectionTool = new SelectionTool();
this.digitizing = new Digitizing();
this.snapping = new Snapping();
Expand Down

0 comments on commit 1f99c93

Please sign in to comment.