Skip to content

Commit 8165f69

Browse files
authored
Merge pull request #498 from geoadmin/feat-BGDIINF_SB-2890-coordinate-search
BGDIINF_SB-2890 : fix coordinate search
2 parents 7b3395c + eae7e53 commit 8165f69

17 files changed

+438
-252
lines changed

src/api/what3words.api.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WEBMERCATOR, WGS84 } from '@/utils/coordinates/coordinateSystems'
1+
import { WGS84 } from '@/utils/coordinates/coordinateSystems'
22
import log from '@/utils/logging'
33
import axios from 'axios'
44
import proj4 from 'proj4'
@@ -25,9 +25,10 @@ export const isWhat3WordsString = (text) => {
2525
*
2626
* @param {String} what3wordsString A what3word string (validity will be checked before sending it
2727
* to the API)
28+
* @param {CoordinateSystem} outputProjection The wanted output projection for the W3W result
2829
* @returns {Promise<Number[]>} Lat, lon array (in EPSG:3857 so in meters)
2930
*/
30-
export const retrieveWhat3WordsLocation = (what3wordsString) => {
31+
export const retrieveWhat3WordsLocation = (what3wordsString, outputProjection) => {
3132
return new Promise((resolve, reject) => {
3233
if (!isWhat3WordsString(what3wordsString)) {
3334
reject('Bad what3words string :' + what3wordsString)
@@ -38,13 +39,13 @@ export const retrieveWhat3WordsLocation = (what3wordsString) => {
3839
)
3940
// Response structure in the doc : https://developer.what3words.com/public-api/docs#convert-to-coords
4041
.then((response) => {
41-
const what3wordLocationEpsg3857 = proj4(WGS84.epsg, WEBMERCATOR.epsg, [
42+
const what3wordLocationEpsg3857 = proj4(WGS84.epsg, outputProjection.epsg, [
4243
response.data.coordinates.lng,
4344
response.data.coordinates.lat,
4445
])
4546
resolve([
46-
WEBMERCATOR.roundCoordinateValue(what3wordLocationEpsg3857[0]),
47-
WEBMERCATOR.roundCoordinateValue(what3wordLocationEpsg3857[1]),
47+
outputProjection.roundCoordinateValue(what3wordLocationEpsg3857[0]),
48+
outputProjection.roundCoordinateValue(what3wordLocationEpsg3857[1]),
4849
])
4950
})
5051
.catch((error) => {

src/main.js

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import router from '@/router'
2222
import store from '@/store'
2323
import log from '@/utils/logging'
2424
import setupChartJS from '@/utils/setupChartJS'
25-
import setupProj4 from '@/utils/setupProj4'
2625

2726
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
2827
// Importing styling CSS libraries
@@ -56,7 +55,6 @@ log.debug('Config is', {
5655
})
5756

5857
tippy.setDefaultProps({ theme: 'light-border' })
59-
setupProj4()
6058
setupChartJS()
6159

6260
const app = createApp(App)

src/modules/drawing/lib/__tests__/drawingUtils.spec.js

-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@ import {
66
wrapXCoordinates,
77
} from '@/modules/drawing/lib/drawingUtils'
88
import { LV95, WEBMERCATOR, WGS84 } from '@/utils/coordinates/coordinateSystems'
9-
import setupProj4 from '@/utils/setupProj4'
109
import { expect } from 'chai'
1110
import { describe, it } from 'vitest'
1211

13-
// setting up projection for proj4 otherwise they will fail when asked
14-
setupProj4()
15-
1612
describe('Unit test functions from drawingUtils.js', () => {
1713
describe('toLv95(coordinate, "EPSG:4326")', () => {
1814
it('reprojects points from EPSG:4326', () => {

src/store/modules/position.store.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ const state = {
5757
*
5858
* @type Number
5959
*/
60-
zoom: DEFAULT_PROJECTION.getDefaultZoom(),
60+
// some unit tests fail because DEFAULT_PROJECTION is somehow not yet defined when they are run
61+
// hence the `?.` operator
62+
zoom: DEFAULT_PROJECTION?.getDefaultZoom(),
6163

6264
/**
6365
* The map rotation expressed so that -Pi < rotation <= Pi
@@ -71,7 +73,9 @@ const state = {
7173
*
7274
* @type Array<Number>
7375
*/
74-
center: DEFAULT_PROJECTION.bounds.center,
76+
// some unit tests fail because DEFAULT_PROJECTION is somehow not yet defined when they are run
77+
// hence the `?.` operator
78+
center: DEFAULT_PROJECTION?.bounds.center,
7579

7680
/**
7781
* Projection used to express the position (and subsequently used to define how the mapping
@@ -252,6 +256,12 @@ const actions = {
252256
matchingProjection = allCoordinateSystems.find(
253257
(coordinateSystem) => coordinateSystem.epsgNumber === projection
254258
)
259+
} else if (typeof projection === 'string' || projection instanceof String) {
260+
matchingProjection = allCoordinateSystems.find(
261+
(coordinateSystem) =>
262+
coordinateSystem.epsg === projection ||
263+
coordinateSystem.epsgNumber === parseInt(projection)
264+
)
255265
}
256266
if (matchingProjection) {
257267
commit('setProjection', matchingProjection)

src/store/modules/search.store.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import search, { CombinedSearchResults, RESULT_TYPE } from '@/api/search.api'
22
import { isWhat3WordsString, retrieveWhat3WordsLocation } from '@/api/what3words.api'
33
import coordinateFromString from '@/utils/coordinates/coordinateExtractors'
4+
import CustomCoordinateSystem from '@/utils/coordinates/CustomCoordinateSystem.class'
45
import { STANDARD_ZOOM_LEVEL_1_25000_MAP } from '@/utils/coordinates/SwissCoordinateSystem.class'
56
import { ActiveLayerConfig } from '@/utils/layerUtils'
67
import log from '@/utils/logging'
@@ -49,22 +50,41 @@ const actions = {
4950
let updatedSearchResults = false
5051
// only firing search if query is longer than 2 chars
5152
if (query.length > 2) {
53+
const currentProjection = rootState.position.projection
5254
// checking first if this corresponds to a set of coordinates (or a what3words)
53-
const coordinate = coordinateFromString(query)
55+
const coordinate = coordinateFromString(query, currentProjection)
5456
if (coordinate) {
5557
dispatch('setCenter', coordinate)
56-
dispatch('setZoom', STANDARD_ZOOM_LEVEL_1_25000_MAP)
58+
if (currentProjection instanceof CustomCoordinateSystem) {
59+
dispatch(
60+
'setZoom',
61+
currentProjection.transformStandardZoomLevelToCustom(
62+
STANDARD_ZOOM_LEVEL_1_25000_MAP
63+
)
64+
)
65+
} else {
66+
dispatch('setZoom', STANDARD_ZOOM_LEVEL_1_25000_MAP)
67+
}
5768
dispatch('setPinnedLocation', coordinate)
5869
} else if (isWhat3WordsString(query)) {
59-
retrieveWhat3WordsLocation(query).then((what3wordLocation) => {
70+
retrieveWhat3WordsLocation(query, currentProjection).then((what3wordLocation) => {
6071
dispatch('setCenter', what3wordLocation)
61-
dispatch('setZoom', STANDARD_ZOOM_LEVEL_1_25000_MAP)
72+
if (currentProjection instanceof CustomCoordinateSystem) {
73+
dispatch(
74+
'setZoom',
75+
currentProjection.transformStandardZoomLevelToCustom(
76+
STANDARD_ZOOM_LEVEL_1_25000_MAP
77+
)
78+
)
79+
} else {
80+
dispatch('setZoom', STANDARD_ZOOM_LEVEL_1_25000_MAP)
81+
}
6282
dispatch('setPinnedLocation', what3wordLocation)
6383
})
6484
} else {
6585
try {
6686
const searchResults = await search(
67-
rootState.position.projection,
87+
currentProjection,
6888
query,
6989
rootState.i18n.lang
7090
)

src/utils/coordinates/__test__/CoordinateSystem.class.spec.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import CoordinateSystemBounds from '@/utils/coordinates/CoordinateSystemBounds.class'
22
import { LV95, WEBMERCATOR, WGS84 } from '@/utils/coordinates/coordinateSystems'
33
import StandardCoordinateSystem from '@/utils/coordinates/StandardCoordinateSystem.class'
4-
import setupProj4 from '@/utils/setupProj4'
54
import { describe, expect, it } from 'vitest'
65

7-
setupProj4()
8-
96
describe('CoordinateSystem', () => {
107
const coordinateSystemWithouBounds = new StandardCoordinateSystem('test', 'test', 1234, null)
118
describe('getBoundsAs', () => {

0 commit comments

Comments
 (0)