gridviz 2.0.28 → 2.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/gridviz.js +34368 -0
  2. package/dist/gridviz.min.js +1 -1
  3. package/package.json +10 -6
  4. package/src/App.js +654 -0
  5. package/src/BackgroundLayer.js +182 -0
  6. package/src/BackgroundLayerWMS.js +131 -0
  7. package/src/Dataset.js +76 -0
  8. package/src/DatasetComponent.js +103 -0
  9. package/src/GeoCanvas.js +419 -0
  10. package/src/LabelLayer.js +190 -0
  11. package/src/Layer.js +92 -0
  12. package/src/Legend.js +86 -0
  13. package/src/LineLayer.js +150 -0
  14. package/src/Style.js +114 -0
  15. package/src/Tooltip.js +150 -0
  16. package/src/dataset/COGeo.js +24 -0
  17. package/src/dataset/CSVGrid.js +104 -0
  18. package/src/dataset/GeoTIFF.js +127 -0
  19. package/src/dataset/GridTile.js +41 -0
  20. package/src/dataset/ParquetGrid.js +122 -0
  21. package/src/dataset/TiledGrid.js +296 -0
  22. package/src/index.js +177 -0
  23. package/src/legend/ColorCategoryLegend.js +124 -0
  24. package/src/legend/ColorDiscreteLegend.js +105 -0
  25. package/src/legend/ColorLegend.js +125 -0
  26. package/src/legend/SegmentOrientationLegend.js +87 -0
  27. package/src/legend/SegmentWidthLegend.js +97 -0
  28. package/src/legend/SizeLegend.js +141 -0
  29. package/src/style/CompositionStyle.js +300 -0
  30. package/src/style/ContourStyle.js +54 -0
  31. package/src/style/DotDensityStyle.js +156 -0
  32. package/src/style/JoyPlotStyle.js +135 -0
  33. package/src/style/KernelSmoothingOldStyle.js +221 -0
  34. package/src/style/KernelSmoothingStyle.js +115 -0
  35. package/src/style/KernelSmoothingWGLStyle.js +130 -0
  36. package/src/style/LegoStyle.js +112 -0
  37. package/src/style/MosaicStyle.js +107 -0
  38. package/src/style/NinjaStarStyle.js +124 -0
  39. package/src/style/PillarStyle.js +230 -0
  40. package/src/style/SegmentStyle.js +142 -0
  41. package/src/style/ShapeColorSizeStyle.js +126 -0
  42. package/src/style/SideCatStyle.js +172 -0
  43. package/src/style/SideStyle.js +218 -0
  44. package/src/style/SquareColorCatWGLStyle.js +120 -0
  45. package/src/style/SquareColorWGLStyle.js +146 -0
  46. package/src/style/StrokeStyle.js +131 -0
  47. package/src/style/TanakaStyle.js +160 -0
  48. package/src/style/TextStyle.js +132 -0
  49. package/src/utils/Utils.js +35 -0
  50. package/src/utils/WebGLRectangleColoring.js +106 -0
  51. package/src/utils/WebGLSquareColoring.js +118 -0
  52. package/src/utils/WebGLSquareColoringAdvanced.js +230 -0
  53. package/src/utils/WebGLSquareColoringCatAdvanced.js +133 -0
  54. package/src/utils/WebGLSquareColoringKS.js +153 -0
  55. package/src/utils/stretching.js +131 -0
  56. package/src/utils/webGLUtils.js +71 -0
  57. package/src/utils/zoomUtils.js +142 -0
