-
Notifications
You must be signed in to change notification settings - Fork 14
/
mapview.jsx
351 lines (293 loc) · 11.3 KB
/
mapview.jsx
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import React, { Component, createRef } from 'react';
import { connect } from 'react-redux';
import proj4 from 'proj4';
import 'ol/ol.css';
import Map from 'ol/map';
import View from 'ol/view';
import TileLayer from 'ol/layer/tile';
import TileWMS from 'ol/source/tilewms';
import TileGrid from 'ol/tilegrid/tilegrid';
import proj from 'ol/proj';
import extent from 'ol/extent';
import { fromUrl, fromUrls, Pool } from 'geotiff';
import CanvasTileImageSource, { ProgressBar } from '../maputil';
import { renderData } from '../renderutils';
import { tileStartLoading, tileStopLoading, setPosition } from '../actions/main';
import { type } from 'os';
proj.setProj4(proj4);
async function all(promises) {
return await Promise.all(promises);
}
const mapStateToProps = ({ scenes, main }) => {
return { scenes, longitude: main.longitude, latitude: main.latitude, zoom: main.zoom };
};
const mapDispatchToProps = {
tileStartLoading,
tileStopLoading,
setPosition,
};
class MapView extends Component {
constructor() {
super();
this.mapRef = createRef();
this.progressBarRef = createRef();
this.map = new Map({
layers: [
new TileLayer({
extent: [-180, -90, 180, 90],
source: new TileWMS({
url: 'https://s2maps-tiles.eu/wms',
params: { LAYERS: 's2cloudless-2020' },
projection: 'EPSG:4326',
attributions: [
'<a xmlns: dct="http://purl.org/dc/terms/" href="https://s2maps.eu" property="dct:title">Sentinel-2 cloudless - https://s2maps.eu</a> by <a xmlns:cc="http://creativecommons.org/ns#" href="https://eox.at" property="cc:attributionName" rel="cc:attributionURL">EOX IT Services GmbH</a> (Contains modified Copernicus Sentinel data 2020)',
],
}),
}),
],
view: new View({
projection: 'EPSG:4326',
center: [0, 0],
zoom: 5,
// maxZoom: 13,
minZoom: 3,
maxZoom: 23,
}),
});
this.sceneLayers = {};
this.sceneSources = {};
this.tileCache = {};
this.renderedTileCache = {};
this.progressBar = new ProgressBar();
this.map.on('moveend', () => {
const view = this.map.getView();
this.props.setPosition(...view.getCenter(), view.getZoom());
});
this.pool = new Pool();
}
componentDidMount() {
this.map.setTarget(this.mapRef.current);
this.progressBar.setElement(this.progressBarRef.current);
}
componentDidUpdate(prevProps, prevState) {
const { scenes: prevScenes } = prevProps;
const { scenes } = this.props;
if (prevScenes.length > scenes.length) {
// TODO find scene and remove layer
const removedScene = prevScenes.find(scene => scenes.indexOf(scene) === -1);
delete this.renderedTileCache[removedScene.id];
this.map.removeLayer(this.sceneLayers[removedScene.id]);
} else if (prevScenes.length < scenes.length) {
this.addSceneLayer(scenes[scenes.length - 1]);
} else {
const changedScene = scenes.find((scene, index) => scene !== prevScenes[index]);
if (changedScene) {
delete this.renderedTileCache[changedScene.id];
const layer = this.sceneLayers[changedScene.id];
if (layer) {
const source = layer.getSource();
// refresh the source cache
source.setTileUrlFunction(
source.getTileUrlFunction(),
(new Date()).getTime(),
);
}
}
}
}
componentWillUnmount() {
this.map.setTarget(null);
}
async getImage(sceneId, url, hasOvr = true) {
if (!this.sceneSources[sceneId]) {
this.sceneSources[sceneId] = {};
}
if (!this.sceneSources[sceneId][url]) {
if (hasOvr) {
this.sceneSources[sceneId][url] = fromUrls(url, [`${url}.ovr`]);
} else {
this.sceneSources[sceneId][url] = fromUrl(url);
}
}
return this.sceneSources[sceneId][url];
}
async getRawTile(tiff, url, z, x, y, isRGB = false, samples) {
const id = `${url}-${samples ? samples.join(',') : 'all'}-${z}-${x}-${y}`;
if (!this.tileCache[id]) {
const image = await tiff.getImage(await tiff.getImageCount() - z - 1);
// const poolSize = image.fileDirectory.Compression === 5 ? 4 : null;
// const poolSize = null;
const wnd = [
x * image.getTileWidth(),
image.getHeight() - ((y + 1) * image.getTileHeight()),
(x + 1) * image.getTileWidth(),
image.getHeight() - (y * image.getTileHeight()),
];
if (isRGB) {
this.tileCache[id] = image.readRGB({
window: wnd,
pool: image.fileDirectory.Compression === 5 ? this.pool : null,
});
} else {
this.tileCache[id] = image.readRasters({
window: wnd,
samples,
pool: image.fileDirectory.Compression === 5 ? this.pool : null,
});
}
}
return this.tileCache[id];
}
async addSceneLayer(scene) {
this.sceneSources[scene.id] = {
[scene.redBand]: this.getImage(scene.id, scene.bands.get(scene.redBand), scene.hasOvr),
[scene.greenBand]: this.getImage(scene.id, scene.bands.get(scene.greenBand), scene.hasOvr),
[scene.blueBand]: this.getImage(scene.id, scene.bands.get(scene.blueBand), scene.hasOvr),
};
const tiff = await this.getImage(scene.id, scene.bands.get(scene.redBand), scene.hasOvr);
// calculate tilegrid from the 'red' image
const images = [];
const count = await tiff.getImageCount();
for (let i = 0; i < count; ++i) {
images.push(await tiff.getImage(i));
}
const first = images[0];
const resolutions = images.map((image => image.getResolution(first)[0]));
const tileSizes = images.map((image => [image.getTileWidth(), image.getTileHeight()]));
const tileGrid = new TileGrid({
extent: first.getBoundingBox(),
origin: [first.getOrigin()[0], first.getBoundingBox()[1]],
resolutions: resolutions.reverse(),
tileSizes: tileSizes.reverse(),
});
const code = first.geoKeys.ProjectedCSTypeGeoKey || first.geoKeys.GeographicTypeGeoKey;
if (typeof code === 'undefined') {
throw new Error('No ProjectedCSTypeGeoKey or GeographicTypeGeoKey provided');
}
const epsg = `EPSG:${code}`;
if (!proj4.defs(epsg)) {
const response = await fetch(`//epsg.io/${code}.proj4`);
proj4.defs(epsg, await response.text());
}
const layer = new TileLayer({
source: new CanvasTileImageSource({
projection: epsg,
tileGrid,
tileRenderFunction: (...args) => this.renderTile(scene.id, ...args),
attributions: scene.attribution,
}),
});
this.map.addLayer(layer);
this.sceneLayers[scene.id] = layer;
const view = this.map.getView();
const lonLatExtent = proj.transformExtent(
first.getBoundingBox(), epsg, this.map.getView().getProjection(),
);
// only animate to new bounds when center is not already inside image
if (!extent.containsCoordinate(lonLatExtent, view.getCenter())) {
view.fit(
lonLatExtent, {
duration: 1000,
padding: [0, this.map.getSize()[0] / 2, 0, 0],
},
);
}
const source = layer.getSource();
this.progressBar.setSource(source);
source.on('tileloadstart', this.props.tileStartLoading);
source.on('tileloadend', this.props.tileStopLoading);
source.on('tileloaderror', this.props.tileStopLoading);
}
async renderTile(sceneId, canvas, z, x, y) {
const id = `${z}-${x}-${y}`;
if (!this.renderedTileCache[sceneId]) {
this.renderedTileCache[sceneId] = {};
}
if (!this.renderedTileCache[sceneId][id]) {
this.renderedTileCache[sceneId][id] = this.renderTileInternal(sceneId, canvas, z, x, y);
}
return this.renderedTileCache[sceneId][id];
}
async renderTileInternal(sceneId, canvas, z, x, y) {
const scene = this.props.scenes.find(s => s.id === sceneId);
if (!scene) {
return;
}
if (scene.isRGB) { // && scene.isSingle) {
const tiff = await this.getImage(sceneId, scene.bands.get(scene.redBand), scene.hasOvr);
tiff.baseUrl = sceneId;
console.time(`parsing ${sceneId + z + x + y}`);
const data = await this.getRawTile(tiff, tiff.baseUrl, z, x, y, true);
console.timeEnd(`parsing ${sceneId + z + x + y}`);
const { width, height } = data;
canvas.width = width;
canvas.height = height;
console.time(`rendering ${sceneId + z + x + y}`);
// const ctx = canvas.getContext('2d');
// const imageData = ctx.createImageData(width, height);
// const out = imageData.data;
// let o = 0;
// let shift = 0;
// if (data instanceof Uint16Array) {
// shift = 8;
// }
// for (let i = 0; i < data.length; i += 3) {
// out[o] = data[i] >> shift;
// out[o + 1] = data[i + 1] >> shift;
// out[o + 2] = data[i + 2] >> shift;
// out[o + 3] = data[i] || data[i + 1] || data[i + 2] ? 255 : 0;
// o += 4;
// }
// ctx.putImageData(imageData, 0, 0);
renderData(canvas, scene.pipeline, width, height, data, null, null, true);
console.timeEnd(`rendering ${sceneId + z + x + y}`);
} else if (scene.isSingle) {
const tiff = await this.getImage(sceneId, scene.bands.get(scene.redBand), scene.hasOvr);
tiff.baseUrl = sceneId;
console.time(`parsing ${sceneId + z + x + y}`);
const data = await this.getRawTile(tiff, tiff.baseUrl, z, x, y, false, [
scene.redBand, scene.greenBand, scene.blueBand,
]);
console.timeEnd(`parsing ${sceneId + z + x + y}`);
const { width, height } = data;
canvas.width = width;
canvas.height = height;
console.time(`rendering ${sceneId + z + x + y}`);
const [red, green, blue] = data;
// const [red, green, blue] = [redArr, greenArr, blueArr].map(arr => arr[0]);
renderData(canvas, scene.pipeline, width, height, red, green, blue, false);
console.timeEnd(`rendering ${sceneId + z + x + y}`);
} else {
const [redImage, greenImage, blueImage] = await all([
this.getImage(sceneId, scene.bands.get(scene.redBand), scene.hasOvr),
this.getImage(sceneId, scene.bands.get(scene.greenBand), scene.hasOvr),
this.getImage(sceneId, scene.bands.get(scene.blueBand), scene.hasOvr),
]);
redImage.baseUrl = scene.bands.get(scene.redBand);
greenImage.baseUrl = scene.bands.get(scene.greenBand);
blueImage.baseUrl = scene.bands.get(scene.blueBand);
const [redArr, greenArr, blueArr] = await all([redImage, greenImage, blueImage].map(
tiff => this.getRawTile(tiff, tiff.baseUrl, z, x, y),
));
const { width, height } = redArr;
canvas.width = width;
canvas.height = height;
console.time(`rendering ${sceneId + z + x + y}`);
const [red, green, blue] = [redArr, greenArr, blueArr].map(arr => arr[0]);
renderData(canvas, scene.pipeline, width, height, red, green, blue, false);
console.timeEnd(`rendering ${sceneId + z + x + y}`);
}
}
render() {
const { longitude, latitude, zoom } = this.props;
this.map.getView().setCenter([longitude, latitude]);
this.map.getView().setZoom(zoom);
return (
<div ref={this.mapRef}>
<div ref={this.progressBarRef} className="map-progress-bar" />
</div>
);
}
}
const ConnectedMapView = connect(mapStateToProps, mapDispatchToProps)(MapView);
export default ConnectedMapView;