gridviz 3.0.14 → 3.0.16

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 (55) hide show
  1. package/README.md +127 -128
  2. package/dist/gridviz.js +7959 -7860
  3. package/dist/gridviz.min.js +1 -1
  4. package/package.json +74 -74
  5. package/src/button/ZoomButtons.js +101 -96
  6. package/src/core/Dataset.js +154 -157
  7. package/src/core/Drawable.js +60 -61
  8. package/src/core/GeoCanvas.js +409 -370
  9. package/src/core/Layer.js +23 -25
  10. package/src/core/Legend.js +108 -110
  11. package/src/core/Map.js +495 -495
  12. package/src/core/MultiResolutionDataset.js +93 -97
  13. package/src/core/Style.js +98 -99
  14. package/src/dataset/CSVGrid.js +112 -112
  15. package/src/dataset/GeoTIFF.js +122 -123
  16. package/src/dataset/JSGrid.js +55 -54
  17. package/src/dataset/TiledGrid.js +289 -288
  18. package/src/dataset/worker.js +18 -0
  19. package/src/index.js +77 -71
  20. package/src/layer/BackgroundLayer.js +162 -158
  21. package/src/layer/BackgroundLayerImage.js +81 -84
  22. package/src/layer/BackgroundLayerWMS.js +104 -108
  23. package/src/layer/GeoJSONLayer.js +214 -220
  24. package/src/layer/GridLayer.js +148 -157
  25. package/src/layer/LabelLayer.js +194 -193
  26. package/src/legend/ColorCategoryLegend.js +109 -110
  27. package/src/legend/ColorDiscreteLegend.js +100 -102
  28. package/src/legend/ColorLegend.js +115 -118
  29. package/src/legend/OrientationLegend.js +89 -91
  30. package/src/legend/SizeLegend.js +224 -224
  31. package/src/legend/TernaryLegend.js +302 -210
  32. package/src/style/CompositionStyle.js +291 -288
  33. package/src/style/DotDensityStyle.js +133 -134
  34. package/src/style/ImageStyle.js +100 -102
  35. package/src/style/IsoFenceStyle.js +217 -203
  36. package/src/style/JoyPlotStyle.js +127 -128
  37. package/src/style/LegoStyle.js +110 -110
  38. package/src/style/MosaicStyle.js +99 -99
  39. package/src/style/NinjaStarStyle.js +102 -102
  40. package/src/style/PillarStyle.js +207 -208
  41. package/src/style/SegmentStyle.js +105 -105
  42. package/src/style/ShapeColorSizeStyle.js +147 -132
  43. package/src/style/SideCategoryStyle.js +112 -114
  44. package/src/style/SideStyle.js +212 -205
  45. package/src/style/SquareColorCategoryWebGLStyle.js +107 -108
  46. package/src/style/SquareColorWebGLStyle.js +126 -126
  47. package/src/style/StrokeStyle.js +135 -136
  48. package/src/style/TanakaStyle.js +69 -68
  49. package/src/style/TextStyle.js +110 -111
  50. package/src/style/TimeSeriesStyle.js +222 -229
  51. package/src/utils/scale.js +218 -228
  52. package/src/utils/stretching.js +120 -133
  53. package/src/utils/ternary.js +203 -203
  54. package/src/utils/utils.js +58 -63
  55. package/src/utils/webGLUtils.js +72 -70
