Skip to content

Commit 132001b

Browse files
authored
Merge pull request #509 from geoadmin/bug-BGDIINF_SB-2890-legacy-param-e2e
BGDIINF_SB-2890: Fixed legacy parameters coordinates
2 parents 30837d1 + f2096b4 commit 132001b

File tree

3 files changed

+111
-90
lines changed

3 files changed

+111
-90
lines changed

src/router/legacyPermalinkManagement.routerPlugin.js

+108-83
Original file line numberDiff line numberDiff line change
@@ -43,99 +43,124 @@ const handleLegacyKmlAdminIdParam = async (legacyParams, newQuery) => {
4343
return newQuery
4444
}
4545

46+
const handleLegacyParam = (
47+
param,
48+
legacyValue,
49+
store,
50+
newQuery,
51+
latlongCoordinates,
52+
legacyCoordinates
53+
) => {
54+
const { projection } = store.state.position
55+
let newValue
56+
57+
let key = param
58+
switch (param) {
59+
case 'zoom':
60+
// the legacy viewer always expresses its zoom level in the LV95 context (so in SwissCoordinateSystem)
61+
if (!(projection instanceof SwissCoordinateSystem)) {
62+
newValue = LV95.transformCustomZoomLevelToStandard(legacyValue)
63+
if (projection instanceof CustomCoordinateSystem) {
64+
newValue = projection.transformStandardZoomLevelToCustom(newValue)
65+
}
66+
} else {
67+
newValue = legacyValue
68+
}
69+
key = 'z'
70+
break
71+
72+
// storing coordinate parts for later conversion
73+
case 'E':
74+
case 'X':
75+
legacyCoordinates[0] = Number(legacyValue)
76+
break
77+
case 'N':
78+
case 'Y':
79+
legacyCoordinates[1] = Number(legacyValue)
80+
break
81+
82+
case 'lon':
83+
latlongCoordinates[0] = Number(legacyValue)
84+
break
85+
case 'lat':
86+
latlongCoordinates[1] = Number(legacyValue)
87+
break
88+
89+
// taking all layers related param aside so that they can be processed later (see below)
90+
// this only occurs if the syntax is recognized as a mf-geoadmin3 syntax (or legacy)
91+
case 'layers':
92+
if (isLayersUrlParamLegacy(legacyValue)) {
93+
// for legacy layers param, we need to give the whole search query
94+
// as it needs to look for layers, layers_visibility, layers_opacity and
95+
// layers_timestamp param altogether
96+
const layers = getLayersFromLegacyUrlParams(
97+
store.state.layers.config,
98+
window.location.search
99+
)
100+
newValue = layers.map((layer) => transformLayerIntoUrlString(layer)).join(';')
101+
log.debug('Importing legacy layers as', newValue)
102+
} else {
103+
// if not legacy, we let it go as it is
104+
newValue = legacyValue
105+
}
106+
break
107+
case 'layers_opacity':
108+
case 'layers_visibility':
109+
case 'layers_timestamp':
110+
// we ignore those params as they are now obsolete
111+
// see adr/2021_03_16_url_param_structure.md
112+
break
113+
114+
// if no special work to do, we just copy past legacy params to the new viewer
115+
116+
default:
117+
newValue = legacyValue
118+
}
119+
120+
if (newValue) {
121+
// When receiving a query, the application will encode the URI components
122+
// We decode those so that the new query won't encode encoded character
123+
// for example, we avoid having " " becoming %2520 in the URI
124+
newQuery[key] = decodeURIComponent(newValue)
125+
}
126+
}
127+
46128
const handleLegacyParams = (legacyParams, store, to, next) => {
47129
log.info(`Legacy permalink with param=`, legacyParams)
48130
// if so, we transfer all old param (stored before vue-router's /#) and transfer them to the MapView
49131
// we will also transform legacy zoom level here (see comment below)
50132
const newQuery = { ...to.query }
51133
const { projection } = store.state.position
52134
let legacyCoordinates = []
53-
let legacyCoordinatesAreExpressedInWGS84 = false
135+
let latlongCoordinates = []
136+
54137
Object.keys(legacyParams).forEach((param) => {
55-
let value
56-
57-
let key = param
58-
switch (param) {
59-
case 'zoom':
60-
// the legacy viewer always expresses its zoom level in the LV95 context (so in SwissCoordinateSystem)
61-
if (!(projection instanceof SwissCoordinateSystem)) {
62-
value = LV95.transformCustomZoomLevelToStandard(legacyParams[param])
63-
if (projection instanceof CustomCoordinateSystem) {
64-
value = projection.transformStandardZoomLevelToCustom(value)
65-
}
66-
} else {
67-
value = legacyParams[param]
68-
}
69-
key = 'z'
70-
break
71-
72-
// storing coordinate parts for later conversion
73-
case 'E':
74-
case 'X':
75-
case 'lon':
76-
legacyCoordinates[0] = Number(legacyParams[param])
77-
break
78-
case 'N':
79-
case 'Y':
80-
case 'lat':
81-
legacyCoordinates[1] = Number(legacyParams[param])
82-
break
83-
84-
case 'lon':
85-
case 'lat':
86-
legacyCoordinatesAreExpressedInWGS84 = true
87-
break
88-
89-
// taking all layers related param aside so that they can be processed later (see below)
90-
// this only occurs if the syntax is recognized as a mf-geoadmin3 syntax (or legacy)
91-
case 'layers':
92-
if (isLayersUrlParamLegacy(legacyParams[param])) {
93-
// for legacy layers param, we need to give the whole search query
94-
// as it needs to look for layers, layers_visibility, layers_opacity and
95-
// layers_timestamp param altogether
96-
const layers = getLayersFromLegacyUrlParams(
97-
store.state.layers.config,
98-
window.location.search
99-
)
100-
value = layers.map((layer) => transformLayerIntoUrlString(layer)).join(';')
101-
log.debug('Importing legacy layers as', value)
102-
} else {
103-
// if not legacy, we let it go as it is
104-
value = legacyParams[param]
105-
}
106-
break
107-
case 'layers_opacity':
108-
case 'layers_visibility':
109-
case 'layers_timestamp':
110-
// we ignore those params as they are now obsolete
111-
// see adr/2021_03_16_url_param_structure.md
112-
break
113-
114-
// if no special work to do, we just copy past legacy params to the new viewer
115-
116-
default:
117-
value = legacyParams[param]
118-
}
138+
handleLegacyParam(
139+
param,
140+
legacyParams[param],
141+
store,
142+
newQuery,
143+
latlongCoordinates,
144+
legacyCoordinates
145+
)
146+
})
119147

120-
// if a legacy coordinate (x/y, N/E or lon/lat) was used, we need to build the center param from them
121-
if (legacyCoordinates.length === 2 && legacyCoordinates[0] && legacyCoordinates[1]) {
122-
// if lon/lat were used, we need to re-project them in the current projection system
123-
if (legacyCoordinatesAreExpressedInWGS84) {
124-
legacyCoordinates = proj4(WGS84.epsg, projection.epsg, legacyCoordinates)
125-
} else if (projection.epsg !== LV95.epsg) {
126-
// if the current projection is not LV95, we also need to re-project x/y or N/E
127-
// (the legacy viewer was always writing coordinates in LV95 in the URL)
128-
legacyCoordinates = proj4(LV95.epsg, projection.epsg, legacyCoordinates)
129-
}
130-
newQuery['center'] = legacyCoordinates.join(',')
148+
// Convert legacies coordinates if needed
149+
if (latlongCoordinates.length === 2) {
150+
legacyCoordinates = proj4(WGS84.epsg, projection.epsg, latlongCoordinates)
151+
} else if (legacyCoordinates.length === 2) {
152+
if (projection.epsg !== LV95.epsg) {
153+
// if the current projection is not LV95, we also need to re-project x/y or N/E
154+
// (the legacy viewer was always writing coordinates in LV95 in the URL)
155+
legacyCoordinates = proj4(LV95.epsg, projection.epsg, legacyCoordinates)
131156
}
132-
if (value) {
133-
// When receiving a query, the application will encode the URI components
134-
// We decode those so that the new query won't encode encoded character
135-
// for example, we avoid having " " becoming %2520 in the URI
136-
newQuery[key] = decodeURIComponent(value)
137-
}
138-
})
157+
}
158+
159+
// if a legacy coordinate (x/y, N/E or lon/lat) was used, we need to build the
160+
// center param from them
161+
if (legacyCoordinates.length === 2) {
162+
newQuery['center'] = legacyCoordinates.join(',')
163+
}
139164

