Skip to content

Commit 465be3f

Browse files
committed
Fixed ImageWMS source rendering
1 parent a93df5a commit 465be3f

File tree

1 file changed

+46
-13
lines changed

1 file changed

+46
-13
lines changed

src/components/gtt-client.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Map, Feature, View, Geolocation } from 'ol'
44
import 'ol-ext/filter/Base'
55
import { Geometry, Point } from 'ol/geom'
66
import { GeoJSON, WKT } from 'ol/format'
7-
import { Layer, Tile } from 'ol/layer'
7+
import { Layer, Tile, Image } from 'ol/layer'
88
import VectorLayer from 'ol/layer/Vector'
99
import { OSM, XYZ, TileWMS, ImageWMS } from 'ol/source'
1010
import { Style, Fill, Stroke, Circle } from 'ol/style'
@@ -38,6 +38,9 @@ import { position } from 'ol-ext/control/control'
3838
import { ResizeObserver } from '@juggle/resize-observer'
3939
import VectorSource from 'ol/source/Vector'
4040
import { FeatureLike } from 'ol/Feature'
41+
import TileSource from 'ol/source/Tile'
42+
import ImageSource from 'ol/source/Image'
43+
import { Options as ImageWMSOptions } from 'ol/source/ImageWMS'
4144

4245
interface GttClientOption {
4346
target: HTMLDivElement | null
@@ -56,6 +59,16 @@ interface FilterOption {
5659
distance: boolean
5760
}
5861

62+
interface TileLayerSource {
63+
layer: typeof Tile
64+
source: typeof OSM | typeof XYZ
65+
}
66+
67+
interface ImageLayerSource {
68+
layer: typeof Image
69+
source: typeof ImageWMS
70+
}
71+
5972
export class GttClient {
6073
readonly map: Map
6174
maps: Array<Map>
@@ -148,27 +161,47 @@ export class GttClient {
148161
const layers = JSON.parse(this.contents.layers) as [LayerObject]
149162
layers.forEach((layer) => {
150163
const s = layer.type.split('.')
151-
const tileSource = getTileSource(s[1], s[2])
152-
if (tileSource) {
153-
const l = new Tile({
164+
const layerSource = getLayerSource(s[1], s[2])
165+
const tileLayerSource = layerSource as TileLayerSource
166+
if (tileLayerSource) {
167+
const l = new (tileLayerSource.layer)({
168+
visible: false,
169+
source: new (tileLayerSource.source)(layer.options)
170+
})
171+
172+
l.set('lid', layer.id)
173+
l.set('title', layer.name)
174+
l.set('baseLayer', layer.baselayer)
175+
if( layer.baselayer ) {
176+
l.on('change:visible', e => {
177+
const target = e.target as Tile<TileSource>
178+
if (target.getVisible()) {
179+
const lid = target.get('lid')
180+
document.cookie = `_redmine_gtt_basemap=${lid};path=/`
181+
}
182+
})
183+
}
184+
this.layerArray.push(l)
185+
} else if (layerSource as ImageLayerSource) {
186+
const imageLayerSource = layerSource as ImageLayerSource
187+
const l = new (imageLayerSource.layer)({
154188
visible: false,
155-
source: new (tileSource)(layer.options)
156-
})
189+
source: new (imageLayerSource.source)(layer.options as ImageWMSOptions)
190+
})
157191

158192
l.set('lid', layer.id)
159193
l.set('title', layer.name)
160194
l.set('baseLayer', layer.baselayer)
161195
if( layer.baselayer ) {
162196
l.on('change:visible', e => {
163-
const target = e.target as Tile<XYZ>
197+
const target = e.target as Image<ImageSource>
164198
if (target.getVisible()) {
165199
const lid = target.get('lid')
166200
document.cookie = `_redmine_gtt_basemap=${lid};path=/`
167201
}
168202
})
169203
}
170204
this.layerArray.push(l)
171-
//this.map.addLayer(l)
172205
}
173206
}, this)
174207

@@ -1228,16 +1261,16 @@ export class GttClient {
12281261
}
12291262
}
12301263

1231-
const getTileSource = (source: string, class_name: string): any => {
1264+
const getLayerSource = (source: string, class_name: string): TileLayerSource | ImageLayerSource | undefined => {
12321265
if (source === 'source') {
12331266
if (class_name === 'OSM') {
1234-
return OSM
1267+
return { layer: Tile, source: OSM }
12351268
} else if (class_name === 'XYZ') {
1236-
return XYZ
1269+
return { layer: Tile, source: XYZ }
12371270
} else if (class_name === 'TileWMS') {
1238-
return TileWMS
1271+
return { layer: Tile, source: TileWMS }
12391272
} else if (class_name === 'ImageWMS') {
1240-
return ImageWMS
1273+
return { layer: Image, source: ImageWMS }
12411274
}
12421275
}
12431276
return undefined

0 commit comments

Comments
 (0)