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/dist/gridviz.js +56 -73
- package/dist/gridviz.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +3 -2
- package/src/style/ImageStyle.js +48 -25
package/package.json
CHANGED
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
|
|
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
|
package/src/style/ImageStyle.js
CHANGED
|
@@ -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
|
|
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.
|
|
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
|
|
52
|
-
const
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
}
|