Skip to content

Commit 7450f14

Browse files
committed
Replace leaflet with mapbox-gl
1 parent ae9ad4c commit 7450f14

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

packages/geojson-extension/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
"@jupyterlab/rendermime-interfaces": "^1.0.3",
2525
"@phosphor/messaging": "^1.2.2",
2626
"@phosphor/widgets": "^1.5.0",
27-
"leaflet": "^1.2.0"
27+
"leaflet": "^1.2.0",
28+
"mapbox-gl": "^0.44.2"
2829
},
2930
"devDependencies": {
30-
"@types/leaflet": "^1.2.0",
31+
"@types/mapbox-gl": "^0.44.1",
3132
"rimraf": "~2.6.2",
3233
"typescript": "~2.6.2"
3334
},

packages/geojson-extension/src/index.tsx

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ import {
1313
IRenderMime
1414
} from '@jupyterlab/rendermime-interfaces';
1515

16-
import * as leaflet from 'leaflet';
16+
import * as mapboxgl from 'mapbox-gl';
1717

1818
import 'leaflet/dist/leaflet.css';
1919

2020
import '../style/index.css';
2121

22-
import * as iconRetinaUrl from 'leaflet/dist/images/marker-icon-2x.png';
23-
import * as iconUrl from 'leaflet/dist/images/marker-icon.png';
24-
import * as shadowUrl from 'leaflet/dist/images/marker-shadow.png';
2522

2623
/**
2724
* The CSS class to add to the GeoJSON Widget.
@@ -39,39 +36,7 @@ const CSS_ICON_CLASS = 'jp-MaterialIcon jp-GeoJSONIcon';
3936
export
4037
const MIME_TYPE = 'application/geo+json';
4138

42-
/**
43-
* Set base path for leaflet images.
44-
*/
45-
46-
// https://github.com/Leaflet/Leaflet/issues/4968
47-
// Marker file names are hard-coded in the leaflet source causing
48-
// issues with webpack.
49-
// This workaround allows webpack to inline all marker URLs.
50-
51-
delete (leaflet.Icon.Default.prototype as any)['_getIconUrl'];
52-
53-
leaflet.Icon.Default.mergeOptions({
54-
iconRetinaUrl: iconRetinaUrl,
55-
iconUrl: iconUrl,
56-
shadowUrl: shadowUrl
57-
});
58-
59-
60-
/**
61-
* The url template that leaflet tile layers.
62-
* See http://leafletjs.com/reference-1.0.3.html#tilelayer
63-
*/
64-
const URL_TEMPLATE: string = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
65-
66-
/**
67-
* The options for leaflet tile layers.
68-
* See http://leafletjs.com/reference-1.0.3.html#tilelayer
69-
*/
70-
const LAYER_OPTIONS: leaflet.TileLayerOptions = {
71-
attribution: 'Map data (c) <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
72-
minZoom: 0,
73-
maxZoom: 18
74-
};
39+
mapboxgl.accessToken = 'pk.eyJ1IjoibWlja3QiLCJhIjoiLXJIRS1NbyJ9.EfVT76g4A5dyuApW_zuIFQ';
7540

7641

7742
export
@@ -82,14 +47,7 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
8247
constructor(options: IRenderMime.IRendererOptions) {
8348
super();
8449
this.addClass(CSS_CLASS);
85-
this._mimeType = options.mimeType;
86-
// Create leaflet map object
87-
// trackResize option set to false as it is not needed to track
88-
// window.resize events since we have individual phosphor resize
89-
// events.
90-
this._map = leaflet.map(this.node, {
91-
trackResize: false
92-
});
50+
this._mimeType = options.mimeType;
9351
}
9452

