11import { 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-
82import { defaults as defaultControls } from 'ol/control/defaults.js' ;
9- // import { MapOptions } from "ol/Map";
10-
113import GeoJSON from "ol/format/GeoJSON" ;
12-
134import Overlay from "ol/Overlay" ;
14-
5+ import { fromLonLat } from "ol/proj" ;
156import { JSONConverter } from "./json" ;
7+ import { addTooltip2 } from "./tooltip2" ;
168
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
2110import type Layer from "ol/layer/Layer" ;
2211import type Control from "ol/control/Control" ;
23-
2412import type VectorSource from "ol/source/Vector" ;
25-
2613import type VectorLayer from "ol/layer/Vector" ;
2714import 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 "." ;
3315import type { Coordinate } from "ol/coordinate" ;
16+ import type { MyMapOptions } from "." ;
3417
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- //
5018type Metadata = {
5119 layers : any [ ] ;
5220 controls : any [ ] ;
5321} ;
5422
23+ // --- Constants
24+ // TODO: Move to constants
5525const TYPE_IDENTIFIER = "@@type" ;
5626const GEOJSON_IDENTIFIER = "@@geojson" ;
5727
5828const jsonConverter = new JSONConverter ( ) ;
5929
30+ // --- Helpers
6031function parseViewDef ( viewDef : JSONDef ) : View {
6132 const view = jsonConverter . parse ( viewDef ) as View ;
6233 const center = view . getCenter ( ) ;
@@ -72,68 +43,30 @@ function parseViewDef(viewDef: JSONDef): View {
7243
7344function parseLayerDef ( layerDef : JSONDef ) : Layer {
7445 const layer = jsonConverter . parse ( layerDef ) ;
46+ console . log ( "layerDef" , layerDef ) ;
7547 layer . set ( "id" , layerDef . id ) ;
76- addGeojsonFeatures ( layer , layerDef [ GEOJSON_IDENTIFIER ] ) ;
48+ addGeojsonFeatures ( layer , layerDef [ "source" ] [ GEOJSON_IDENTIFIER ] ) ;
7749 return layer ;
7850}
7951
8052function 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+ }
8758}
8859
89-
60+ // --- Base class
9061export default class MapWidget {
9162 _container : HTMLElement ;
9263 _map : Map ;
9364 _metadata : Metadata = { layers : [ ] , controls : [ ] } ;
9465
9566 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- */
13667 const view = parseViewDef ( mapOptions . view ) ;
68+ let baseControls : Control [ ] = [ ] ;
69+ let baseLayers : Layer [ ] = [ ] ;
13770
13871 this . _container = mapElement ;
13972 this . _map = new Map ( {
@@ -154,19 +87,9 @@ export default class MapWidget {
15487 }
15588 }
15689
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 ;
16892 }
169- */
17093
17194 getMap ( ) : Map {
17295 return this . _map ;
@@ -188,32 +111,7 @@ export default class MapWidget {
188111
189112 }
190113
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
217115 getLayer ( layerId : string ) : Layer | undefined {
218116 for ( let layer of this . _map . getLayers ( ) . getArray ( ) ) {
219117 if ( layer . get ( "id" ) === layerId )
@@ -240,7 +138,23 @@ export default class MapWidget {
240138 }
241139 }
242140
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
244158 getControl ( controlId : string ) : Control | undefined {
245159 for ( let control of this . _map . getControls ( ) . getArray ( ) ) {
246160 if ( control . get ( "id" ) === controlId )
@@ -268,32 +182,6 @@ export default class MapWidget {
268182 }
269183 }
270184
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-
297185 // TODO: Test only at the moment
298186 addOverlay ( position : Coordinate | undefined ) : void {
299187 const el = document . createElement ( "div" ) ;
0 commit comments