-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathindex.tsx
224 lines (192 loc) · 5.72 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
Widget
} from '@phosphor/widgets';
import {
Message
} from '@phosphor/messaging';
import {
IRenderMime
} from '@jupyterlab/rendermime-interfaces';
import * as leaflet from 'leaflet';
import 'leaflet/dist/leaflet.css';
import '../style/index.css';
import * as iconRetinaUrl from 'leaflet/dist/images/marker-icon-2x.png';
import * as iconUrl from 'leaflet/dist/images/marker-icon.png';
import * as shadowUrl from 'leaflet/dist/images/marker-shadow.png';
/**
* The CSS class to add to the GeoJSON Widget.
*/
const CSS_CLASS = 'jp-RenderedGeoJSON';
/**
* The CSS class for a GeoJSON icon.
*/
const CSS_ICON_CLASS = 'jp-MaterialIcon jp-GeoJSONIcon';
/**
* The MIME type for GeoJSON.
*/
export
const MIME_TYPE = 'application/geo+json';
/**
* Set base path for leaflet images.
*/
// https://github.com/Leaflet/Leaflet/issues/4968
// Marker file names are hard-coded in the leaflet source causing
// issues with webpack.
// This workaround allows webpack to inline all marker URLs.
delete (leaflet.Icon.Default.prototype as any)['_getIconUrl'];
leaflet.Icon.Default.mergeOptions({
iconRetinaUrl: iconRetinaUrl,
iconUrl: iconUrl,
shadowUrl: shadowUrl
});
/**
* The url template that leaflet tile layers.
* See http://leafletjs.com/reference-1.0.3.html#tilelayer
*/
const URL_TEMPLATE: string = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
/**
* The options for leaflet tile layers.
* See http://leafletjs.com/reference-1.0.3.html#tilelayer
*/
const LAYER_OPTIONS: leaflet.TileLayerOptions = {
attribution: 'Map data (c) <a href="https://openstreetmap.org">OpenStreetMap</a> contributors',
minZoom: 0,
maxZoom: 20
};
class FitToBounds extends leaflet.Control {
_img: HTMLImageElement;
delegate: RenderedGeoJSON;
onAdd(map: leaflet.Map): HTMLElement {
this._img = leaflet.DomUtil.create('img') as HTMLImageElement;
this._img.className = 'jp-ZoomToBoundsIcon';
this._img.style.width = '40px';
this._img.onclick = this.delegate.fitToBounds.bind(this.delegate);
return this._img;
};
};
export
class RenderedGeoJSON extends Widget implements IRenderMime.IRenderer {
/**
* Create a new widget for rendering GeoJSON.
*/
constructor(options: IRenderMime.IRendererOptions) {
super();
this.addClass(CSS_CLASS);
this._mimeType = options.mimeType;
// Create leaflet map object
// trackResize option set to false as it is not needed to track
// window.resize events since we have individual phosphor resize
// events.
this._map = leaflet.map(this.node, {
trackResize: false
});
let ftb_control = new FitToBounds();
ftb_control.delegate = this;
ftb_control.addTo(this._map);
}
/**
* Dispose of the widget.
*/
dispose(): void {
// Dispose of leaflet map
this._map.remove();
this._map = null;
super.dispose();
}
/**
* Render GeoJSON into this widget's node.
*/
renderModel(model: IRenderMime.IMimeModel): Promise<void> {
const data = model.data[this._mimeType] as any | GeoJSON.GeoJsonObject;
const metadata = model.metadata[this._mimeType] as any || {};
return new Promise<void>((resolve, reject) => {
// Add leaflet tile layer to map
leaflet.tileLayer(
metadata.url_template || URL_TEMPLATE,
metadata.layer_options || LAYER_OPTIONS
).addTo(this._map);
// Create GeoJSON layer from data and add to map
this._geoJSONLayer = leaflet.geoJSON(data).addTo(this._map);
this.update();
resolve();
});
}
fitToBounds(): void {
// Update map size after panel/window is resized
this._map.fitBounds(this._geoJSONLayer.getBounds());
}
/**
* A message handler invoked on an `'after-attach'` message.
*/
protected onAfterAttach(msg: Message): void {
if (this.parent.hasClass('jp-OutputArea-child')) {
// Disable scroll zoom by default to avoid conflicts with notebook scroll
this._map.scrollWheelZoom.disable();
// Enable scroll zoom on map focus
this._map.on('blur', (event) => {
this._map.scrollWheelZoom.disable();
});
// Disable scroll zoom on blur
this._map.on('focus', (event) => {
this._map.scrollWheelZoom.enable();
});
}
this.fitToBounds();
this.update();
}
/**
* A message handler invoked on an `'after-show'` message.
*/
protected onAfterShow(msg: Message): void {
this.fitToBounds();
this.update();
}
/**
* A message handler invoked on a `'resize'` message.
*/
protected onResize(msg: Widget.ResizeMessage): void {
this.update();
}
/**
* A message handler invoked on an `'update-request'` message.
*/
protected onUpdateRequest(msg: Message): void {
// Update map size after update
if (this.isVisible) this._map.invalidateSize();
}
private _map: leaflet.Map;
private _geoJSONLayer: leaflet.GeoJSON;
private _mimeType: string;
}
/**
* A mime renderer factory for GeoJSON data.
*/
export
const rendererFactory: IRenderMime.IRendererFactory = {
safe: true,
mimeTypes: [MIME_TYPE],
createRenderer: options => new RenderedGeoJSON(options)
};
const extensions: IRenderMime.IExtension | IRenderMime.IExtension[] = [
{
id: '@jupyterlab/geojson-extension:factory',
rendererFactory,
rank: 0,
dataType: 'json',
fileTypes: [{
name: 'geojson',
mimeTypes: [MIME_TYPE],
extensions: ['.geojson', '.geo.json'],
iconClass: CSS_ICON_CLASS
}],
documentWidgetFactoryOptions: {
name: 'GeoJSON',
primaryFileType: 'geojson',
fileTypes: ['geojson', 'json'],
defaultFor: ['geojson']
}
}
];
export default extensions;