@@ -0,0 +1,127 @@
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { DatasetComponent } from '../DatasetComponent.js'
5
+ //import { fromUrl } from "geotiff"
6
+
7
+ /**
8
+ * @todo
9
+ * A dataset composed of a single GeoTiff file.
10
+ *
11
+ * @author Joseph Davies, Julien Gaffuri
12
+ */
13
+ export class GeoTIFF extends DatasetComponent {
14
+ /**
15
+ * @param {string} url The URL of the dataset.
16
+ * @param {number} resolution The dataset resolution in geographical unit.
17
+ * @param {{preprocess?:(function(import("../Dataset").Cell):boolean)}} opts
18
+ */
19
+ constructor(url, resolution, opts = {}) {
20
+ super(url, resolution, opts)
21
+
22
+ /**
23
+ * @private
24
+ * @type {Array.<import("../Dataset").Cell>} */
25
+ this.cells = []
26
+
27
+ /**
28
+ * @type {string}
29
+ * @private */
30
+ this.infoLoadingStatus = 'notLoaded'
31
+ }
32
+
33
+ /**
34
+ * Request data within a geographic envelope.
35
+ *
36
+ * @param {import("../Dataset").Envelope|undefined} e
37
+ * @param {function():void} redraw
38
+ */
39
+ getData(e, redraw) {
40
+ //check if data already loaded
41
+ if (this.infoLoadingStatus != 'notLoaded') return this
42
+
43
+ //load data
44
+ this.infoLoadingStatus = 'loading'
45
+
46
+ /*
47
+
48
+ fromUrl(this.url)
49
+ .then(tiff => {
50
+ console.log(tiff);
51
+
52
+ (async function () {
53
+ const image = await tiff.getImage();
54
+ const width = image.getWidth();
55
+ const height = image.getHeight();
56
+ const tileWidth = image.getTileWidth();
57
+ const tileHeight = image.getTileHeight();
58
+ const samplesPerPixel = image.getSamplesPerPixel();
59
+
60
+ //for geotiff
61
+ const origin = image.getOrigin();
62
+ const resolution = image.getResolution();
63
+ const bbox = image.getBoundingBox();
64
+
65
+ console.log(image)
66
+ console.log(width, height)
67
+ console.log(tileWidth, tileHeight)
68
+ console.log(samplesPerPixel)
69
+ console.log(origin)
70
+ console.log(resolution)
71
+ console.log(bbox)
72
+
73
+ console.log("-------")
74
+
75
+ const data = await image.readRasters();
76
+ console.log(data)
77
+
78
+ })()
79
+
80
+ //convert coordinates in numbers
81
+ //for (const c of data) { c.x = +c.x; c.y = +c.y; }
82
+
83
+ //this.cells = data;
84
+
85
+ //execute preprocess, if any
86
+ //if (this.preprocess) for (const c of this.cells) this.preprocess(c);
87
+
88
+ //TODO check if redraw is necessary
89
+ //that is if the dataset belongs to a layer which is visible at the current zoom level
90
+
91
+ //execute the callback, usually a draw function
92
+ if (redraw) redraw()
93
+
94
+ this.infoLoadingStatus = "loaded";
95
+
96
+
97
+ })
98
+ .catch(() => {
99
+ //mark as failed
100
+ this.infoLoadingStatus = "failed";
101
+ this.cells = []
102
+ });
103
+ */
104
+
105
+ return this
106
+ }
107
+
108
+ /**
109
+ * Fill the view cache with all cells which are within a geographical envelope.
110
+ *
111
+ * @param {import("../Dataset").Envelope} extGeo
112
+ * @returns {void}
113
+ */
114
+ updateViewCache(extGeo) {
115
+ //data not loaded yet
116
+ if (!this.cells) return
117
+
118
+ this.cellsViewCache = []
119
+ for (const cell of this.cells) {
120
+ if (+cell.x + this.resolution < extGeo.xMin) continue
121
+ if (+cell.x - this.resolution > extGeo.xMax) continue
122
+ if (+cell.y + this.resolution < extGeo.yMin) continue
123
+ if (+cell.y - this.resolution > extGeo.yMax) continue
124
+ this.cellsViewCache.push(cell)
125
+ }
126
+ }
127
+ }
@@ -0,0 +1,41 @@
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ /**
5
+ * A grid tile.
6
+ *
7
+ * @author Joseph Davies, Julien Gaffuri
8
+ */
9
+ export class GridTile {
10
+ /**
11
+ * @param {Array.<import("../Dataset").Cell>} cells The tile cells.
12
+ * @param {number} xT The X position of the tile.
13
+ * @param {number} yT The Y position of the tile.
14
+ * @param {import("./CSVGrid").GridInfo} gridInfo The grid info object.
15
+ */
16
+ constructor(cells, xT, yT, gridInfo) {
17
+ /** @type {Array.<import("../Dataset").Cell>} */
18
+ this.cells = cells
19
+ /** @type {number} */
20
+ this.x = xT
21
+ /** @type {number} */
22
+ this.y = yT
23
+
24
+ const r = gridInfo.resolutionGeo
25
+ const s = gridInfo.tileSizeCell
26
+
27
+ /** @type {import("../Dataset").Envelope} */
28
+ this.extGeo = {
29
+ xMin: gridInfo.originPoint.x + r * s * this.x,
30
+ xMax: gridInfo.originPoint.x + r * s * (this.x + 1),
31
+ yMin: gridInfo.originPoint.y + r * s * this.y,
32
+ yMax: gridInfo.originPoint.y + r * s * (this.y + 1),
33
+ }
34
+
35
+ //convert cell coordinates into geographical coordinates
36
+ for (let cell of this.cells) {
37
+ cell.x = this.extGeo.xMin + cell.x * r
38
+ cell.y = this.extGeo.yMin + cell.y * r
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,122 @@
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { DatasetComponent } from '../DatasetComponent.js'
5
+ import { tableFromIPC } from 'apache-arrow'
6
+ //import { readParquet } from "parquet-wasm"
7
+
8
+ /**
9
+ * A dataset composed of a single parquet file (not tiled).
10
+ *
11
+ * @author Julien Gaffuri
12
+ */
13
+ export class ParquetGrid extends DatasetComponent {
14
+ /**
15
+ * @param {string} url The URL of the dataset.
16
+ * @param {number} resolution The dataset resolution in geographical unit.
17
+ * @param {{preprocess?:(function(import("../Dataset").Cell):boolean), readParquetFun?:Function}} opts
18
+ */
19
+ constructor(url, resolution, opts = {}) {
20
+ super(url, resolution, opts)
21
+
22
+ /**
23
+ * @private
24
+ * @type {Array.<import("../Dataset").Cell>} */
25
+ this.cells = []
26
+
27
+ /**
28
+ * @type {string}
29
+ * @private */
30
+ this.infoLoadingStatus = 'notLoaded'
31
+
32
+ if (!opts.readParquetFun) throw new Error('readParquet function needed for parquet dataset')
33
+
34
+ /**
35
+ * @type {Function}
36
+ * @private */
37
+ this.readParquetFun = opts.readParquetFun
38
+ }
39
+
40
+ /**
41
+ * Request data within a geographic envelope.
42
+ *
43
+ * @param {import("../Dataset").Envelope|undefined} e
44
+ * @param {function():void} redraw
45
+ */
46
+ getData(e, redraw) {
47
+ //check if data already loaded
48
+ if (this.infoLoadingStatus != 'notLoaded') return this
49
+
50
+ //load data
51
+ this.infoLoadingStatus = 'loading'
52
+ ;(async () => {
53
+ try {
54
+ const resp = await fetch(this.url)
55
+ const parquetUint8Array = new Uint8Array(await resp.arrayBuffer())
56
+ const arrowUint8Array = this.readParquetFun(parquetUint8Array)
57
+
58
+ const t = tableFromIPC(arrowUint8Array)
59
+ //see https://arrow.apache.org/docs/js/
60
+ //https://loaders.gl/arrowjs/docs/developer-guide/tables#record-tojson-and-toarray
61
+
62
+ this.cells = []
63
+ for (const e of t) {
64
+ //get cell
65
+ const c = e.toJSON()
66
+
67
+ //preprocess/filter
68
+ if (this.preprocess) {
69
+ const b = this.preprocess(c)
70
+ if (b == false) continue
71
+ this.cells.push(c)
72
+ } else {
73
+ this.cells.push(c)
74
+ }
75
+ }
76
+
77
+ //TODO check if redraw is necessary
78
+ //that is if the dataset belongs to a layer which is visible at the current zoom level
79
+
80
+ //execute the callback, usually a draw function
81
+ if (redraw) redraw()
82
+
83
+ this.infoLoadingStatus = 'loaded'
84
+ } catch (error) {
85
+ //mark as failed
86
+ this.infoLoadingStatus = 'failed'
87
+ this.cells = []
88
+ }
89
+ })()
90
+
91
+ return this
92
+ }
93
+
94
+ /**
95
+ * Fill the view cache with all cells which are within a geographical envelope.
96
+ *
97
+ * @param {import("../Dataset").Envelope} extGeo
98
+ * @returns {void}
99
+ */
100
+ updateViewCache(extGeo) {
101
+ //data not loaded yet
102
+ if (!this.cells) return
103
+
104
+ this.cellsViewCache = []
105
+ for (const cell of this.cells) {
106
+ if (+cell.x + this.resolution < extGeo.xMin) continue
107
+ if (+cell.x - this.resolution > extGeo.xMax) continue
108
+ if (+cell.y + this.resolution < extGeo.yMin) continue
109
+ if (+cell.y - this.resolution > extGeo.yMax) continue
110
+ this.cellsViewCache.push(cell)
111
+ }
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Retrieve parquet wasm decoder
117
+ */
118
+ const getReadParquet = async () => {
119
+ //const parquetModule = await import("https://unpkg.com/parquet-wasm@0.4.0-beta.5/esm/arrow2.js");
120
+ //await parquetModule.default();
121
+ //return parquetModule.readParquet;
122
+ }
@@ -0,0 +1,296 @@
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ /** @typedef {{ dims: object, crs: string, tileSizeCell: number, originPoint: {x:number,y:number}, resolutionGeo: number, tilingBounds:import("../Dataset").Envelope, format:import("../DatasetComponent").Format }} GridInfo */
5
+
6
+ // internal
7
+ import { GridTile } from './GridTile.js'
8
+ import { App } from '../App.js'
9
+ import { DatasetComponent } from '../DatasetComponent.js'
10
+ import { monitor, monitorDuration } from '../utils/Utils.js'
11
+
12
+ // external
13
+ import { json, csv } from 'd3-fetch'
14
+ import { tableFromIPC } from 'apache-arrow'
15
+
16
+ /**
17
+ * A tiled dataset, composed of CSV (or parquet) tiles.
18
+ *
19
+ * @author Joseph Davies, Julien Gaffuri
20
+ */
21
+ export class TiledGrid extends DatasetComponent {
22
+ /**
23
+ * @param {string} url The URL of the dataset.
24
+ * @param {App} app The application.
25
+ * @param {{preprocess?:(function(import("../Dataset").Cell):boolean), readParquetFun?:Function }} opts
26
+ */
27
+ constructor(url, app, opts = {}) {
28
+ super(url, 0, opts)
29
+
30
+ /**
31
+ * The app being used.
32
+ * @type {App}
33
+ */
34
+ this.app = app
35
+
36
+ /**
37
+ * The grid info object, from the info.json file.
38
+ * @type {GridInfo | undefined}
39
+ * @private
40
+ * */
41
+ this.info = undefined
42
+
43
+ /**
44
+ * @type {string}
45
+ * @private */
46
+ this.infoLoadingStatus = 'notLoaded'
47
+
48
+ /**
49
+ * The cache of the loaded tiles. It is double indexed: by xT and then yT.
50
+ * Example: this.cache[xT][yT] returns the tile at [xT][yT] location.
51
+ *
52
+ * @type {object}
53
+ * */
54
+ this.cache = {}
55
+
56
+ /**
57
+ * @type {Function|undefined}
58
+ * @private */
59
+ this.readParquetFun = opts.readParquetFun
60
+ }
61
+
62
+ /**
63
+ * Load the info.json from the url.
64
+ *
65
+ * @param {function():void} callback
66
+ * @returns this
67
+ */
68
+ loadInfo(callback) {
69
+ if (!this.info && this.infoLoadingStatus === 'notLoaded') {
70
+ ;(async () => {
71
+ try {
72
+ const data = await json(this.url + 'info.json')
73
+ this.info = data
74
+ this.resolution = data.resolutionGeo
75
+ this.infoLoadingStatus = 'loaded'
76
+ if (callback) callback()
77
+ } catch (error) {
78
+ //mark as failed
79
+ this.infoLoadingStatus = 'failed'
80
+ }
81
+ })()
82
+ } else if (callback && (this.infoLoadingStatus === 'loaded' || this.infoLoadingStatus === 'failed'))
83
+ callback()
84
+ return this
85
+ }
86
+
87
+ /**
88
+ * Compute a tiling envelope from a geographical envelope.
89
+ * This is the function to use to know which tiles to download for a geographical view.
90
+ *
91
+ * @param {import("../Dataset").Envelope} e
92
+ * @returns {import("../Dataset").Envelope|undefined}
93
+ */
94
+ getTilingEnvelope(e) {
95
+ if (!this.info) {
96
+ this.loadInfo(() => {})
97
+ return
98
+ }
99
+
100
+ const po = this.info.originPoint,
101
+ r = this.info.resolutionGeo,
102
+ s = this.info.tileSizeCell
103
+
104
+ return {
105
+ xMin: Math.floor((e.xMin - po.x) / (r * s)),
106
+ xMax: Math.floor((e.xMax - po.x) / (r * s)),
107
+ yMin: Math.floor((e.yMin - po.y) / (r * s)),
108
+ yMax: Math.floor((e.yMax - po.y) / (r * s)),
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Request data within a geographic envelope.
114
+ *
115
+ * @param {import("../Dataset").Envelope} extGeo
116
+ * @param {function():void} redrawFun
117
+ * @returns {this}
118
+ */
119
+ getData(extGeo, redrawFun) {
120
+ //TODO empty cache when it gets too big ?
121
+
122
+ //check if info has been loaded
123
+ if (!this.info) return this
124
+
125
+ //tiles within the scope
126
+ /** @type {import("../Dataset").Envelope|undefined} */
127
+ const tb = this.getTilingEnvelope(extGeo)
128
+ if (!tb) return this
129
+
130
+ //grid bounds
131
+ /** @type {import("../Dataset").Envelope} */
132
+ const gb = this.info.tilingBounds
133
+
134
+ const format = this.info.format
135
+
136
+ for (let xT = Math.max(tb.xMin, gb.xMin); xT <= Math.min(tb.xMax, gb.xMax); xT++) {
137
+ for (let yT = Math.max(tb.yMin, gb.yMin); yT <= Math.min(tb.yMax, gb.yMax); yT++) {
138
+ //prepare cache
139
+ if (!this.cache[xT]) this.cache[xT] = {}
140
+
141
+ //check if tile exists in the cache
142
+ /** @type {GridTile} */
143
+ let tile = this.cache[xT][yT]
144
+ if (tile) continue
145
+
146
+ //mark tile as loading
147
+ this.cache[xT][yT] = 'loading'
148
+ ;(async () => {
149
+ //request tile
150
+ /** @type {Array.<import("../Dataset").Cell>} */
151
+ let cells
152
+
153
+ try {
154
+ if (!format || format === 'CSV') {
155
+ /** @type {Array.<import("../Dataset").Cell>} */
156
+ // @ts-ignore
157
+ const data = await csv(this.url + xT + '/' + yT + '.csv')
158
+
159
+ if (monitor) monitorDuration('*** TiledGrid parse start')
160
+
161
+ //preprocess/filter
162
+ if (this.preprocess) {
163
+ cells = []
164
+ for (const c of data) {
165
+ const b = this.preprocess(c)
166
+ if (b == false) continue
167
+ cells.push(c)
168
+ }
169
+ } else {
170
+ cells = data
171
+ }
172
+
173
+ if (monitor) monitorDuration('preprocess / filter')
174
+ } else if (format === 'PARQUET') {
175
+ if (!this.readParquetFun)
176
+ throw new Error('readParquet function needed for parquet dataset')
177
+
178
+ const resp = await fetch(this.url + xT + '/' + yT + '.parquet')
179
+ const parquetUint8Array = new Uint8Array(await resp.arrayBuffer())
180
+ const arrowUint8Array = this.readParquetFun(parquetUint8Array)
181
+ const t = tableFromIPC(arrowUint8Array)
182
+
183
+ cells = []
184
+ for (const e of t) {
185
+ //get cell
186
+ const c = e.toJSON()
187
+
188
+ //preprocess/filter
189
+ if (this.preprocess) {
190
+ const b = this.preprocess(c)
191
+ if (b == false) continue
192
+ cells.push(c)
193
+ } else {
194
+ cells.push(c)
195
+ }
196
+ }
197
+ } else throw new Error('Tiled format not supported: ' + format)
198
+ } catch (error) {
199
+ //mark as failed
200
+ this.cache[xT][yT] = 'failed'
201
+ return
202
+ }
203
+
204
+ //store tile in cache
205
+ if (!this.info) {
206
+ console.error('Tile info inknown')
207
+ return
208
+ }
209
+ const tile_ = new GridTile(cells, xT, yT, this.info)
210
+ this.cache[xT][yT] = tile_
211
+
212
+ if (monitor) monitorDuration('storage')
213
+
214
+ //if no redraw is specified, then leave
215
+ if (!redrawFun) return
216
+
217
+ //check if redraw is really needed, that is if:
218
+
219
+ // 1. the dataset belongs to a layer which is visible at the current zoom level
220
+ let redraw = false
221
+ //go through the layers
222
+ const zf = this.app.getZoomFactor()
223
+ for (const lay of this.app.layers) {
224
+ if (!lay.visible) continue
225
+ if (lay.getDatasetComponent(zf) != this) continue
226
+ //found one layer. No need to seek more.
227
+ redraw = true
228
+ break
229
+ }
230
+ if (monitor) monitorDuration('check redraw 1')
231
+
232
+ if (!redraw) return
233
+
234
+ // 2. the tile is within the view, that is its geo envelope intersects the viewer geo envelope.
235
+ const env = this.app.updateExtentGeo()
236
+ const envT = tile_.extGeo
237
+ if (env.xMax <= envT.xMin) return
238
+ if (env.xMin >= envT.xMax) return
239
+ if (env.yMax <= envT.yMin) return
240
+ if (env.yMin >= envT.yMax) return
241
+
242
+ if (monitor) monitorDuration('check redraw 2')
243
+ if (monitor) monitorDuration('*** TiledGrid parse end')
244
+
245
+ //redraw
246
+ redrawFun()
247
+ })()
248
+ }
249
+ }
250
+ return this
251
+ }
252
+
253
+ /**
254
+ * Fill the view cache with all cells which are within a geographical envelope.
255
+ * @abstract
256
+ * @param {import("../Dataset").Envelope} extGeo
257
+ * @returns {void}
258
+ */
259
+ updateViewCache(extGeo) {
260
+ //
261
+ this.cellsViewCache = []
262
+
263
+ //check if info has been loaded
264
+ if (!this.info) return
265
+
266
+ //tiles within the scope
267
+ /** @type {import("../Dataset").Envelope|undefined} */
268
+ const tb = this.getTilingEnvelope(extGeo)
269
+ if (!tb) return
270
+
271
+ //grid bounds
272
+ /** @type {import("../Dataset").Envelope} */
273
+ const gb = this.info.tilingBounds
274
+
275
+ for (let xT = Math.max(tb.xMin, gb.xMin); xT <= Math.min(tb.xMax, gb.xMax); xT++) {
276
+ if (!this.cache[xT]) continue
277
+ for (let yT = Math.max(tb.yMin, gb.yMin); yT <= Math.min(tb.yMax, gb.yMax); yT++) {
278
+ //get tile
279
+ /** @type {GridTile} */
280
+ const tile = this.cache[xT][yT]
281
+ if (!tile || typeof tile === 'string') continue
282
+
283
+ //get cells
284
+ //this.cellsViewCache = this.cellsViewCache.concat(tile.cells)
285
+
286
+ for (const cell of tile.cells) {
287
+ if (+cell.x + this.resolution < extGeo.xMin) continue
288
+ if (+cell.x - this.resolution > extGeo.xMax) continue
289
+ if (+cell.y + this.resolution < extGeo.yMin) continue
290
+ if (+cell.y - this.resolution > extGeo.yMax) continue
291
+ this.cellsViewCache.push(cell)
292
+ }
293
+ }
294
+ }
295
+ }
296
+ }