Skip to content

Commit f221029

Browse files
committed
Failed attempt at typing geoJsonSTylefromLiterals
1 parent a4a3cee commit f221029

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

packages/layers/src/types/geoJsonStyle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export interface GeoAdminGeoJSONStyle<Type extends GeoAdminGeoJSONStyleType> {
8585
}
8686

8787
interface CommonRangeDefinition {
88-
geomType: string
88+
geomType: GeoAdminGeoJSONStyleType
8989
minResolution?: number
9090
maxResolution?: number
9191
vectorOptions?: GeoAdminGeoJSONVectorOptions

packages/viewer/src/modules/map/components/openlayers/utils/geoJsonStyleFromLiterals.ts

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ import type {
33
GeoAdminGeoJSONRangeDefinition,
44
GeoAdminGeoJSONStyle,
55
GeoAdminGeoJSONVectorOptions,
6+
GeoAdminGeoJSONStyleDefinition,
7+
GeoAdminGeoJSONStyleSingle,
68
} from '@swissgeo/layers'
9+
import type { Options as RegularShapeOptions } from 'ol/style/RegularShape'
10+
711
import log, { LogPreDefinedColor } from '@swissgeo/log'
812
import { isNumber } from '@swissgeo/numbers'
9-
import type { Options as RegularShapeOptions } from 'ol/style/RegularShape'
1013
import {
1114
LineString,
1215
MultiLineString,
@@ -18,6 +21,9 @@ import {
1821
} from 'ol/geom'
1922
import { Circle, Fill, Icon, RegularShape, Stroke, Style, Text } from 'ol/style'
2023

24+
// TODO I don't know enough about styles to do this right now...
25+
26+
2127
// copied and adapted from https://github.com/geoadmin/mf-geoadmin3/blob/master/src/components/StylesFromLiteralsService.js
2228

2329
export function getOlImageStyleForShape(
@@ -152,36 +158,36 @@ function getGeomTypeFromGeometry(olGeometry: SimpleGeometry): string | undefined
152158
}
153159
}
154160

155-
function getLabelProperty(value) {
161+
function getLabelProperty(value: GeoAdminGeoJSONStyle<GeoAdminGeoJSONStyleType>) {
156162
if (value) {
157163
return value.property
158164
}
159165
return null
160166
}
161167

162-
function getLabelTemplate(value) {
168+
function getLabelTemplate(value: GeoAdminGeoJSONStyle<GeoAdminGeoJSONStyleType>) {
163169
if (value) {
164170
return value.template || ''
165171
}
166172
return null
167173
}
168174

169-
function getStyleSpec(value) {
175+
function getStyleSpec(value: GeoAdminGeoJSONRangeDefinition) {
170176
return {
171177
olStyle: getOlStyleFromLiterals(value),
172178
minResolution: getMinResolution(value),
173179
maxResolution: getMaxResolution(value),
174-
labelProperty: getLabelProperty(value.vectorOptions.label),
175-
labelTemplate: getLabelTemplate(value.vectorOptions.label),
180+
labelProperty: getLabelProperty(value.vectorOptions?.label),
181+
labelTemplate: getLabelTemplate(value.vectorOptions?.label),
176182
imageRotationProperty: value.rotation,
177183
}
178184
}
179185

180-
function getMinResolution(value) {
186+
function getMinResolution(value: GeoAdminGeoJSONRangeDefinition) {
181187
return value.minResolution || 0
182188
}
183189

184-
function getMaxResolution(value) {
190+
function getMaxResolution(value: GeoAdminGeoJSONRangeDefinition) {
185191
return value.maxResolution || Infinity
186192
}
187193

@@ -196,21 +202,37 @@ class OlStyleForPropertyValue {
196202
private readonly key: string
197203
private readonly type: GeoAdminGeoJSONStyleType
198204

199-
// TODO: finish Typescript migration of this "mess"
205+
singleStyle: GeoAdminGeoJSONStyle<GeoAdminGeoJSONStyleType> | null
206+
defaultVal: string
207+
defaultStyle: Style
208+
styles: Record<GeoAdminGeoJSONStyleType, Record<string, Style[]>>
209+
200210

201-
constructor(geoadminStyleJson: GeoAdminGeoJSONStyle<GeoAdminGeoJSONStyleType>) {
211+
constructor(geoadminStyleJson: GeoAdminGeoJSONStyleDefinition) {
202212
this.key = geoadminStyleJson.property
203213
this.singleStyle = null
204214
this.defaultVal = 'defaultVal'
205215
this.defaultStyle = new Style()
206216
this.styles = {
207-
point: {},
208-
line: {},
209-
polygon: {},
217+
'single': {},
218+
'unique': {},
219+
'range': {}
210220
}
221+
// this.styles: [key in Language]GeoAdminGeoJSONStyleType //{
222+
// // point: {},
223+
// // line: {},
224+
// // polygon: {},
225+
// //}
211226
this.type = geoadminStyleJson.type
212227

213-
if (geoadminStyleJson.type === 'single') {
228+
const isStyleSingle = (style: GeoAdminGeoJSONStyleDefinition): style is GeoAdminGeoJSONStyleSingle => {
229+
return style.type === 'single'
230+
}
231+
232+
// if (geoadminStyleJson.type === 'single') {
233+
if (isStyleSingle(geoadminStyleJson)) {
234+
// TODO I don't get it. The code tests for 'single', but then getOlStyleFromLiterals
235+
// demands a range. Range is below though!
214236
this.singleStyle = {
215237
olStyle: getOlStyleFromLiterals(geoadminStyleJson),
216238
labelProperty: getLabelProperty(geoadminStyleJson.vectorOptions.label),
@@ -229,38 +251,43 @@ class OlStyleForPropertyValue {
229251
}
230252
}
231253

232-
pushOrInitialize_(geomType, key, styleSpec) {
254+
pushOrInitialize_(geomType: GeoAdminGeoJSONStyleType, key: string, styleSpec) {
233255
// Happens when styling is only resolution dependent (unique type only)
234256
if (key === undefined) {
235257
key = this.defaultVal
236258
}
237-
if (!this.styles[geomType][key]) {
238-
this.styles[geomType][key] = [styleSpec]
259+
260+
if (key in this.styles[geomType]) {
261+
this.styles[geomType][key]?.push(styleSpec)
239262
} else {
240-
this.styles[geomType][key].push(styleSpec)
263+
this.styles[geomType][key] = [styleSpec]
241264
}
242265
}
243266

244-
findOlStyleInRange_(value, geomType) {
245-
let olStyle = null
267+
findOlStyleInRange_(value: number, geomType: GeoAdminGeoJSONStyleType) {
268+
let olStyle: Style | null = null
269+
246270
Object.keys(this.styles[geomType]).forEach((range) => {
247271
const limits = range.split(',')
248272
const min = parseFloat(limits[0].replace(/\s/g, ''))
249273
const max = parseFloat(limits[1].replace(/\s/g, ''))
274+
250275
if (!olStyle && value >= min && value < max) {
251-
olStyle = this.styles[geomType][range]
276+
if (this.styles[geomType][range]) {
277+
olStyle = this.styles[geomType][range] // TODO this is a list. what should go here?
278+
}
252279
}
253280
})
254281
return olStyle
255282
}
256283

257-
getOlStyleForResolution_(olStyles, resolution) {
284+
getOlStyleForResolution_(olStyles: OLBasicStyles[], resolution: number) {
258285
return olStyles.find(
259286
(style) => style.minResolution <= resolution && style.maxResolution > resolution
260287
)
261288
}
262289

263-
log_(value, id) {
290+
log_(value: string, id: string) {
264291
const logValue = value === '' ? '<empty string>' : value
265292
log.debug({
266293
title: 'GeoJSON style from litrals',

0 commit comments

Comments
 (0)