140165
// removing old query part (new ones will be added by vue-router after the /# part of the URL)
141166
const urlWithoutQueryParam = window.location.href.substr(0, window.location.href.indexOf('?'))

tests/e2e-cypress/integration/geolocation.cy.js

-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22

33
import { DEFAULT_PROJECTION } from '@/config'
44
import { WGS84 } from '@/utils/coordinates/coordinateSystems'
5-
import setupProj4 from '@/utils/setupProj4'
65
import proj4 from 'proj4'
76

8-
setupProj4()
9-
107
const geolocationButtonSelector = '[data-cy="geolocation-button"]'
118

129
function getGeolocationButtonAndClickIt() {

tests/e2e-cypress/integration/legacyParamImport.cy.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { DEFAULT_PROJECTION } from '@/config'
44
import { WGS84 } from '@/utils/coordinates/coordinateSystems'
5+
import proj4 from 'proj4'
56

67
describe('Test on legacy param import', () => {
78
context('Coordinates import', () => {
@@ -43,7 +44,7 @@ describe('Test on legacy param import', () => {
4344
})
4445
})
4546

46-
it('reproject LV95 coordinates/zoom param to EPSG:4326', () => {
47+
it('reproject LV95 coordinates param to EPSG:4326', () => {
4748
const E = 2660000
4849
const N = 1200000
4950
const lv95zoom = 8
@@ -53,9 +54,7 @@ describe('Test on legacy param import', () => {
5354
zoom: lv95zoom,
5455
})
5556

56-
// the LV95 zoom level should be translated to a mercator zoom level of 15.5 according to
57-
// https://github.com/geoadmin/mf-geoadmin3/blob/ce885985e4af5e3e20c87321e67a650388af3602/src/components/map/MapUtilsService.js#L603-L631
58-
cy.readStoreValue('state.position.zoom').should('eq', 15.5)
57+
cy.readStoreValue('state.position.zoom').should('eq', lv95zoom)
5958

6059
// checking that we are reprojected to lon: 8.2267733° lat: 46.9483767°
6160
// (according to https://epsg.io/transform#s_srs=2056&t_srs=4326&x=2660000.0000000&y=1200000.0000000)

0 commit comments

Comments
 (0)