gridviz 3.0.11 → 3.0.12

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gridviz",
3
- "version": "3.0.11",
3
+ "version": "3.0.12",
4
4
  "description": "Visualization tool for gridded statistics",
5
5
  "keywords": [
6
6
  "statistics",
package/src/index.js CHANGED
@@ -56,8 +56,9 @@ export { TrivariateLegend } from './legend/TrivariateLegend.js'
56
56
  export * from './utils/stretching.js'
57
57
  export * from './utils/scale.js'
58
58
  export * from './utils/trivariate.js'
59
- export * from './utils/chernoff.js'
60
- export { nice, loadImage } from './utils/utils.js'
59
+ //export * from './utils/chernoff.js'
60
+ export { nice } from './utils/utils.js'
61
+ //loadImage
61
62
 
62
63
  import { GeoCanvas } from './core/GeoCanvas.js'
63
64
  export const getParameterByName = GeoCanvas.getParameterByName
@@ -1,7 +1,9 @@
1
1
  //@ts-check
2
2
  'use strict'
3
3
 
4
+ import { map } from 'd3-array'
4
5
  import { Style } from '../core/Style.js'
6
+ import { loadImage } from '../utils/utils.js'
5
7
 
6
8
  /**
7
9
  * @module style
@@ -14,17 +16,19 @@ export class ImageStyle extends Style {
14
16
  super(opts)
15
17
  opts = opts || {}
16
18
 
17
- /** A function returning the image code of a cell.
19
+ /** A function returning the image URL of a cell.
18
20
  * @type {function(import('../core/Dataset.js').Cell, number, number, object):string} */
19
- this.imageCode = opts.imageCode || (() => '') //(c,r,z,vs) => {}
20
-
21
- /** The dictionnary code -> image
22
- * @type {object} */
23
- this.images = opts.images || {}
21
+ this.image = opts.image || (() => '') //(c,r,z,vs) => {}
24
22
 
25
23
  /** The image size in ground meters
26
24
  * @type {function(import('../core/Dataset.js').Cell, number, number, object):number} */
27
25
  this.size = opts.size || ((cell, resolution) => resolution)
26
+
27
+ /** Dictionnary of preloaded images. url -> image
28
+ * @private
29
+ * @type {object} */
30
+ this.cache = {}
31
+
28
32
  }
29
33
 
30
34
  /**
@@ -33,7 +37,7 @@ export class ImageStyle extends Style {
33
37
  * @param {number} resolution
34
38
  * @override
35
39
  */
36
- draw(cells, geoCanvas, resolution) {
40
+ async draw(cells, geoCanvas, resolution) {
37
41
 
38
42
  //
39
43
  const z = geoCanvas.view.z,
@@ -48,28 +52,47 @@ export class ImageStyle extends Style {
48
52
  //
49
53
  for (let cell of cells) {
50
54
 
51
- //get cell image code
52
- const code = this.imageCode(cell, resolution, z, viewScale)
53
-
54
- //get image
55
- const image = this.images[code]
56
- if (!image) continue
55
+ //get cell image url
56
+ const url = this.image(cell, resolution, z, viewScale)
57
+ if (!url) continue
57
58
 
58
59
  //size and position values
59
60
  let sizePix = this.size(cell, resolution, z, viewScale) / z
60
61
  if (!sizePix) continue
61
- const d = (resolutionPix - sizePix) / 2
62
-
63
- try {
64
- geoCanvas.ctx.drawImage(image, geoCanvas.geoToPixX(cell.x) + d, geoCanvas.geoToPixY(cell.y) + d -resolutionPix, sizePix, sizePix)
65
-
66
- /*/red color filter
67
- geoCanvas.ctx.globalCompositeOperation = 'source-in';
68
- geoCanvas.ctx.fillStyle = 'red';
69
- geoCanvas.ctx.fillRect(geoCanvas.geoToPixX(cell.x) + d, geoCanvas.geoToPixY(cell.y) + d, sizePix, sizePix);
70
- geoCanvas.ctx.globalCompositeOperation = 'source-over';*/
71
- } catch (error) {
72
- console.error(error)
62
+
63
+ //get image from cache
64
+ const image = this.cache[url]
65
+
66
+ //loading, keep waiting
67
+ if (image == "loading") return;
68
+
69
+ //no image: load it
70
+ else if (!image) {
71
+ //tag as loading
72
+ this.cache[url] = "loading"
73
+
74
+ //define image
75
+ const img = new Image()
76
+ img.onload = () => {
77
+ //store image data in cache and redraw
78
+ this.cache[url] = img
79
+ geoCanvas.redraw()
80
+ }
81
+ img.onerror = () => {
82
+ //case when no image
83
+ console.warn("Could not retrieve image from", url)
84
+ }
85
+ //set URL to launch the download
86
+ img.src = url
87
+
88
+ } else {
89
+ //draw image
90
+ const d = (resolutionPix - sizePix) / 2
91
+ try {
92
+ geoCanvas.ctx.drawImage(image, geoCanvas.geoToPixX(cell.x) + d, geoCanvas.geoToPixY(cell.y) + d - resolutionPix, sizePix, sizePix)
93
+ } catch (error) {
94
+ console.error(error)
95
+ }
73
96
  }
74
97
 
75
98
  }