Skip to content

Commit 270f883

Browse files
author
Stefan Kuethe
committed
Fix geojson source
1 parent 30f0741 commit 270f883

File tree

10 files changed

+115
-272
lines changed

10 files changed

+115
-272
lines changed

notebooks/layers/Untitled.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
{
5353
"data": {
5454
"application/vnd.jupyter.widget-view+json": {
55-
"model_id": "f08204b538294150862d3323346f4dec",
55+
"model_id": "ee4ebb74b08d4764ba14f4b4330800a8",
5656
"version_major": 2,
5757
"version_minor": 1
5858
},

src/openlayers/js/openlayers.anywidget.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/openlayers/js/openlayers.standalone.js

Lines changed: 42 additions & 42 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcjs/ipywidget-ts/anywidget.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { type AnyModel } from "@anywidget/types";
2-
// import { transform as transformProj } from "ol/proj";
3-
import { parseClickEvent } from "./utils";
4-
5-
// import "./style.css";
61
import "ol/ol.css";
72

83
import MapWidget from "./map";
4+
import { parseClickEvent } from "./utils";
95

6+
// --- Types
7+
import type { AnyModel } from "@anywidget/types";
8+
9+
// --- Main function
1010
function render({ model, el }: { model: AnyModel; el: HTMLElement }): void {
1111
console.log("Welcome to ol-anywidget", el);
1212

@@ -51,10 +51,6 @@ function render({ model, el }: { model: AnyModel; el: HTMLElement }): void {
5151
}
5252
});
5353

54-
// ...
55-
const debugData = model.get("debug_data");
56-
mapWidget.debugData(debugData);
57-
5854
el.appendChild(mapElement);
5955
}
6056

srcjs/ipywidget-ts/controls.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import FullScreenControl from 'ol/control/FullScreen.js';
33
import ZoomSliderControl from 'ol/control/ZoomSlider.js';
44
import MousePositionControl from 'ol/control/MousePosition.js';
55
import OverviewMapControl from 'ol/control/OverviewMap.js';
6+
67
import { InfoBox } from './custom-controls';
78

89
const controlCatalog: ControlCatalog = {
@@ -14,10 +15,4 @@ const controlCatalog: ControlCatalog = {
1415
InfoBox: InfoBox
1516
};
1617

17-
/*
18-
function newControl(type: any, options: any): any {
19-
return new controlCatalog[type](options);
20-
}
21-
*/
22-
23-
export { controlCatalog }
18+
export { controlCatalog };

srcjs/ipywidget-ts/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import type { ViewOptions } from "ol/View";
22

33
declare type MyMapOptions = {
4-
// TODO: Pass View JSONDef instead of ViewOptions only
5-
// viewOptions: ViewOptions | undefined;
64
view: JSONDef;
75
layers: JSONDef[] | undefined;
86
controls: JSONDef[] | undefined;

srcjs/ipywidget-ts/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class JSONConverter {
5050
parse(jsonDef: JSONDef): any {
5151
// console.log(this._catalog);
5252
const parsedOptions = this.parseOptions(jsonDef);
53-
console.log("parsedOptions", parsedOptions);
53+
console.log("parsed options", parsedOptions);
5454
console.log("type detected", jsonDef["@@type"]);
5555
return new this._catalog[jsonDef["@@type"]](parsedOptions);
5656
}

srcjs/ipywidget-ts/layers.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import TileLayer from "ol/layer/Tile";
22
import VectorLayer from "ol/layer/Vector";
33
import WebGLVectorLayer from 'ol/layer/WebGLVector.js';
44
import WebGLTileLayer from 'ol/layer/WebGLTile.js';
5-
// import BaseLayer from "ol/layer/Base";
6-
7-
// import { newSource } from "./sources";
85

96
const layerCatalog: LayerCatalog = {
107
TileLayer: TileLayer,
@@ -13,13 +10,4 @@ const layerCatalog: LayerCatalog = {
1310
WebGLTileLayer: WebGLTileLayer
1411
};
1512

16-
/*
17-
function newLayer(type: LayerCatalogKey, options: LayerOptions): any {
18-
const sourceDef = options.source;
19-
options.source = newSource(sourceDef.type, sourceDef.options);
20-
const layer = new layerCatalog[type](options)
21-
return layer;
22-
}
23-
*/
24-
2513
export { layerCatalog };

srcjs/ipywidget-ts/map.ts

Lines changed: 37 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,33 @@
11
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-
82
import { defaults as defaultControls } from 'ol/control/defaults.js';
9-
// import { MapOptions } from "ol/Map";
10-
113
import GeoJSON from "ol/format/GeoJSON";
12-
134
import Overlay from "ol/Overlay";
14-
5+
import { fromLonLat } from "ol/proj";
156
import { 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
2110
import type Layer from "ol/layer/Layer";
2211
import type Control from "ol/control/Control";
23-
2412
import type VectorSource from "ol/source/Vector";
25-
2613
import type VectorLayer from "ol/layer/Vector";
2714
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 ".";
3315
import 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-
//
5018
type Metadata = {
5119
layers: any[];
5220
controls: any[];
5321
};
5422

23+
// --- Constants
24+
// TODO: Move to constants
5525
const TYPE_IDENTIFIER = "@@type";
5626
const GEOJSON_IDENTIFIER = "@@geojson";
5727

5828
const jsonConverter = new JSONConverter();
5929

30+
// --- Helpers
6031
function 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

7344
function 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

8052
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+
}
8758
}
8859

89-
60+
// --- Base class
9061
export 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");

srcjs/ipywidget-ts/sources.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import VectorSource from "ol/source/Vector";
33
import GeoTIFFSource from 'ol/source/GeoTIFF.js';
44
import ImageTileSource from "ol/source/ImageTile";
55

6-
// import { GeoJSON } from "ol/format";
7-
8-
// import { OSM } from "ol/source";
9-
106
const sourceCatalog: SourceCatalog = {
117
OSM: OSM,
128
VectorSource: VectorSource,
@@ -15,22 +11,4 @@ const sourceCatalog: SourceCatalog = {
1511
ImageTileSource: ImageTileSource
1612
};
1713

18-
/*
19-
function parseSourceOptions(options: SourceOptions): SourceOptions {
20-
if (options.format === "geojson") {
21-
options.format = new GeoJSON()
22-
}
23-
24-
return options;
25-
}
26-
*/
27-
28-
/*
29-
function newSource(type: SourceCatalogKey, options?: SourceOptions): any {
30-
options = options ? parseSourceOptions(options) : options;
31-
const source = new sourceCatalog[type](options)
32-
return source;
33-
}
34-
*/
35-
3614
export { sourceCatalog };

0 commit comments

Comments
 (0)