9553
/**
@@ -107,16 +65,20 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
10765
*/
10866
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
10967
const data = model.data[this._mimeType] as any | GeoJSON.GeoJsonObject;
110-
const metadata = model.metadata[this._mimeType] as any || {};
68+
// const metadata = model.metadata[this._mimeType] as any || {};
11169
return new Promise<void>((resolve, reject) => {
112-
// Add leaflet tile layer to map
113-
leaflet.tileLayer(
114-
metadata.url_template || URL_TEMPLATE,
115-
metadata.layer_options || LAYER_OPTIONS
116-
).addTo(this._map);
117-
// Create GeoJSON layer from data and add to map
118-
this._geoJSONLayer = leaflet.geoJSON(data).addTo(this._map);
119-
this.update();
70+
// Add GeoJSON layer to map
71+
if (this._map) {
72+
this._map.addLayer({
73+
id: 'layer',
74+
type: 'symbol',
75+
source: {
76+
type: 'geojson',
77+
data
78+
}
79+
});
80+
this.update();
81+
}
12082
resolve();
12183
});
12284
}
@@ -125,16 +87,22 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
12587
* A message handler invoked on an `'after-attach'` message.
12688
*/
12789
protected onAfterAttach(msg: Message): void {
90+
this._map = new mapboxgl.Map({
91+
container: this.node,
92+
style: 'mapbox://styles/mapbox/light-v9',
93+
minZoom: 0,
94+
maxZoom: 18
95+
});
12896
if (this.parent.hasClass('jp-OutputArea-child')) {
12997
// Disable scroll zoom by default to avoid conflicts with notebook scroll
130-
this._map.scrollWheelZoom.disable();
98+
this._map.scrollZoom.disable();
13199
// Enable scroll zoom on map focus
132-
this._map.on('blur', (event) => {
133-
this._map.scrollWheelZoom.disable();
100+
this._map.on('blur', (event: Event) => {
101+
this._map.scrollZoom.disable();
134102
});
135103
// Disable scroll zoom on blur
136-
this._map.on('focus', (event) => {
137-
this._map.scrollWheelZoom.enable();
104+
this._map.on('focus', (event: Event) => {
105+
this._map.scrollZoom.enable();
138106
});
139107
}
140108
this.update();
@@ -159,13 +127,12 @@ class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
159127
*/
160128
protected onUpdateRequest(msg: Message): void {
161129
// Update map size after update
162-
if (this.isVisible) this._map.invalidateSize();
163-
// Update map size after panel/window is resized
164-
this._map.fitBounds(this._geoJSONLayer.getBounds());
130+
if (this._map && this.isVisible) {
131+
this._map.resize();
132+
}
165133
}
166134

167-
private _map: leaflet.Map;
168-
private _geoJSONLayer: leaflet.GeoJSON;
135+
private _map: mapboxgl.Map;
169136
private _mimeType: string;
170137
}
171138

yarn.lock

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,9 @@
503503
version "0.5.0"
504504
resolved "https://registry.npmjs.org/@types/katex/-/katex-0.5.0.tgz#a7dbb981f1bc89c8b1b5e7fa8a1ce2617ab2f358"
505505

506-
"@types/leaflet@^1.2.0":
507-
version "1.2.5"
508-
resolved "https://registry.npmjs.org/@types/leaflet/-/leaflet-1.2.5.tgz#b172502c298849cadd034314dabfc936beff7636"
506+
"@types/mapbox-gl@^0.44.1":
507+
version "0.44.1"
508+
resolved "https://registry.npmjs.org/@types/mapbox-gl/-/mapbox-gl-0.44.1.tgz#6d8618ab2a28e8b78ce2cca403f28a227c9f217d"
509509
dependencies:
510510
"@types/geojson" "*"
511511

@@ -3439,6 +3439,42 @@ mapbox-gl@^0.44.0:
34393439
vt-pbf "^3.0.1"
34403440
webworkify "^1.5.0"
34413441

3442+
mapbox-gl@^0.44.2:
3443+
version "0.44.2"
3444+
resolved "https://registry.npmjs.org/mapbox-gl/-/mapbox-gl-0.44.2.tgz#8c118ba8c5c15b054272644f30877309db0f8ee2"
3445+
dependencies:
3446+
"@mapbox/gl-matrix" "^0.0.1"
3447+
"@mapbox/mapbox-gl-supported" "^1.3.0"
3448+
"@mapbox/point-geometry" "^0.1.0"
3449+
"@mapbox/shelf-pack" "^3.1.0"
3450+
"@mapbox/tiny-sdf" "^1.1.0"
3451+
"@mapbox/unitbezier" "^0.0.0"
3452+
"@mapbox/vector-tile" "^1.3.0"
3453+
"@mapbox/whoots-js" "^3.0.0"
3454+
brfs "^1.4.0"
3455+
bubleify "^0.7.0"
3456+
csscolorparser "~1.0.2"
3457+
earcut "^2.1.3"
3458+
geojson-rewind "^0.3.0"
3459+
geojson-vt "^3.0.0"
3460+
gray-matter "^3.0.8"
3461+
grid-index "^1.0.0"
3462+
jsonlint-lines-primitives "~1.6.0"
3463+
minimist "0.0.8"
3464+
package-json-versionify "^1.0.2"
3465+
pbf "^3.0.5"
3466+
quickselect "^1.0.0"
3467+
rw "^1.3.3"
3468+
shuffle-seed "^1.1.6"
3469+
sort-object "^0.3.2"
3470+
supercluster "^2.3.0"
3471+
through2 "^2.0.3"
3472+
tinyqueue "^1.1.0"
3473+
unassertify "^2.0.0"
3474+
unflowify "^1.0.0"
3475+
vt-pbf "^3.0.1"
3476+
webworkify "^1.5.0"
3477+
34423478
marching-simplex-table@^1.0.0:
34433479
version "1.0.0"
34443480
resolved "https://registry.npmjs.org/marching-simplex-table/-/marching-simplex-table-1.0.0.tgz#bc16256e0f8f9b558aa9b2872f8832d9433f52ea"

0 commit comments

Comments
 (0)