1
1
import { Map , View } from "ol" ;
2
- // import type { ViewOptions } from "ol/View";
3
-
4
- // import TileLayer from "ol/layer/Tile";
5
-
6
- // import OSM from "ol/source/OSM";
7
-
8
2
import { defaults as defaultControls } from 'ol/control/defaults.js' ;
9
- // import { MapOptions } from "ol/Map";
10
-
11
3
import GeoJSON from "ol/format/GeoJSON" ;
12
-
13
4
import Overlay from "ol/Overlay" ;
14
-
5
+ import { fromLonLat } from "ol/proj" ;
15
6
import { JSONConverter } from "./json" ;
7
+ import { addTooltip2 } from "./tooltip2" ;
16
8
17
- // import { addTooltipTo } from "./tooltip"; // Uses an overlay
18
- import { addTooltip2 } from "./tooltip2" ; // Uses custom div container
19
-
20
- // import { populatedPlacesLayer } from "./test-json-converter";
9
+ // --- Types
21
10
import type Layer from "ol/layer/Layer" ;
22
11
import type Control from "ol/control/Control" ;
23
-
24
12
import type VectorSource from "ol/source/Vector" ;
25
-
26
13
import type VectorLayer from "ol/layer/Vector" ;
27
14
import type WebGLVectorLayer from "ol/layer/WebGLVector" ;
28
-
29
- import { transform as transformProj , fromLonLat } from "ol/proj" ;
30
-
31
- // My types
32
- import type { MyMapOptions } from "." ;
33
15
import type { Coordinate } from "ol/coordinate" ;
16
+ import type { MyMapOptions } from "." ;
34
17
35
- // ...
36
- /*
37
- type LayerStore = {
38
- [key: string]: Layer;
39
- };
40
- */
41
-
42
- //
43
- /*
44
- type ControlStore = {
45
- [key: string]: Control;
46
- };
47
- */
48
-
49
- //
50
18
type Metadata = {
51
19
layers : any [ ] ;
52
20
controls : any [ ] ;
53
21
} ;
54
22
23
+ // --- Constants
24
+ // TODO: Move to constants
55
25
const TYPE_IDENTIFIER = "@@type" ;
56
26
const GEOJSON_IDENTIFIER = "@@geojson" ;
57
27
58
28
const jsonConverter = new JSONConverter ( ) ;
59
29
30
+ // --- Helpers
60
31
function parseViewDef ( viewDef : JSONDef ) : View {
61
32
const view = jsonConverter . parse ( viewDef ) as View ;
62
33
const center = view . getCenter ( ) ;
@@ -72,68 +43,30 @@ function parseViewDef(viewDef: JSONDef): View {
72
43
73
44
function parseLayerDef ( layerDef : JSONDef ) : Layer {
74
45
const layer = jsonConverter . parse ( layerDef ) ;
46
+ console . log ( "layerDef" , layerDef ) ;
75
47
layer . set ( "id" , layerDef . id ) ;
76
- addGeojsonFeatures ( layer , layerDef [ GEOJSON_IDENTIFIER ] ) ;
48
+ addGeojsonFeatures ( layer , layerDef [ "source" ] [ GEOJSON_IDENTIFIER ] ) ;
77
49
return layer ;
78
50
}
79
51
80
52
function addGeojsonFeatures ( layer : Layer , features : any ) : void {
81
- if ( features === undefined )
82
- return ;
83
-
84
- const source = layer . getSource ( ) as VectorSource ;
85
- source . addFeatures ( new GeoJSON ( ) . readFeatures ( features ) ) ;
86
- console . log ( "geojson features added" , features ) ;
53
+ if ( features ) {
54
+ const source = layer . getSource ( ) as VectorSource ;
55
+ source . addFeatures ( new GeoJSON ( ) . readFeatures ( features ) ) ;
56
+ console . log ( "geojson features added" , features ) ;
57
+ }
87
58
}
88
59
89
-
60
+ // --- Base class
90
61
export default class MapWidget {
91
62
_container : HTMLElement ;
92
63
_map : Map ;
93
64
_metadata : Metadata = { layers : [ ] , controls : [ ] } ;
94
65
95
66
constructor ( mapElement : HTMLElement , mapOptions : MyMapOptions ) {
96
- let baseLayers : Layer [ ] = [ ] // defaultLayers;
97
- /*
98
- if (mapOptions.layers !== undefined) {
99
- for (let layerJSONDef of mapOptions.layers) {
100
-
101
- // TODO: Duplicated code, use 'addLayer' after map was created instead
102
- const layer = jsonConverter.parse(layerJSONDef);
103
-
104
- // if (layerJSONDef.source["@@geojson "] !== undefined)
105
- this.addGeoJSONToSource(layer, layerJSONDef.source["@@geojson"]);
106
-
107
- baseLayers.push(layer);
108
- this._layerStore[layerJSONDef.id] = layer;
109
- }
110
- }
111
- */
112
-
113
- let baseControls : Control [ ] = [ ] ;
114
- // TODO: Use 'addControls' after map was created instead
115
- /*
116
- if (mapOptions.controls !== undefined) {
117
- for (let controlJSONDef of mapOptions.controls) {
118
- const control = jsonConverter.parse(controlJSONDef);
119
- baseControls.push(control);
120
- this._controlStore[controlJSONDef.id] = control;
121
- }
122
- }
123
- */
124
-
125
- // TODO: Move to func 'parseView'
126
- /*
127
- const view = jsonConverter.parse(mapOptions.view) as View;
128
- const center = view.getCenter();
129
- console.log("center", center)
130
- if (center && view.getProjection().getCode() !== "EPSG:4326") {
131
- const centerTransformed = fromLonLat(center);
132
- console.log("centerTransformed", centerTransformed);
133
- view.setCenter(centerTransformed);
134
- }
135
- */
136
67
const view = parseViewDef ( mapOptions . view ) ;
68
+ let baseControls : Control [ ] = [ ] ;
69
+ let baseLayers : Layer [ ] = [ ] ;
137
70
138
71
this . _container = mapElement ;
139
72
this . _map = new Map ( {
@@ -154,19 +87,9 @@ export default class MapWidget {
154
87
}
155
88
}
156
89
157
- // TODO: Obsolete
158
- /*
159
- transformCenter(viewOptions: ViewOptions): ViewOptions {
160
- const center = viewOptions.center;
161
- if (center && viewOptions.projection !== "EPSG:4326") {
162
- // console.log("center before", center);
163
- viewOptions.center = fromLonLat(center);
164
- // viewOptions.center = transformProj(center, "EPSG:4326", viewOptions.projection || "EPSG:3857");
165
- // console.log("center after", viewOptions.center);
166
- }
167
- return viewOptions;
90
+ getElement ( ) : HTMLElement {
91
+ return this . _container ;
168
92
}
169
- */
170
93
171
94
getMap ( ) : Map {
172
95
return this . _map ;
@@ -188,32 +111,7 @@ export default class MapWidget {
188
111
189
112
}
190
113
191
- /*
192
- addGeoJSONToSource(layer: Layer, geoJSONObject: any): void {
193
- if (geoJSONObject === undefined)
194
- return;
195
-
196
- const source = layer.getSource() as VectorSource;
197
- source.addFeatures(new GeoJSON().readFeatures(geoJSONObject));
198
- console.log("geojsonObject added to VectorSource", geoJSONObject);
199
- }
200
- */
201
-
202
- /*
203
- addLayerOld(layerJSONDef: JSONDef): void {
204
- const layer = jsonConverter.parse(layerJSONDef);
205
- layer.set("id", layerJSONDef.id);
206
-
207
- //if (layerJSONDef.source["@@geojson "] !== undefined)
208
- this.addGeoJSONToSource(layer, layerJSONDef.source["@@geojson"]);
209
-
210
- this._map.addLayer(layer);
211
- this._layerStore[layerJSONDef.id] = layer;
212
- console.log("layerStore", this._layerStore);
213
- }
214
- */
215
-
216
- // --- Layers
114
+ // --- Layer methods
217
115
getLayer ( layerId : string ) : Layer | undefined {
218
116
for ( let layer of this . _map . getLayers ( ) . getArray ( ) ) {
219
117
if ( layer . get ( "id" ) === layerId )
@@ -240,7 +138,23 @@ export default class MapWidget {
240
138
}
241
139
}
242
140
243
- // --- Controls
141
+ setLayerStyle ( layerId : string , style : any ) : void {
142
+ const layer = this . getLayer ( layerId ) as VectorLayer | WebGLVectorLayer ;
143
+ if ( layer ) {
144
+ console . log ( "set layer style" , layerId , style ) ;
145
+ layer . setStyle ( style )
146
+ }
147
+ }
148
+
149
+ applyCallToLayer ( layerId : string , call : OLAnyWidgetCall ) : void {
150
+ console . log ( "run layer method" , layerId ) ;
151
+ const layer = this . getLayer ( layerId ) ;
152
+
153
+ // @ts -expect-error
154
+ layer [ call . method ] ( ...call . args )
155
+ }
156
+
157
+ // --- Control methods
244
158
getControl ( controlId : string ) : Control | undefined {
245
159
for ( let control of this . _map . getControls ( ) . getArray ( ) ) {
246
160
if ( control . get ( "id" ) === controlId )
@@ -268,32 +182,6 @@ export default class MapWidget {
268
182
}
269
183
}
270
184
271
- // --- Misc
272
- setLayerStyle ( layerId : string , style : any ) : void {
273
- const layer = this . getLayer ( layerId ) as VectorLayer | WebGLVectorLayer ;
274
- if ( layer ) {
275
- console . log ( "set layer style" , layerId , style ) ;
276
- layer . setStyle ( style )
277
- }
278
- }
279
-
280
- applyCallToLayer ( layerId : string , call : OLAnyWidgetCall ) : void {
281
- console . log ( "run layer method" , layerId ) ;
282
- const layer = this . getLayer ( layerId ) ;
283
-
284
- // @ts -expect-error
285
- layer [ call . method ] ( ...call . args )
286
- }
287
-
288
- // TODO: Remove
289
- testJSONDef ( jsonDef : JSONDef ) : any {
290
- return jsonConverter . parse ( jsonDef ) ;
291
- }
292
-
293
- // TODO: Remove
294
- debugData ( data : any ) : void {
295
- }
296
-
297
185
// TODO: Test only at the moment
298
186
addOverlay ( position : Coordinate | undefined ) : void {
299
187
const el = document . createElement ( "div" ) ;
0 commit comments