Skip to content

Commit 80bc658

Browse files
authored
Store map position in schema (#34)
* Store map position in schema * Missing default values for position * Missing map location in test file * Mark properties as required * Iterate * Add new example * Fix test file
1 parent b8d9511 commit 80bc658

File tree

8 files changed

+165
-10
lines changed

8 files changed

+165
-10
lines changed

examples/france_hiking.jGIS

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"layers": {
3+
"7db81237-a579-4daa-938f-5e61fdfb17e7": {
4+
"name": "NASAGIBS.ModisTerraTrueColorCR Layer",
5+
"visible": true,
6+
"parameters": {
7+
"source": "52252f5d-3cb7-45a8-a724-5793bf9950ec",
8+
"opacity": 0.3
9+
},
10+
"type": "RasterLayer"
11+
},
12+
"0bfee293-9e2f-4434-8c5a-c90d19836bab": {
13+
"name": "WaymarkedTrails.hiking Layer",
14+
"type": "RasterLayer",
15+
"parameters": {
16+
"opacity": 0.6,
17+
"source": "82691e55-f9e2-43be-8a07-3ae0409af7b4"
18+
},
19+
"visible": true
20+
},
21+
"4a0703b3-ed56-4158-8a2e-e008c3d0fee2": {
22+
"name": "OpenStreetMap.Mapnik Layer",
23+
"visible": true,
24+
"type": "RasterLayer",
25+
"parameters": {
26+
"source": "60da082e-8b70-4fa2-b2f0-48524468fea0"
27+
}
28+
}
29+
},
30+
"sources": {
31+
"60da082e-8b70-4fa2-b2f0-48524468fea0": {
32+
"type": "RasterSource",
33+
"parameters": {
34+
"url": "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
35+
"minZoom": 0.0,
36+
"maxZoom": 19.0,
37+
"urlParameters": {},
38+
"provider": "OpenStreetMap",
39+
"attribution": "(C) OpenStreetMap contributors"
40+
},
41+
"name": "OpenStreetMap.Mapnik"
42+
},
43+
"52252f5d-3cb7-45a8-a724-5793bf9950ec": {
44+
"parameters": {
45+
"urlParameters": {
46+
"time": "2024-07-07",
47+
"tilematrixset": "GoogleMapsCompatible_Level",
48+
"format": "jpg",
49+
"variant": "MODIS_Terra_CorrectedReflectance_TrueColor"
50+
},
51+
"url": "https://map1.vis.earthdata.nasa.gov/wmts-webmerc/{variant}/default/{time}/{tilematrixset}{max_zoom}/{z}/{y}/{x}.{format}",
52+
"minZoom": 1.0,
53+
"maxZoom": 9.0,
54+
"attribution": "Imagery provided by services from the Global Imagery Browse Services (GIBS), operated by the NASA/GSFC/Earth Science Data and Information System (ESDIS) with funding provided by NASA/HQ.",
55+
"provider": "NASAGIBS"
56+
},
57+
"name": "NASAGIBS.ModisTerraTrueColorCR",
58+
"type": "RasterSource"
59+
},
60+
"82691e55-f9e2-43be-8a07-3ae0409af7b4": {
61+
"name": "WaymarkedTrails.hiking",
62+
"type": "RasterSource",
63+
"parameters": {
64+
"minZoom": 0.0,
65+
"url": "https://tile.waymarkedtrails.org/{variant}/{z}/{x}/{y}.png",
66+
"attribution": "Map data: (C) OpenStreetMap contributors | Map style: (C) waymarkedtrails.org (CC-BY-SA)",
67+
"maxZoom": 18.0,
68+
"provider": "WaymarkedTrails",
69+
"urlParameters": {
70+
"variant": "hiking"
71+
}
72+
}
73+
}
74+
},
75+
"options": {
76+
"latitude": 46.623742146769416,
77+
"zoom": 4.947275971927249,
78+
"longitude": 1.6082511087276998
79+
},
80+
"layerTree": [
81+
"4a0703b3-ed56-4158-8a2e-e008c3d0fee2",
82+
"7db81237-a579-4daa-938f-5e61fdfb17e7",
83+
"0bfee293-9e2f-4434-8c5a-c90d19836bab"
84+
]
85+
}

examples/test.jGIS

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@
5454
}
5555
}
5656
},
57-
"options": {},
57+
"options": {
58+
"latitude": 0,
59+
"longitude": 0,
60+
"zoom": 0
61+
},
5862
"layerTree": [
5963
"2467576f-b527-4cb7-998d-fa1d056fb8a1",
6064
{
@@ -70,4 +74,4 @@
7074
"name": "level 1 group"
7175
}
7276
]
73-
}
77+
}

packages/base/src/mainview/mainview.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ export class MainView extends React.Component<IProps, IStates> {
100100
container: this.divRef.current
101101
});
102102

