Skip to content

Commit 645b2dd

Browse files
committed
PB-114: Moved wrapXCoordinates util
1 parent 8d3f99c commit 645b2dd

File tree

5 files changed

+63
-62
lines changed

5 files changed

+63
-62
lines changed

src/modules/drawing/components/useDrawingModeInteraction.composable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { computed, inject, nextTick, onBeforeUnmount, onMounted, ref } from 'vue
77
import { useStore } from 'vuex'
88

99
import { EditableFeature } from '@/api/features.api'
10-
import { wrapXCoordinates } from '@/modules/drawing/lib/drawingUtils'
1110
import { editingFeatureStyleFunction } from '@/modules/drawing/lib/style'
1211
import useSaveKmlOnChange from '@/modules/drawing/useKmlDataManagement.composable'
12+
import { wrapXCoordinates } from '@/utils/coordinates/coordinateUtils'
1313
import { featureStyleFunction } from '@/utils/featureStyleUtils'
1414
import { GeodesicGeometries } from '@/utils/geodesicManager'
1515
import log from '@/utils/logging'

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

-38
This file was deleted.

src/modules/drawing/lib/drawingUtils.js

-23
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,4 @@
1-
import { wrapX } from 'ol/coordinate'
21
import { LineString, Polygon } from 'ol/geom'
3-
import { get as getProjection } from 'ol/proj'
4-
5-
/**
6-
* Wraps the provided coordinates in the world extents (i.e. the coordinate range that if equivalent
7-
* to the wgs84 [-180, 180))
8-
*
9-
* @param {Array} coordinates The coordinates (or array of coordinates) to wrap
10-
* @param {CoordinateSystem} projection Projection of the coordinates
11-
* @param {boolean} inPlace If false, the original coordinates remain untouched and only a copy is
12-
* modified
13-
* @returns If "inPlace", then the same reference as "coords", else a reference to the modified copy
14-
*/
15-
export function wrapXCoordinates(coordinates, projection, inPlace = false) {
16-
let wrappedCoords = coordinates
17-
if (!inPlace) {
18-
wrappedCoords = wrappedCoords.slice()
19-
}
20-
if (Array.isArray(wrappedCoords[0])) {
21-
return wrappedCoords.map((c) => wrapXCoordinates(c, projection, inPlace))
22-
}
23-
return wrapX(wrappedCoords, getProjection(projection.epsg))
24-
}
252

263
/**
274
* Checks if point is at target within tolerance.

src/utils/coordinates/__test__/coordinateUtils.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
reprojectUnknownSrsCoordsToWGS84,
1010
toLv95,
1111
} from '@/utils/coordinates/coordinateUtils'
12+
import { wrapXCoordinates } from '@/utils/coordinates/coordinateUtils'
1213

1314
describe('Unit test functions from coordinateUtils.js', () => {
1415
describe('reprojectUnknownSrsCoordsToWGS84(x,y)', () => {
@@ -141,4 +142,35 @@ describe('Unit test functions from coordinateUtils.js', () => {
141142
})
142143
})
143144
})
145+
146+
describe('wrapXCoordinates()', () => {
147+
it('Wrap in place', () => {
148+
const original = [
149+
[300, 300],
150+
[360, 360],
151+
]
152+
const ref2Original = wrapXCoordinates(original, WGS84, true)
153+
expect(ref2Original).to.deep.equal([
154+
[-60, 300],
155+
[0, 360],
156+
])
157+
expect(ref2Original).to.deep.equal(original)
158+
})
159+
it('Wrap not in place', () => {
160+
const original = [
161+
[300, 300],
162+
[360, 360],
163+
]
164+
const ref2Original = wrapXCoordinates(original, WGS84, false)
165+
expect(ref2Original).to.deep.equal([
166+
[-60, 300],
167+
[0, 360],
168+
])
169+
expect(ref2Original).to.not.deep.equal(original)
170+
expect(original).to.deep.equal([
171+
[300, 300],
172+
[360, 360],
173+
])
174+
})
175+
})
144176
})

src/utils/coordinates/coordinateUtils.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { wrapX } from 'ol/coordinate'
2+
import { get as getProjection } from 'ol/proj'
13
import proj4 from 'proj4'
24

35
import log from '../logging'
@@ -127,10 +129,38 @@ export class OutOfBoundsError extends Error {
127129
}
128130
}
129131

132+
/**
133+
* Convert recursively input coordinates into LV95
134+
*
135+
* @param {[]} input Coordinate of a point, multipoint or polygon
136+
* @param {String} epsg EPSG code of the coordinates
137+
* @returns {[]} Reprojected coordinates
138+
*/
130139
export function toLv95(input, epsg) {
131140
if (Array.isArray(input[0])) {
132141
return input.map((si) => toLv95(si, epsg))
133142
} else {
134143
return proj4(epsg, LV95.epsg, [input[0], input[1]])
135144
}
136145
}
146+
147+
/**
148+
* Wraps the provided coordinates in the world extents (i.e. the coordinate range that if equivalent
149+
* to the wgs84 [-180, 180))
150+
*
151+
* @param {Array} coordinates The coordinates (or array of coordinates) to wrap
152+
* @param {CoordinateSystem} projection Projection of the coordinates
153+
* @param {boolean} inPlace If false, the original coordinates remain untouched and only a copy is
154+
* modified
155+
* @returns If "inPlace", then the same reference as "coords", else a reference to the modified copy
156+
*/
157+
export function wrapXCoordinates(coordinates, projection, inPlace = false) {
158+
let wrappedCoords = coordinates
159+
if (!inPlace) {
160+
wrappedCoords = wrappedCoords.slice()
161+
}
162+
if (Array.isArray(wrappedCoords[0])) {
163+
return wrappedCoords.map((c) => wrapXCoordinates(c, projection, inPlace))
164+
}
165+
return wrapX(wrappedCoords, getProjection(projection.epsg))
166+
}

0 commit comments

Comments
 (0)