package/src/index.js CHANGED
@@ -1,71 +1,77 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
- // the application
5
- export { Map } from './core/Map.js'
6
- export { GeoCanvas } from './core/GeoCanvas.js'
7
- export { Style } from './core/Style.js'
8
- export { Layer } from './core/Layer.js'
9
- export { Dataset } from './core/Dataset.js'
10
- export { MultiResolutionDataset } from './core/MultiResolutionDataset.js'
11
-
12
- // export dataset types
13
- export { TiledGrid } from './dataset/TiledGrid.js'
14
- export { CSVGrid } from './dataset/CSVGrid.js'
15
- export { JSGrid } from './dataset/JSGrid.js'
16
-
17
- // export styles
18
- export { ShapeColorSizeStyle } from './style/ShapeColorSizeStyle.js'
19
- export { StrokeStyle } from './style/StrokeStyle.js'
20
- export { JoyPlotStyle } from './style/JoyPlotStyle.js'
21
- export { CompositionStyle } from './style/CompositionStyle.js'
22
- export { SegmentStyle } from './style/SegmentStyle.js'
23
- export { TextStyle } from './style/TextStyle.js'
24
- export { PillarStyle } from './style/PillarStyle.js'
25
- export { SideStyle } from './style/SideStyle.js'
26
- //export { ContourStyle } from './style/ContourStyle.js'
27
- export { SideCategoryStyle } from './style/SideCategoryStyle.js'
28
- export { DotDensityStyle } from './style/DotDensityStyle.js'
29
- export { TanakaStyle } from './style/TanakaStyle.js'
30
- export { LegoStyle } from './style/LegoStyle.js'
31
- export { SquareColorWebGLStyle } from './style/SquareColorWebGLStyle.js'
32
- export { SquareColorCategoryWebGLStyle } from './style/SquareColorCategoryWebGLStyle.js'
33
- export { MosaicStyle } from './style/MosaicStyle.js'
34
- export { NinjaStarStyle } from './style/NinjaStarStyle.js'
35
- export { TimeSeriesStyle } from './style/TimeSeriesStyle.js'
36
- export { IsoFenceStyle } from './style/IsoFenceStyle.js'
37
- export { ImageStyle } from './style/ImageStyle.js'
38
-
39
- // export additional layers
40
- export { GridLayer } from './layer/GridLayer.js'
41
- export { BackgroundLayer } from './layer/BackgroundLayer.js'
42
- export { BackgroundLayerWMS } from './layer/BackgroundLayerWMS.js'
43
- export { BackgroundLayerImage } from './layer/BackgroundLayerImage.js'
44
- export { LabelLayer } from './layer/LabelLayer.js'
45
- export { GeoJSONLayer } from './layer/GeoJSONLayer.js'
46
-
47
- // export legends
48
- export { ColorLegend } from './legend/ColorLegend.js'
49
- export { ColorDiscreteLegend } from './legend/ColorDiscreteLegend.js'
50
- export { ColorCategoryLegend } from './legend/ColorCategoryLegend.js'
51
- export { SizeLegend, sizeLegend, sizeLegendViewScale, sizeDiscreteLegend, sizeDiscreteViewScaleLegend } from './legend/SizeLegend.js'
52
- export { OrientationLegend, orientationLegend } from './legend/OrientationLegend.js'
53
- export { TernaryLegend } from './legend/TernaryLegend.js'
54
-
55
- // export { goToStraight, zoomTo } from "./utils/zoomUtils"
56
- export * from './utils/stretching.js'
57
- export * from './utils/scale.js'
58
- export * from './utils/ternary.js'
59
- export { nice } from './utils/utils.js'
60
-
61
- import { GeoCanvas } from './core/GeoCanvas.js'
62
- export const getParameterByName = GeoCanvas.getParameterByName
63
-
64
- // set default d3 locale
65
- import { formatDefaultLocale } from 'd3-format'
66
- formatDefaultLocale({
67
- decimal: '.',
68
- thousands: ' ',
69
- grouping: [3],
70
- currency: ['', '€'],
71
- })
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ // the application
5
+ export { Map } from './core/Map.js'
6
+ export { GeoCanvas } from './core/GeoCanvas.js'
7
+ export { Style } from './core/Style.js'
8
+ export { Layer } from './core/Layer.js'
9
+ export { Dataset } from './core/Dataset.js'
10
+ export { MultiResolutionDataset } from './core/MultiResolutionDataset.js'
11
+
12
+ // export dataset types
13
+ export { TiledGrid } from './dataset/TiledGrid.js'
14
+ export { CSVGrid } from './dataset/CSVGrid.js'
15
+ export { JSGrid } from './dataset/JSGrid.js'
16
+
17
+ // export styles
18
+ export { ShapeColorSizeStyle } from './style/ShapeColorSizeStyle.js'
19
+ export { StrokeStyle } from './style/StrokeStyle.js'
20
+ export { JoyPlotStyle } from './style/JoyPlotStyle.js'
21
+ export { CompositionStyle } from './style/CompositionStyle.js'
22
+ export { SegmentStyle } from './style/SegmentStyle.js'
23
+ export { TextStyle } from './style/TextStyle.js'
24
+ export { PillarStyle } from './style/PillarStyle.js'
25
+ export { SideStyle } from './style/SideStyle.js'
26
+ //export { ContourStyle } from './style/ContourStyle.js'
27
+ export { SideCategoryStyle } from './style/SideCategoryStyle.js'
28
+ export { DotDensityStyle } from './style/DotDensityStyle.js'
29
+ export { TanakaStyle } from './style/TanakaStyle.js'
30
+ export { LegoStyle } from './style/LegoStyle.js'
31
+ export { SquareColorWebGLStyle } from './style/SquareColorWebGLStyle.js'
32
+ export { SquareColorCategoryWebGLStyle } from './style/SquareColorCategoryWebGLStyle.js'
33
+ export { MosaicStyle } from './style/MosaicStyle.js'
34
+ export { NinjaStarStyle } from './style/NinjaStarStyle.js'
35
+ export { TimeSeriesStyle } from './style/TimeSeriesStyle.js'
36
+ export { IsoFenceStyle } from './style/IsoFenceStyle.js'
37
+ export { ImageStyle } from './style/ImageStyle.js'
38
+
39
+ // export additional layers
40
+ export { GridLayer } from './layer/GridLayer.js'
41
+ export { BackgroundLayer } from './layer/BackgroundLayer.js'
42
+ export { BackgroundLayerWMS } from './layer/BackgroundLayerWMS.js'
43
+ export { BackgroundLayerImage } from './layer/BackgroundLayerImage.js'
44
+ export { LabelLayer } from './layer/LabelLayer.js'
45
+ export { GeoJSONLayer } from './layer/GeoJSONLayer.js'
46
+
47
+ // export legends
48
+ export { ColorLegend } from './legend/ColorLegend.js'
49
+ export { ColorDiscreteLegend } from './legend/ColorDiscreteLegend.js'
50
+ export { ColorCategoryLegend } from './legend/ColorCategoryLegend.js'
51
+ export {
52
+ SizeLegend,
53
+ sizeLegend,
54
+ sizeLegendViewScale,
55
+ sizeDiscreteLegend,
56
+ sizeDiscreteViewScaleLegend,
57
+ } from './legend/SizeLegend.js'
58
+ export { OrientationLegend, orientationLegend } from './legend/OrientationLegend.js'
59
+ export { TernaryLegend } from './legend/TernaryLegend.js'
60
+
61
+ // export { goToStraight, zoomTo } from "./utils/zoomUtils"
62
+ export * from './utils/stretching.js'
63
+ export * from './utils/scale.js'
64
+ export * from './utils/ternary.js'
65
+ export { nice } from './utils/utils.js'
66
+
67
+ import { GeoCanvas } from './core/GeoCanvas.js'
68
+ export const getParameterByName = GeoCanvas.getParameterByName
69
+
70
+ // set default d3 locale
71
+ import { formatDefaultLocale } from 'd3-format'
72
+ formatDefaultLocale({
73
+ decimal: '.',
74
+ thousands: ' ',
75
+ grouping: [3],
76
+ currency: ['', '€'],
77
+ })
@@ -1,158 +1,162 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
- import { Layer } from '../core/Layer.js'
5
-
6
- /**
7
- *
8
- * A map background layer in "Slippy map" XYZ standard.
9
- * See https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
10
- * https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/#6/27.88/44.48
11
- *
12
- * @module layer
13
- * @author Julien Gaffuri
14
- */
15
- export class BackgroundLayer extends Layer {
16
- /**
17
- * @param {object} opts
18
- */
19
- constructor(opts) {
20
- super(opts)
21
- opts = opts || {}
22
-
23
- /** The image cache, indexed by z/y/x */
24
- this.cache = {}
25
-
26
- /**
27
- * @type {string} */
28
- this.url = opts.url
29
- /** @type {function(number,number,number):string} */
30
- this.urlFun = opts.urlFun || ((x, y, z) => this.url + z + '/' + x + '/' + y + '.png')
31
-
32
- /** @type {Array.<number>} */
33
- this.resolutions = opts.resolutions
34
- if (!this.resolutions || this.resolutions.length == 0)
35
- throw new Error('No resolutions provided for background layer')
36
-
37
- /** @type {number} */
38
- this.nbPix = opts.nbPix || 256
39
- /** CRS coordinates of top left corner
40
- * @type {Array.<number>} */
41
- this.origin = opts.origin || [0, 0]
42
- /** @type {number} */
43
- this.z0 = opts.z0 || 0
44
- }
45
-
46
- /**
47
- * Get z/x/y cache data.
48
- * @param {number} z
49
- * @param {number} x
50
- * @param {number} y
51
- * @returns {HTMLImageElement|string|undefined}
52
- * @private
53
- */
54
- get(z, x, y) {
55
- let d = this.cache[z]
56
- if (!d) return
57
- d = d[x]
58
- if (!d) return
59
- return d[y]
60
- }
61
-
62
- /**
63
- * Get z/x/y cache data.
64
- * @param {HTMLImageElement|string} img
65
- * @param {number} z
66
- * @param {number} x
67
- * @param {number} y
68
- * @returns
69
- * @private
70
- */
71
- put(img, z, x, y) {
72
- if (!this.cache[z]) this.cache[z] = {}
73
- if (!this.cache[z][x]) this.cache[z][x] = {}
74
- this.cache[z][x][y] = img
75
- }
76
-
77
- /**
78
- * @param {import("../core/GeoCanvas").GeoCanvas} geoCanvas The canvas where to draw the layer.
79
- * @returns {void}
80
- */
81
- draw(geoCanvas) {
82
-
83
- if (!this.resolutions || this.resolutions.length == 0) {
84
- console.error('No resolutions provided for background layer')
85
- return
86
- }
87
-
88
- //
89
- const z = geoCanvas.view.z
90
- const x0 = this.origin[0],
91
- y0 = this.origin[1]
92
-
93
- //get zoom level and resolution
94
- let z_ = 0
95
- for (z_ = 0; z_ < this.resolutions.length; z_++) if (this.resolutions[z_] < z) break
96
- z_ -= 1
97
- z_ = Math.max(0, z_)
98
- z_ = Math.min(z_, this.resolutions.length - 1)
99
- //console.log(this.resolutions.length, z)
100
- const res = this.resolutions[z_]
101
-
102
- z_ += this.z0
103
-
104
- const sizeG = this.nbPix * res
105
- const size = sizeG / z
106
-
107
- //get tile numbers
108
- const xGeoToTMS = (x) => Math.ceil((x - x0) / sizeG)
109
- const yGeoToTMS = (y) => Math.ceil(-(y - y0) / sizeG)
110
- const xMin = xGeoToTMS(geoCanvas.extGeo.xMin) - 1
111
- const xMax = xGeoToTMS(geoCanvas.extGeo.xMax)
112
- const yMax = yGeoToTMS(geoCanvas.extGeo.yMin)
113
- const yMin = yGeoToTMS(geoCanvas.extGeo.yMax) - 1
114
-
115
- //handle images
116
- for (let x = xMin; x < xMax; x++) {
117
- for (let y = yMin; y < yMax; y++) {
118
- //get image
119
- let img = this.get(z_, x, y)
120
-
121
- //load image
122
- if (!img) {
123
- const img = new Image()
124
- this.put(img, z_, x, y)
125
- img.onload = () => {
126
- geoCanvas.redraw()
127
- }
128
- img.onerror = () => {
129
- //case when no image
130
- this.put('failed', z_, x, y)
131
- }
132
- img.src = this.urlFun(x, y, z_)
133
- continue
134
- }
135
-
136
- //case when no image
137
- if (img === 'failed') continue
138
- if (!(img instanceof HTMLImageElement)) {
139
- console.log(img)
140
- continue
141
- }
142
- if (img.width == 0 || img.height == 0) continue
143
-
144
- //draw image
145
- const xGeo = x0 + x * sizeG
146
- const yGeo = y0 - y * sizeG
147
- try {
148
- geoCanvas.initCanvasTransform()
149
- geoCanvas.ctx.drawImage(img, geoCanvas.geoToPixX(xGeo), geoCanvas.geoToPixY(yGeo), size, size)
150
- //cg.ctx.drawImage(img, xGeo, yGeo, sizeG, -sizeG)
151
- } catch (error) {
152
- console.error(error)
153
- }
154
- }
155
- }
156
-
157
- }
158
- }
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { Layer } from '../core/Layer.js'
5
+
6
+ /**
7
+ *
8
+ * A map background layer in "Slippy map" XYZ standard.
9
+ * See https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
10
+ * https://www.maptiler.com/google-maps-coordinates-tile-bounds-projection/#6/27.88/44.48
11
+ *
12
+ * @module layer
13
+ * @author Julien Gaffuri
14
+ */
15
+ export class BackgroundLayer extends Layer {
16
+ /**
17
+ * @param {object} opts
18
+ */
19
+ constructor(opts) {
20
+ super(opts)
21
+ opts = opts || {}
22
+
23
+ /** The image cache, indexed by z/y/x */
24
+ this.cache = {}
25
+
26
+ /**
27
+ * @type {string} */
28
+ this.url = opts.url
29
+ /** @type {function(number,number,number):string} */
30
+ this.urlFun = opts.urlFun || ((x, y, z) => this.url + z + '/' + x + '/' + y + '.png')
31
+
32
+ /** @type {Array.<number>} */
33
+ this.resolutions = opts.resolutions
34
+ if (!this.resolutions || this.resolutions.length == 0)
35
+ throw new Error('No resolutions provided for background layer')
36
+
37
+ /** @type {number} */
38
+ this.nbPix = opts.nbPix || 256
39
+ /** CRS coordinates of top left corner
40
+ * @type {Array.<number>} */
41
+ this.origin = opts.origin || [0, 0]
42
+ /** @type {number} */
43
+ this.z0 = opts.z0 || 0
44
+ }
45
+
46
+ /**
47
+ * Get z/x/y cache data.
48
+ * @param {number} z
49
+ * @param {number} x
50
+ * @param {number} y
51
+ * @returns {HTMLImageElement|string|undefined}
52
+ * @private
53
+ */
54
+ get(z, x, y) {
55
+ let d = this.cache[z]
56
+ if (!d) return
57
+ d = d[x]
58
+ if (!d) return
59
+ return d[y]
60
+ }
61
+
62
+ /**
63
+ * Get z/x/y cache data.
64
+ * @param {HTMLImageElement|string} img
65
+ * @param {number} z
66
+ * @param {number} x
67
+ * @param {number} y
68
+ * @returns
69
+ * @private
70
+ */
71
+ put(img, z, x, y) {
72
+ if (!this.cache[z]) this.cache[z] = {}
73
+ if (!this.cache[z][x]) this.cache[z][x] = {}
74
+ this.cache[z][x][y] = img
75
+ }
76
+
77
+ /**
78
+ * @param {import("../core/GeoCanvas").GeoCanvas} geoCanvas The canvas where to draw the layer.
79
+ * @returns {void}
80
+ */
81
+ draw(geoCanvas) {
82
+ if (!this.resolutions || this.resolutions.length == 0) {
83
+ console.error('No resolutions provided for background layer')
84
+ return
85
+ }
86
+
87
+ //
88
+ const z = geoCanvas.view.z
89
+ const x0 = this.origin[0],
90
+ y0 = this.origin[1]
91
+
92
+ //get zoom level and resolution
93
+ let z_ = 0
94
+ for (z_ = 0; z_ < this.resolutions.length; z_++) if (this.resolutions[z_] < z) break
95
+ z_ -= 1
96
+ z_ = Math.max(0, z_)
97
+ z_ = Math.min(z_, this.resolutions.length - 1)
98
+ //console.log(this.resolutions.length, z)
99
+ const res = this.resolutions[z_]
100
+
101
+ z_ += this.z0
102
+
103
+ const sizeG = this.nbPix * res
104
+ const size = sizeG / z
105
+
106
+ //get tile numbers
107
+ const xGeoToTMS = (x) => Math.ceil((x - x0) / sizeG)
108
+ const yGeoToTMS = (y) => Math.ceil(-(y - y0) / sizeG)
109
+ const xMin = xGeoToTMS(geoCanvas.extGeo.xMin) - 1
110
+ const xMax = xGeoToTMS(geoCanvas.extGeo.xMax)
111
+ const yMax = yGeoToTMS(geoCanvas.extGeo.yMin)
112
+ const yMin = yGeoToTMS(geoCanvas.extGeo.yMax) - 1
113
+
114
+ //handle images
115
+ for (let x = xMin; x < xMax; x++) {
116
+ for (let y = yMin; y < yMax; y++) {
117
+ //get image
118
+ let img = this.get(z_, x, y)
119
+
120
+ //load image
121
+ if (!img) {
122
+ const img = new Image()
123
+ this.put(img, z_, x, y)
124
+ img.onload = () => {
125
+ geoCanvas.redraw()
126
+ }
127
+ img.onerror = () => {
128
+ //case when no image
129
+ this.put('failed', z_, x, y)
130
+ }
131
+ img.src = this.urlFun(x, y, z_)
132
+ continue
133
+ }
134
+
135
+ //case when no image
136
+ if (img === 'failed') continue
137
+ if (!(img instanceof HTMLImageElement)) {
138
+ console.log(img)
139
+ continue
140
+ }
141
+ if (img.width == 0 || img.height == 0) continue
142
+
143
+ //draw image
144
+ const xGeo = x0 + x * sizeG
145
+ const yGeo = y0 - y * sizeG
146
+ try {
147
+ geoCanvas.initCanvasTransform()
148
+ geoCanvas.offscreenCtx.drawImage(
149
+ img,
150
+ geoCanvas.geoToPixX(xGeo),
151
+ geoCanvas.geoToPixY(yGeo),
152
+ size,
153
+ size
154
+ )
155
+ //cg.ctx.drawImage(img, xGeo, yGeo, sizeG, -sizeG)
156
+ } catch (error) {
157
+ console.error(error)
158
+ }
159
+ }
160
+ }
161
+ }
162
+ }
@@ -1,84 +1,81 @@
1
- //@ts-check
2
- 'use strict'
3
-
4
- import { Layer } from '../core/Layer.js'
5
-
6
- /**
7
- *
8
- * A map background layer composed of a single image file, geolocated.
9
- *
10
- * @module layer
11
- * @author Julien Gaffuri
12
- */
13
- export class BackgroundLayerImage extends Layer {
14
- /**
15
- * @param {object} opts
16
- */
17
- constructor(opts) {
18
- super(opts)
19
- opts = opts || {}
20
-
21
- /** The image file URL
22
- * @type {string} */
23
- this.url = opts.url
24
-
25
- /** The image left coordinate
26
- * @type {number} */
27
- this.xMin = opts.xMin || 0;
28
- /** The image top coordinate
29
- * @type {number} */
30
- this.yMax = opts.yMax || 0;
31
-
32
- /** The image width, in geo unit
33
- * @type {number} */
34
- this.width = opts.width || 20000
35
- /** The image height, in geo unit
36
- * @type {number} */
37
- this.height = opts.height || 20000
38
-
39
- /** The image object
40
- * @type {HTMLImageElement|undefined} */
41
- this.img = undefined;
42
- }
43
-
44
- /**
45
- * @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas The canvas where to draw the layer.
46
- * @returns {void}
47
- */
48
- draw(geoCanvas) {
49
-
50
- //update map extent
51
- //geoCanvas.updateExtentGeo(0)
52
-
53
- if (this.img) {
54
- //the image was already downloaded: draw it
55
-
56
- //compute screen coordinates and size ratio
57
- const x = geoCanvas.geoToPixX(this.xMin)
58
- const y = geoCanvas.geoToPixY(this.yMax)
59
- const z = geoCanvas.getView().z
60
-
61
- //draw image
62
- geoCanvas.initCanvasTransform()
63
- geoCanvas.ctx.drawImage(this.img, x, y, this.width / z, this.height / z)
64
-
65
- } else {
66
- //retrieve image
67
-
68
- if (!this.img) {
69
- this.img = new Image()
70
- this.img.onload = () => {
71
- geoCanvas.redraw()
72
- }
73
- this.img.onerror = () => {
74
- //case when no image
75
- console.warn("Could not retrieve background image from", this.url)
76
- }
77
- }
78
-
79
- //set URL to launch the download
80
- this.img.src = this.url
81
- }
82
-
83
- }
84
- }
1
+ //@ts-check
2
+ 'use strict'
3
+
4
+ import { Layer } from '../core/Layer.js'
5
+
6
+ /**
7
+ *
8
+ * A map background layer composed of a single image file, geolocated.
9
+ *
10
+ * @module layer
11
+ * @author Julien Gaffuri
12
+ */
13
+ export class BackgroundLayerImage extends Layer {
14
+ /**
15
+ * @param {object} opts
16
+ */
17
+ constructor(opts) {
18
+ super(opts)
19
+ opts = opts || {}
20
+
21
+ /** The image file URL
22
+ * @type {string} */
23
+ this.url = opts.url
24
+
25
+ /** The image left coordinate
26
+ * @type {number} */
27
+ this.xMin = opts.xMin || 0
28
+ /** The image top coordinate
29
+ * @type {number} */
30
+ this.yMax = opts.yMax || 0
31
+
32
+ /** The image width, in geo unit
33
+ * @type {number} */
34
+ this.width = opts.width || 20000
35
+ /** The image height, in geo unit
36
+ * @type {number} */
37
+ this.height = opts.height || 20000
38
+
39
+ /** The image object
40
+ * @type {HTMLImageElement|undefined} */
41
+ this.img = undefined
42
+ }
43
+
44
+ /**
45
+ * @param {import("../core/GeoCanvas.js").GeoCanvas} geoCanvas The canvas where to draw the layer.
46
+ * @returns {void}
47
+ */
48
+ draw(geoCanvas) {
49
+ //update map extent
50
+ //geoCanvas.updateExtentGeo(0)
51
+
52
+ if (this.img) {
53
+ //the image was already downloaded: draw it
54
+
55
+ //compute screen coordinates and size ratio
56
+ const x = geoCanvas.geoToPixX(this.xMin)
57
+ const y = geoCanvas.geoToPixY(this.yMax)
58
+ const z = geoCanvas.getView().z
59
+
60
+ //draw image
61
+ geoCanvas.initCanvasTransform()
62
+ geoCanvas.offscreenCtx.drawImage(this.img, x, y, this.width / z, this.height / z)
63
+ } else {
64
+ //retrieve image
65
+
66
+ if (!this.img) {
67
+ this.img = new Image()
68
+ this.img.onload = () => {
69
+ geoCanvas.redraw()
70
+ }
71
+ this.img.onerror = () => {
72
+ //case when no image
73
+ console.warn('Could not retrieve background image from', this.url)
74
+ }
75
+ }
76
+
77
+ //set URL to launch the download
78
+ this.img.src = this.url
79
+ }
80
+ }
81
+ }