Skip to content

Commit 2a50f03

Browse files
committed
Use geoverview for GeoJSON renderer
1 parent 602568f commit 2a50f03

File tree

3 files changed

+7
-101
lines changed

3 files changed

+7
-101
lines changed

packages/geojson-extension/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@
5656
"@jupyterlab/rendermime-interfaces": "^3.2.8",
5757
"@lumino/messaging": "^1.10.1",
5858
"@lumino/widgets": "^1.31.1",
59-
"leaflet": "^1.5.0"
59+
"geoverview": "^1.2.0"
6060
},
6161
"devDependencies": {
6262
"@jupyterlab/builder": "^3.2.8",
63-
"@types/leaflet": "^1.4.0",
6463
"npm-run-all": "^4.1.5",
6564
"rimraf": "^3.0.2",
6665
"typescript": "~4.1.3"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'geoverview';

packages/geojson-extension/src/index.ts

Lines changed: 5 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,10 @@ import { Message } from '@lumino/messaging';
77

88
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
99

10-
import { defaultSanitizer } from '@jupyterlab/apputils';
11-
12-
import leaflet from 'leaflet';
13-
14-
import 'leaflet/dist/leaflet.css';
10+
import {view} from 'geoverview';
1511

1612
import '../style/index.css';
1713

18-
import iconRetinaUrl from 'leaflet/dist/images/marker-icon-2x.png';
19-
import iconUrl from 'leaflet/dist/images/marker-icon.png';
20-
import shadowUrl from 'leaflet/dist/images/marker-shadow.png';
21-
2214
/**
2315
* The CSS class to add to the GeoJSON Widget.
2416
*/
@@ -34,40 +26,6 @@ const CSS_ICON_CLASS = 'jp-MaterialIcon jp-GeoJSONIcon';
3426
*/
3527
export const MIME_TYPE = 'application/geo+json';
3628

37-
/**
38-
* Set base path for leaflet images.
39-
*/
40-
41-
// https://github.com/Leaflet/Leaflet/issues/4968
42-
// Marker file names are hard-coded in the leaflet source causing
43-
// issues with webpack.
44-
// This workaround allows webpack to inline all marker URLs.
45-
46-
delete (leaflet.Icon.Default.prototype as any)['_getIconUrl'];
47-
48-
leaflet.Icon.Default.mergeOptions({
49-
iconRetinaUrl: iconRetinaUrl,
50-
iconUrl: iconUrl,
51-
shadowUrl: shadowUrl,
52-
});
53-
54-
/**
55-
* The url template that leaflet tile layers.
56-
* See http://leafletjs.com/reference-1.0.3.html#tilelayer
57-
*/
58-
const URL_TEMPLATE = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
59-
60-
/**
61-
* The options for leaflet tile layers.
62-
* See http://leafletjs.com/reference-1.0.3.html#tilelayer
63-
*/
64-
const LAYER_OPTIONS: leaflet.TileLayerOptions = {
65-
attribution:
66-
'Map data (c) <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
67-
minZoom: 0,
68-
maxZoom: 18,
69-
};
70-
7129
export class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
7230
/**
7331
* Create a new widget for rendering GeoJSON.
@@ -76,59 +34,24 @@ export class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
7634
super();
7735
this.addClass(CSS_CLASS);
7836
this._mimeType = options.mimeType;
79-
// Create leaflet map object
80-
// trackResize option set to false as it is not needed to track
81-
// window.resize events since we have individual phosphor resize
82-
// events.
83-
this._map = leaflet.map(this.node, {
84-
trackResize: false,
85-
});
8637
}
8738

8839
/**
8940
* Dispose of the widget.
9041
*/
9142
dispose(): void {
92-
// Dispose of leaflet map
93-
this._map.remove();
94-
this._map = null;
9543
super.dispose();
9644
}
9745

9846
/**
9947
* Render GeoJSON into this widget's node.
10048
*/
10149
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
102-
const data = model.data[this._mimeType] as any | GeoJSON.GeoJsonObject;
103-
const metadata = (model.metadata[this._mimeType] as any) || {};
50+
const data = model.data[this._mimeType] as any;
10451
return new Promise<void>((resolve, reject) => {
105-
// Add leaflet tile layer to map
106-
leaflet
107-
.tileLayer(
108-
metadata.url_template || URL_TEMPLATE,
109-
metadata.layer_options || LAYER_OPTIONS
110-
)
111-
.addTo(this._map);
112-
// Create GeoJSON layer from data and add to map
113-
this._geoJSONLayer = leaflet
114-
.geoJSON(data, {
115-
onEachFeature: function (feature, layer) {
116-
if (feature.properties) {
117-
let popupContent = '<table>';
118-
for (const p in feature.properties) {
119-
popupContent +=
120-
'<tr><td>' +
121-
p +
122-
':</td><td><b>' +
123-
feature.properties[p] +
124-
'</b></td></tr>';
125-
}
126-
popupContent += '</table>';
127-
layer.bindPopup(defaultSanitizer.sanitize(popupContent));
128-
}
129-
},
130-
})
131-
.addTo(this._map);
52+
const map = view(data);
53+
this.node.appendChild(map.next().value);
54+
map.next();
13255
this.update();
13356
resolve();
13457
});
@@ -138,18 +61,6 @@ export class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
13861
* A message handler invoked on an `'after-attach'` message.
13962
*/
14063
protected onAfterAttach(msg: Message): void {
141-
if (this.parent.hasClass('jp-OutputArea-child')) {
142-
// Disable scroll zoom by default to avoid conflicts with notebook scroll
143-
this._map.scrollWheelZoom.disable();
144-
// Enable scroll zoom on map focus
145-
this._map.on('blur', (event) => {
146-
this._map.scrollWheelZoom.disable();
147-
});
148-
// Disable scroll zoom on blur
149-
this._map.on('focus', (event) => {
150-
this._map.scrollWheelZoom.enable();
151-
});
152-
}
15364
this.update();
15465
}
15566

@@ -173,14 +84,9 @@ export class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
17384
protected onUpdateRequest(msg: Message): void {
17485
// Update map size after update
17586
if (this.isVisible) {
176-
this._map.invalidateSize();
17787
}
178-
// Update map size after panel/window is resized
179-
this._map.fitBounds(this._geoJSONLayer.getBounds());
18088
}
18189

182-
private _map: leaflet.Map;
183-
private _geoJSONLayer: leaflet.GeoJSON;
18490
private _mimeType: string;
18591
}
18692

0 commit comments

Comments
 (0)