103+
this._Map.on('zoomend', () => {
104+
if (!this._initializedPosition) {
105+
return;
106+
}
107+
108+
const zoom = this._Map.getZoom();
109+
this._model.setOptions({ ...this._model.getOptions(), zoom });
110+
});
111+
112+
this._Map.on('moveend', () => {
113+
if (!this._initializedPosition) {
114+
return;
115+
}
116+
117+
const center = this._Map.getCenter();
118+
this._model.setOptions({
119+
...this._model.getOptions(),
120+
latitude: center.lat,
121+
longitude: center.lng
122+
});
123+
});
124+
103125
this.setState(old => ({ ...old, loading: false }));
104126
}
105127
};
@@ -386,7 +408,21 @@ export class MainView extends React.Component<IProps, IStates> {
386408
sender: IJupyterGISDoc,
387409
change: MapChange
388410
): void {
389-
// TODO SOMETHING
411+
if (!this._initializedPosition) {
412+
const options = this._model.getOptions();
413+
414+
// It is important to call setZoom first, otherwise maplibre does set the center properly
415+
this._Map.setZoom(options.zoom || 0);
416+
this._Map.setCenter(
417+
(options.longitude &&
418+
options.latitude && {
419+
lng: options.longitude,
420+
lat: options.latitude
421+
}) || [0, 0]
422+
);
423+
424+
this._initializedPosition = true;
425+
}
390426
}
391427

392428
private _onViewChanged(
@@ -486,6 +522,7 @@ export class MainView extends React.Component<IProps, IStates> {
486522
);
487523
}
488524

525+
private _initializedPosition = false;
489526
private divRef = React.createRef<HTMLDivElement>(); // Reference of render div
490527

491528
private _Map: MapLibre.Map;

packages/schema/src/doc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Delta, MapChange, YDocument } from '@jupyter/ydoc';
2-
import { JSONExt, JSONObject } from '@lumino/coreutils';
2+
import { JSONExt } from '@lumino/coreutils';
33
import { ISignal, Signal } from '@lumino/signaling';
44
import * as Y from 'yjs';
55

@@ -106,8 +106,8 @@ export class JupyterGISDoc
106106
});
107107
}
108108

109-
get options(): JSONObject {
110-
return JSONExt.deepCopy(this._options.toJSON());
109+
get options(): IJGISOptions {
110+
return JSONExt.deepCopy(this._options.toJSON()) as IJGISOptions;
111111
}
112112

113113
get layersChanged(): ISignal<IJupyterGISDoc, IJGISLayerDocChange> {

packages/schema/src/interfaces.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ export interface IJupyterGISModel extends DocumentRegistry.IModel {
146146
groupName?: string,
147147
position?: number
148148
): void;
149+
getOptions(): IJGISOptions;
150+
setOptions(value: IJGISOptions): void;
149151

150152
syncSelected(value: { [key: string]: ISelection }, emitter?: string): void;
151153
setUserToFollow(userId?: number): void;

packages/schema/src/model.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
IJGISLayerGroup,
1414
IJGISLayerTree,
1515
IJGISSource,
16-
IJGISSources
16+
IJGISSources,
17+
IJGISOptions
1718
} from './_interface/jgis';
1819
import { JupyterGISDoc } from './doc';
1920
import {
@@ -164,7 +165,11 @@ export class JupyterGISModel implements IJupyterGISModel {
164165
this.sharedModel.sources = jsonData.sources ?? {};
165166
this.sharedModel.layers = jsonData.layers ?? {};
166167
this.sharedModel.layerTree = jsonData.layerTree ?? [];
167-
this.sharedModel.options = jsonData.options ?? {};
168+
this.sharedModel.options = jsonData.options ?? {
169+
latitude: 0,
170+
longitude: 0,
171+
zoom: 0
172+
};
168173
});
169174
this.dirty = true;
170175
}
@@ -274,6 +279,14 @@ export class JupyterGISModel implements IJupyterGISModel {
274279
this._addLayerTreeItem(id, groupName, position);
275280
}
276281

282+
setOptions(value: IJGISOptions) {
283+
this._sharedModel.options = value;
284+
}
285+
286+
getOptions(): IJGISOptions {
287+
return this._sharedModel.options;
288+
}
289+
277290
syncSelected(value: { [key: string]: ISelection }, emitter?: string): void {
278291
this.sharedModel.awareness.setLocalStateField('selected', {
279292
value,

packages/schema/src/schema/jgis.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,22 @@
115115
"title": "IJGISOptions",
116116
"type": "object",
117117
"default": {},
118+
"required": ["latitude", "longitude", "zoom"],
118119
"additionalProperties": false,
119-
"properties": {}
120+
"properties": {
121+
"latitude": {
122+
"type": "number",
123+
"default": 0
124+
},
125+
"longitude": {
126+
"type": "number",
127+
"default": 0
128+
},
129+
"zoom": {
130+
"type": "number",
131+
"default": 0
132+
}
133+
}
120134
},
121135
"jGISLayerItem": {
122136
"title": "IJGISLayerItem",

python/jupytergis_core/src/jgisplugin/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const activate = (
111111
format: 'text',
112112
size: undefined,
113113
content:
114-
'{\n\t"layers": {},\n\t"sources": {},\n\t"options": {},\n\t"layerTree": []\n}'
114+
'{\n\t"layers": {},\n\t"sources": {},\n\t"options": {"latitude": 0, "longitude": 0, "zoom": 0},\n\t"layerTree": []\n}'
115115
});
116116

117117
// Open the newly created file with the 'Editor'

0 commit comments

Comments
 (0)