Skip to content

Commit e3a6230

Browse files
committed
PB-2027: only request icon in LARGE size, and resize them with the scale
icons were doubly scaled because the scale was applied on top of a smaller icon URL. Reverting back to what was the use-case before the TS migration, which is that icons are always requested in the same size to the backend, and are scaled on the map (could we get rid of the scaling on the backend?)
1 parent 606b20a commit e3a6230

File tree

5 files changed

+29
-55
lines changed

5 files changed

+29
-55
lines changed

packages/viewer/src/api/icon.api.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import axios from 'axios'
33
import { fromString } from 'ol/color'
44

55
import { getViewerDedicatedServicesBaseUrl } from '@/config/baseUrl.config'
6-
import {
7-
type FeatureStyleColor,
8-
type FeatureStyleSize,
9-
LARGE,
10-
RED,
11-
} from '@/utils/featureStyleUtils'
6+
import { type FeatureStyleColor, LARGE, RED } from '@/utils/featureStyleUtils'
127

138
/**
149
* Generate an icon URL from its template. If no iconScale is given, the default scale 1 will be
@@ -17,19 +12,18 @@ import {
1712
*
1813
* @returns A full URL to this icon on the service-icons backend
1914
*/
20-
export function generateIconURL(
21-
icon: DrawingIcon,
22-
iconColor: FeatureStyleColor = RED,
23-
iconSize: FeatureStyleSize = LARGE
24-
) {
15+
export function generateIconURL(icon: DrawingIcon, iconColor: FeatureStyleColor = RED) {
2516
const rgb = fromString(iconColor.fill)
26-
return icon.imageTemplateURL
27-
.replace('{icon_set_name}', icon.iconSetName)
28-
.replace('{icon_name}', icon.name)
29-
.replace('{icon_scale}', iconSize.iconScale + 'x')
30-
.replace('{r}', `${rgb[0]}`)
31-
.replace('{g}', `${rgb[1]}`)
32-
.replace('{b}', `${rgb[2]}`)
17+
return (
18+
icon.imageTemplateURL
19+
.replace('{icon_set_name}', icon.iconSetName)
20+
.replace('{icon_name}', icon.name)
21+
// we always use the LARGE icon scale and resize the icon with the <IconStyle><scale> property in KMLs
22+
.replace('{icon_scale}', LARGE.iconScale + 'x')
23+
.replace('{r}', `${rgb[0]}`)
24+
.replace('{g}', `${rgb[1]}`)
25+
.replace('{b}', `${rgb[2]}`)
26+
)
3327
}
3428

3529
/**

packages/viewer/src/store/modules/drawing/actions/updateCurrentDrawingFeature.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import { generateIconURL } from '@/api/icon.api'
88
import { DrawingSaveState } from '@/store/modules/drawing/types/DrawingSaveState.enum'
99
import debounceSaveDrawing from '@/store/modules/drawing/utils/debounceSaveDrawing'
1010
import useProfileStore from '@/store/modules/profile'
11-
import {
12-
calculateTextOffset,
13-
type FeatureStyleColor,
14-
type FeatureStyleSize,
15-
MEDIUM,
16-
RED,
17-
} from '@/utils/featureStyleUtils'
11+
import { calculateTextOffset } from '@/utils/featureStyleUtils'
1812

1913
export default function updateCurrentDrawingFeature(
2014
this: DrawingStore,
@@ -24,21 +18,24 @@ export default function updateCurrentDrawingFeature(
2418
if (this.feature.current) {
2519
this.save.state = DrawingSaveState.UnsavedChanges
2620

27-
let needIconUrlRefresh = false
28-
2921
Object.assign(this.feature.current, valuesToUpdate)
3022

3123
// keeping values as preferred, if present, so that the next time the user draws, the values are used
3224
if (valuesToUpdate.iconSize) {
3325
this.edit.preferred.size = valuesToUpdate.iconSize
34-
needIconUrlRefresh = true
3526
}
3627
if (valuesToUpdate.textSize) {
3728
this.edit.preferred.size = valuesToUpdate.textSize
3829
}
3930
if (valuesToUpdate.fillColor) {
4031
this.edit.preferred.color = valuesToUpdate.fillColor
41-
needIconUrlRefresh = true
32+
// refreshing the icon color if present
33+
if (this.feature.current.icon) {
34+
this.feature.current.icon.imageURL = generateIconURL(
35+
this.feature.current.icon,
36+
valuesToUpdate.fillColor
37+
)
38+
}
4239
}
4340
if (valuesToUpdate.textColor) {
4441
this.edit.preferred.color = valuesToUpdate.textColor
@@ -62,18 +59,6 @@ export default function updateCurrentDrawingFeature(
6259
}
6360
}
6461

65-
if (this.feature.current.icon && needIconUrlRefresh) {
66-
const newIconSize: FeatureStyleSize =
67-
valuesToUpdate.iconSize ?? this.feature.current.iconSize ?? MEDIUM
68-
const newIconColor: FeatureStyleColor =
69-
valuesToUpdate.fillColor ?? this.feature.current.fillColor ?? RED
70-
this.feature.current.icon.imageURL = generateIconURL(
71-
this.feature.current.icon,
72-
newIconColor,
73-
newIconSize
74-
)
75-
}
76-
7762
const profileStore = useProfileStore()
7863
// updating the profile feature if the currently drawn feature is the profile feature
7964
if (profileStore.feature?.id === this.feature.current.id) {

packages/viewer/src/utils/__tests__/kmlUtils.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { EditableFeature } from '@/api/features.api'
1212
import { type DrawingIconSet, generateIconURL } from '@/api/icon.api'
1313
import { getServiceKmlBaseUrl } from '@/config/baseUrl.config'
1414
import { fakeIconSets } from '@/utils/__tests__/legacyKmlUtils.spec'
15-
import { BLUE, EXTRA_LARGE } from '@/utils/featureStyleUtils'
15+
import { BLUE } from '@/utils/featureStyleUtils'
1616
import {
1717
getIcon,
1818
getKmlExtent,
@@ -350,7 +350,7 @@ describe('Test KML utils', () => {
350350
'https://fake.image.url/api/icons/sets/default/icons/001-marker@1x-255,0,0.png'
351351
)
352352
})
353-
it('get icon with standard arguments from the set with scale and color', () => {
353+
it('get icon with standard arguments from the set with color', () => {
354354
const icon = getIcon(
355355
{
356356
set: 'default',
@@ -363,8 +363,8 @@ describe('Test KML utils', () => {
363363
expect(icon).toBeDefined()
364364
expect(icon!.name).to.be.equal('001-marker')
365365
expect(icon!.iconSetName).to.be.equal('default')
366-
expect(generateIconURL(icon!, BLUE, EXTRA_LARGE)).to.be.equal(
367-
'https://fake.image.url/api/icons/sets/default/icons/001-marker@1.25x-0,0,255.png'
366+
expect(generateIconURL(icon!, BLUE)).to.be.equal(
367+
'https://fake.image.url/api/icons/sets/default/icons/001-marker@1x-0,0,255.png'
368368
)
369369
})
370370
it('get icon with standard arguments from the babs set', () => {

packages/viewer/src/utils/featureStyleUtils.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,10 @@ export function geoadminStyleFunction(
398398
let image: Icon | undefined
399399
if (editableFeature?.icon) {
400400
image = new Icon({
401-
src: generateIconURL(
402-
editableFeature.icon,
403-
editableFeature.fillColor,
404-
editableFeature.iconSize
405-
),
401+
src: generateIconURL(editableFeature.icon, editableFeature.fillColor),
406402
crossOrigin: 'Anonymous',
407403
anchor: editableFeature.icon.anchor,
404+
scale: editableFeature.iconSize?.iconScale,
408405
})
409406
}
410407
const styles = [

packages/viewer/src/utils/kmlUtils.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { CoordinateSystem, FlatExtent } from '@swissgeo/coordinates'
2+
import { WGS84 } from '@swissgeo/coordinates'
23
import type { KMLLayer } from '@swissgeo/layers'
4+
import { KMLStyle } from '@swissgeo/layers'
35
import type { Geometry } from 'ol/geom'
46
import type { Type as GeometryType } from 'ol/geom/Geometry'
57
import type { Size } from 'ol/size'
6-
7-
import { WGS84 } from '@swissgeo/coordinates'
8-
import { KMLStyle } from '@swissgeo/layers'
98
import log, { LogPreDefinedColor } from '@swissgeo/log'
109
import { kml as kmlToGeoJSON } from '@tmcw/togeojson'
1110
import { booleanValid } from '@turf/turf'
@@ -23,7 +22,6 @@ import IconStyle from 'ol/style/Icon'
2322
import Style from 'ol/style/Style'
2423

2524
import type { EditableFeature } from '@/api/features.api'
26-
2725
import { EditableFeatureTypes, extractOlFeatureCoordinates } from '@/api/features.api'
2826
import { proxifyUrl } from '@/api/file-proxy.api'
2927
import { type DrawingIcon, type DrawingIconSet, generateIconURL } from '@/api/icon.api'
@@ -554,7 +552,7 @@ export function getEditableFeatureFromKmlFeature(
554552
})
555553
const image = style.getImage()
556554
if (image instanceof IconStyle) {
557-
image.setSrc(generateIconURL(icon, fillColor, iconSize))
555+
image.setSrc(generateIconURL(icon, fillColor))
558556
}
559557
}
560558

0 commit comments

Comments
 (0)