Skip to content

Commit 8d3f99c

Browse files
committed
PB-114: Moved utils function into utils
1 parent 9b4d290 commit 8d3f99c

File tree

9 files changed

+185
-162
lines changed

9 files changed

+185
-162
lines changed

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

+2-79
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,10 @@
11
import { expect } from 'chai'
22
import { describe, it } from 'vitest'
33

4-
import {
5-
formatMeters,
6-
formatPointCoordinates,
7-
formatTime,
8-
toLv95,
9-
wrapXCoordinates,
10-
} from '@/modules/drawing/lib/drawingUtils'
11-
import { LV95, WEBMERCATOR, WGS84 } from '@/utils/coordinates/coordinateSystems'
4+
import { wrapXCoordinates } from '@/modules/drawing/lib/drawingUtils'
5+
import { WGS84 } from '@/utils/coordinates/coordinateSystems'
126

137
describe('Unit test functions from drawingUtils.js', () => {
14-
describe('toLv95(coordinate, "EPSG:4326")', () => {
15-
it('reprojects points from EPSG:4326', () => {
16-
expect(LV95.isInBounds(...toLv95([6.57268, 46.51333], WGS84.epsg))).to.be.true
17-
})
18-
it('reprojects points from EPSG:3857', () => {
19-
expect(LV95.isInBounds(...toLv95([731667, 5862995], WEBMERCATOR.epsg))).to.be.true
20-
})
21-
it('reprojects lines', () => {
22-
const result = toLv95(
23-
[
24-
[6.57268, 46.51333],
25-
[6.7, 46.7],
26-
],
27-
WGS84.epsg
28-
)
29-
expect(result).to.be.an('Array').lengthOf(2)
30-
result.forEach((coord) => {
31-
expect(LV95.isInBounds(...coord)).to.be.true
32-
})
33-
})
34-
it('reprojects polygons', () => {
35-
const result = toLv95(
36-
[
37-
[6.57268, 46.51333],
38-
[6.7, 46.7],
39-
[6.9, 46.9],
40-
[6.57268, 46.51333],
41-
],
42-
WGS84.epsg
43-
)
44-
expect(result).to.be.an('Array').lengthOf(4)
45-
result.forEach((coord) => {
46-
expect(LV95.isInBounds(...coord)).to.be.true
47-
})
48-
})
49-
})
50-
51-
describe('formatMeters()', () => {
52-
it('format meters', () => {
53-
expect(formatMeters(42)).to.equal('42 m')
54-
expect(formatMeters(4002)).to.equal('4 km')
55-
expect(formatMeters(4200)).to.equal('4.2 km')
56-
expect(formatMeters(4200000)).to.equal("4'200 km")
57-
})
58-
it('format squared meters', () => {
59-
expect(formatMeters(42, { dim: 2 })).to.equal('42 m²')
60-
expect(formatMeters(4002, { dim: 2 })).to.equal("4'002 m²")
61-
expect(formatMeters(4200, { dim: 2 })).to.equal("4'200 m²")
62-
expect(formatMeters(4200000, { dim: 2 })).to.equal('4.2 km²')
63-
expect(formatMeters(4200000000, { dim: 2 })).to.equal("4'200 km²")
64-
})
65-
})
66-
67-
describe('formatPointCoordinates()', () => {
68-
it('format coordinates', () => {
69-
expect(formatPointCoordinates([2533541.8057776038, 1151703.909974419])).to.eql(
70-
"2'533'542, 1'151'704"
71-
)
72-
})
73-
})
74-
75-
describe('formatTime()', () => {
76-
it('format time', () => {
77-
expect(formatTime(null)).to.equal('-')
78-
expect(formatTime(42)).to.equal('42min')
79-
expect(formatTime(1200)).to.equal('20h')
80-
expect(formatTime(1230)).to.equal('20h 30min')
81-
expect(formatTime(1202)).to.equal('20h 2min')
82-
})
83-
})
84-
858
describe('wrapXCoordinates()', () => {
869
it('Wrap in place', () => {
8710
const original = [

src/modules/drawing/lib/drawingUtils.js

+1-79
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
import { wrapX } from 'ol/coordinate'
2-
import { LineString, Point, Polygon } from 'ol/geom'
2+
import { LineString, Polygon } from 'ol/geom'
33
import { get as getProjection } from 'ol/proj'
4-
import proj4 from 'proj4'
5-
6-
import { LV95 } from '@/utils/coordinates/coordinateSystems'
7-
import { format } from '@/utils/numberUtils'
8-
9-
export function toLv95(input, epsg) {
10-
if (Array.isArray(input[0])) {
11-
return input.map((si) => toLv95(si, epsg))
12-
} else {
13-
return proj4(epsg, LV95.epsg, [input[0], input[1]])
14-
}
15-
}
164

175
/**
186
* Wraps the provided coordinates in the world extents (i.e. the coordinate range that if equivalent
@@ -35,72 +23,6 @@ export function wrapXCoordinates(coordinates, projection, inPlace = false) {
3523
return wrapX(wrappedCoords, getProjection(projection.epsg))
3624
}
3725

38-
/** @param {[number, number]} coo Coordinates */
39-
export function formatPointCoordinates(coo) {
40-
return `${format(coo[0], 0)}, ${format(coo[1], 0)}`
41-
}
42-
43-
export function formatMeters(value, { dim = 1, digits = 2, applyFormat = true } = {}) {
44-
const factor = Math.pow(1000, dim)
45-
let unit = dim === 1 ? 'm' : 'm²'
46-
if (value >= factor) {
47-
unit = dim === 1 ? 'km' : 'km²'
48-
value /= factor
49-
}
50-
value = applyFormat ? format(value, digits) : value.toFixed(digits)
51-
return `${value} ${unit}`
52-
}
53-
54-
export function geometryInfo(type, coordinates, epsg) {
55-
const coos95 = toLv95(coordinates, epsg)
56-
const output = {
57-
type,
58-
}
59-
if (type === Point) {
60-
output.location = formatPointCoordinates(coos95)
61-
} else {
62-
if (type === Polygon) {
63-
const poly = new Polygon(coos95)
64-
output.area = formatMeters(poly.getArea(), { dim: 2 })
65-
const line = new LineString(coos95[0])
66-
output.perimeter = formatMeters(line.getLength())
67-
} else {
68-
const line = new LineString(coos95)
69-
output.length = formatMeters(line.getLength())
70-
}
71-
}
72-
return output
73-
}
74-
75-
/**
76-
* Formats minutes to hours and minutes (if more than one hour) e.g. 1230 -> '20h 30min', 55 ->
77-
* '55min'
78-
*
79-
* @param {Number} minutes
80-
* @returns {string}
81-
*/
82-
export function formatTime(minutes) {
83-
if (isNaN(minutes) || minutes === null) {
84-
return '-'
85-
}
86-
let result = ''
87-
if (minutes >= 60) {
88-
let hours = Math.floor(minutes / 60)
89-
minutes = minutes - hours * 60
90-
result += `${hours}h`
91-
if (minutes > 0) {
92-
result += ` ${minutes}min`
93-
}
94-
} else {
95-
result += `${minutes}min`
96-
}
97-
return result
98-
}
99-
100-
export function formatAngle(value, digits = 2) {
101-
return `${value.toFixed(digits)}°`
102-
}
103-
10426
/**
10527
* Checks if point is at target within tolerance.
10628
*

src/modules/infobox/components/FeatureElevationProfileInformation.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030

3131
<script>
3232
import ElevationProfile from '@/api/profile/ElevationProfile.class'
33-
import { formatTime } from '@/modules/drawing/lib/drawingUtils'
3433
import { format } from '@/utils/numberUtils'
34+
import { formatMinutesTime } from '@/utils/utils'
3535
3636
export default {
3737
props: {
@@ -85,7 +85,7 @@ export default {
8585
{
8686
title: 'profile_hike_time',
8787
icons: [['far', 'clock']],
88-
value: formatTime(this.profile.hikingTime),
88+
value: formatMinutesTime(this.profile.hikingTime),
8989
},
9090
]
9191
}

src/modules/infobox/components/styling/SelectedFeatureProfile.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
2121
import { mapState } from 'vuex'
2222
2323
import { EditableFeature, EditableFeatureTypes } from '@/api/features.api'
24-
import { geometryInfo } from '@/modules/drawing/lib/drawingUtils'
24+
import { geometryInfo } from '@/utils/utils'
2525
2626
export default {
2727
components: { FontAwesomeIcon },

src/utils/__tests__/utils.spec.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect } from 'chai'
2+
import { describe, it } from 'vitest'
3+
4+
import { formatMeters, formatMinutesTime, formatPointCoordinates } from '@/utils/utils'
5+
6+
describe('utils', () => {
7+
describe('formatMinutesTime()', () => {
8+
it('format time', () => {
9+
expect(formatMinutesTime(null)).to.equal('-')
10+
expect(formatMinutesTime(42)).to.equal('42min')
11+
expect(formatMinutesTime(1200)).to.equal('20h')
12+
expect(formatMinutesTime(1230)).to.equal('20h 30min')
13+
expect(formatMinutesTime(1202)).to.equal('20h 2min')
14+
})
15+
})
16+
17+
describe('formatMeters()', () => {
18+
it('format meters', () => {
19+
expect(formatMeters(42)).to.equal('42 m')
20+
expect(formatMeters(4002)).to.equal('4 km')
21+
expect(formatMeters(4200)).to.equal('4.2 km')
22+
expect(formatMeters(4200000)).to.equal("4'200 km")
23+
})
24+
it('format squared meters', () => {
25+
expect(formatMeters(42, { dim: 2 })).to.equal('42 m²')
26+
expect(formatMeters(4002, { dim: 2 })).to.equal("4'002 m²")
27+
expect(formatMeters(4200, { dim: 2 })).to.equal("4'200 m²")
28+
expect(formatMeters(4200000, { dim: 2 })).to.equal('4.2 km²')
29+
expect(formatMeters(4200000000, { dim: 2 })).to.equal("4'200 km²")
30+
})
31+
})
32+
33+
describe('formatPointCoordinates()', () => {
34+
it('format coordinates', () => {
35+
expect(formatPointCoordinates([2533541.8057776038, 1151703.909974419])).to.eql(
36+
"2'533'542, 1'151'704"
37+
)
38+
})
39+
})
40+
})

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

+38
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
flattenExtent,
88
normalizeExtent,
99
reprojectUnknownSrsCoordsToWGS84,
10+
toLv95,
1011
} from '@/utils/coordinates/coordinateUtils'
1112

1213
describe('Unit test functions from coordinateUtils.js', () => {
@@ -103,4 +104,41 @@ describe('Unit test functions from coordinateUtils.js', () => {
103104
expect(flattenExtent(flatExtent)).to.deep.equal([1, 2, 3, 4])
104105
})
105106
})
107+
108+
describe('toLv95(coordinate, "EPSG:4326")', () => {
109+
it('reprojects points from EPSG:4326', () => {
110+
expect(LV95.isInBounds(...toLv95([6.57268, 46.51333], WGS84.epsg))).to.be.true
111+
})
112+
it('reprojects points from EPSG:3857', () => {
113+
expect(LV95.isInBounds(...toLv95([731667, 5862995], WEBMERCATOR.epsg))).to.be.true
114+
})
115+
it('reprojects lines', () => {
116+
const result = toLv95(
117+
[
118+
[6.57268, 46.51333],
119+
[6.7, 46.7],
120+
],
121+
WGS84.epsg
122+
)
123+
expect(result).to.be.an('Array').lengthOf(2)
124+
result.forEach((coord) => {
125+
expect(LV95.isInBounds(...coord)).to.be.true
126+
})
127+
})
128+
it('reprojects polygons', () => {
129+
const result = toLv95(
130+
[
131+
[6.57268, 46.51333],
132+
[6.7, 46.7],
133+
[6.9, 46.9],
134+
[6.57268, 46.51333],
135+
],
136+
WGS84.epsg
137+
)
138+
expect(result).to.be.an('Array').lengthOf(4)
139+
result.forEach((coord) => {
140+
expect(LV95.isInBounds(...coord)).to.be.true
141+
})
142+
})
143+
})
106144
})

src/utils/coordinates/coordinateUtils.js

+8
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,11 @@ export class OutOfBoundsError extends Error {
126126
this.name = 'OutOfBoundsError'
127127
}
128128
}
129+
130+
export function toLv95(input, epsg) {
131+
if (Array.isArray(input[0])) {
132+
return input.map((si) => toLv95(si, epsg))
133+
} else {
134+
return proj4(epsg, LV95.epsg, [input[0], input[1]])
135+
}
136+
}

src/utils/geodesicManager.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import RBush from 'ol/structs/RBush' /* Warning: private class of openlayers */
1010
import { Circle, Fill, RegularShape, Stroke, Style, Text } from 'ol/style'
1111
import proj4 from 'proj4'
1212

13-
import { formatAngle, formatMeters } from '@/modules/drawing/lib/drawingUtils'
1413
import { WGS84 } from '@/utils/coordinates/coordinateSystems'
1514
import log from '@/utils/logging'
15+
import { formatAngle, formatMeters } from '@/utils/utils'
1616

1717
const geod = Geodesic.WGS84
1818

0 commit comments

Comments
 (0)