Skip to content

Commit

Permalink
Merge pull request #962 from geoadmin/fix-PB-716-inverted-wgs84-in-se…
Browse files Browse the repository at this point in the history
…archbar

PB-716 : guess WGS84 coordinate order in the searchbar - #patch
  • Loading branch information
pakb authored Jun 26, 2024
2 parents da9e23f + 53d606d commit 9004978
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
12 changes: 9 additions & 3 deletions src/utils/coordinates/__test__/coordinateExtractors.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ describe('Unit test functions from coordinateExtractors.js', () => {
acceptableDelta
)
})
it('Returns coordinate with DMS format with cardinal point information in the south west hemisphere', () => {
// To be reactivated when supporting world-wide coverage (in the meantime the extractor will only output coordinate
// that are in Switzerland, inverting x/y -> y/x if necessary)
it.skip('Returns coordinate with DMS format with cardinal point information in the south west hemisphere', () => {
const pointInSouthAmericaInEPSG3857 = [-6504867, -4110554]
const pointInSouthAmericaInEPSG4326 = ['34°36\'23.937"S', '58°26\'3.172"W']
checkXY(
Expand All @@ -366,7 +368,9 @@ describe('Unit test functions from coordinateExtractors.js', () => {
acceptableDelta
)
})
it('Returns coordinate with DMS format with cardinal point information in the north west hemisphere', () => {
// To be reactivated when supporting world-wide coverage (in the meantime the extractor will only output coordinate
// that are in Switzerland, inverting x/y -> y/x if necessary)
it.skip('Returns coordinate with DMS format with cardinal point information in the north west hemisphere', () => {
const pointInNorthAmericaInEPSG3857 = [-9457276, 4961988]
const pointInNorthAmericaInEPSG4326 = ['40°39\'27.846"N', '84°57\'22.161"W']
checkXY(
Expand All @@ -386,7 +390,9 @@ describe('Unit test functions from coordinateExtractors.js', () => {
acceptableDelta
)
})
it('Returns coordinate with DMS format with cardinal point information in the south east hemisphere', () => {
// To be reactivated when supporting world-wide coverage (in the meantime the extractor will only output coordinate
// that are in Switzerland, inverting x/y -> y/x if necessary)
it.skip('Returns coordinate with DMS format with cardinal point information in the south east hemisphere', () => {
const pointInOceaniaInEPSG3857 = [12894439, -3757563]
const pointInOceaniaInEPSG4326 = ['31°57\'22.332"S', '115°49\'57.779"E']
checkXY(
Expand Down
13 changes: 10 additions & 3 deletions src/utils/coordinates/coordinateExtractors.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import proj4 from 'proj4'

import { WGS84 } from '@/utils/coordinates/coordinateSystems'
import { LV95, WGS84 } from '@/utils/coordinates/coordinateSystems'
import { reprojectUnknownSrsCoordsToWGS84 } from '@/utils/coordinates/coordinateUtils'
import { toPoint as mgrsToWGS84 } from '@/utils/militaryGridProjection'

Expand All @@ -23,6 +23,8 @@ const REGEX_METRIC_COORDINATES =
// Military Grid Reference System (MGRS)
const REGEX_MILITARY_GRID = /^3[123][\sa-z]{3}[\s\d]*/i

const LV95_BOUNDS_IN_WGS84 = LV95.getBoundsAs(WGS84)

const numericalExtractor = (regexMatches) => {
// removing thousand separators
const x = Number(regexMatches[1].replace(/[' ]/g, ''))
Expand Down Expand Up @@ -96,8 +98,13 @@ const webmercatorExtractor = (regexMatches) => {
break
}
}
if (lon && lat && WGS84.isInBounds(lon, lat)) {
return [lon, lat]
if (lon && lat) {
if (LV95_BOUNDS_IN_WGS84.isInBounds(lon, lat)) {
return [lon, lat]
}
if (LV95_BOUNDS_IN_WGS84.isInBounds(lat, lon)) {
return [lat, lon]
}
}
return null
}
Expand Down
31 changes: 25 additions & 6 deletions tests/cypress/tests-e2e/search/coordinates-search.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,24 @@ describe('Testing coordinates typing in search bar', () => {
expect(feature[1]).to.be.approximately(expectedCenter[1], acceptableDelta)
})
}
const standardCheck = (x, y, acceptableDelta = 0.0) => {
const standardCheck = (x, y, options = {}) => {
const { acceptableDelta = 0.0, withInversion = false } = options
cy.get(searchbarSelector).should('be.visible')
cy.get(searchbarSelector).paste(`${x} ${y}`)
checkCenterInStore(acceptableDelta)
checkZoomLevelInStore()
checkThatCoordinateAreHighlighted(acceptableDelta)
if (withInversion) {
cy.get(searchbarSelector).clear()
cy.get(searchbarSelector).paste(`${y} ${x}`)
checkCenterInStore(acceptableDelta)
checkZoomLevelInStore()
checkThatCoordinateAreHighlighted(acceptableDelta)
}
}

it('Paste and clear LV95 coordinates in search bar', () => {
standardCheck(expectedCenterLV95[0], expectedCenterLV95[1])
standardCheck(expectedCenterLV95[0], expectedCenterLV95[1], { withInversion: true })
cy.get('[data-cy="searchbar-clear"]').click()
// checking that search bar has been emptied
cy.readStoreValue('state.search.query').should('be.empty')
Expand All @@ -79,21 +87,32 @@ describe('Testing coordinates typing in search bar', () => {
return `${degree}° ${(minutes * 60.0).toFixed(4)}'`
})
const acceptableDelta = 0.25
standardCheck(expectedCenterWGS84[0], expectedCenterWGS84[1], acceptableDelta)
standardCheck(expectedCenterWGS84[0], expectedCenterWGS84[1], {
acceptableDelta,
withInversion: true,
})
// clear the bar
cy.get('[data-cy="searchbar-clear"]').click()
// checking that search bar has been emptied
cy.readStoreValue('state.search.query').should('be.empty')
standardCheck(expectedCenterWGS84_DD[0], expectedCenterWGS84_DD[1], acceptableDelta)
standardCheck(expectedCenterWGS84_DD[0], expectedCenterWGS84_DD[1], {
acceptableDelta,
withInversion: true,
})
})

it('Paste EPSG:3857 (Web-Mercator) coordinate', () => {
const acceptableDelta = 0
standardCheck(expectedCenterWebMercator[0], expectedCenterWebMercator[1], acceptableDelta)
standardCheck(expectedCenterWebMercator[0], expectedCenterWebMercator[1], {
acceptableDelta,
})
})

it('Paste EPSG:21781 (LV03) coordinates', () => {
standardCheck(expectedCenterLV03[0], expectedCenterLV03[1], 0.1)
standardCheck(expectedCenterLV03[0], expectedCenterLV03[1], {
acceptableDelta: 0.1,
withInversion: true,
})
})

context('What3Words input', () => {
Expand Down

0 comments on commit 9004978

Please sign in to comment.