gridviz 3.0.19 → 3.0.20

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 +128 -128
  2. package/dist/gridviz.js +7861 -7861
  3. package/dist/gridviz.min.js +1 -1
  4. package/package.json +74 -74
  5. package/src/button/ZoomButtons.js +101 -101
  6. package/src/core/Dataset.js +154 -154
  7. package/src/core/Drawable.js +60 -60
  8. package/src/core/GeoCanvas.js +430 -430
  9. package/src/core/Layer.js +23 -23
  10. package/src/core/Legend.js +108 -108
  11. package/src/core/Map.js +516 -516
  12. package/src/core/MultiResolutionDataset.js +93 -93
  13. package/src/core/Style.js +98 -98
  14. package/src/dataset/CSVGrid.js +112 -112
  15. package/src/dataset/GeoTIFF.js +122 -122
  16. package/src/dataset/JSGrid.js +55 -55
  17. package/src/dataset/TiledGrid.js +302 -302
  18. package/src/dataset/worker.js +18 -18
  19. package/src/index.js +77 -77
  20. package/src/layer/BackgroundLayer.js +162 -162
  21. package/src/layer/BackgroundLayerImage.js +81 -81
  22. package/src/layer/BackgroundLayerWMS.js +104 -104
  23. package/src/layer/GeoJSONLayer.js +214 -214
  24. package/src/layer/GridLayer.js +148 -148
  25. package/src/layer/LabelLayer.js +194 -194
  26. package/src/legend/ColorCategoryLegend.js +109 -109
  27. package/src/legend/ColorDiscreteLegend.js +100 -100
  28. package/src/legend/ColorLegend.js +115 -115
  29. package/src/legend/OrientationLegend.js +89 -89
  30. package/src/legend/SizeLegend.js +224 -224
  31. package/src/legend/TernaryLegend.js +302 -302
  32. package/src/style/CompositionStyle.js +291 -291
  33. package/src/style/DotDensityStyle.js +133 -133
  34. package/src/style/ImageStyle.js +100 -100
  35. package/src/style/IsoFenceStyle.js +217 -217
  36. package/src/style/JoyPlotStyle.js +127 -127
  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 -207
  41. package/src/style/SegmentStyle.js +105 -105
  42. package/src/style/ShapeColorSizeStyle.js +147 -147
  43. package/src/style/SideCategoryStyle.js +112 -112
  44. package/src/style/SideStyle.js +212 -212
  45. package/src/style/SquareColorCategoryWebGLStyle.js +107 -107
  46. package/src/style/SquareColorWebGLStyle.js +126 -126
  47. package/src/style/StrokeStyle.js +135 -135
  48. package/src/style/TanakaStyle.js +69 -69
  49. package/src/style/TextStyle.js +110 -110
  50. package/src/style/TimeSeriesStyle.js +222 -222
  51. package/src/utils/scale.js +218 -218
  52. package/src/utils/stretching.js +120 -120
  53. package/src/utils/ternary.js +208 -208
  54. package/src/utils/utils.js +58 -58
  55. package/src/utils/webGLUtils.js +72 -72
package/src/index.js CHANGED
@@ -1,77 +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 {
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
+ //@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,162 +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
- 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
+ //@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,81 +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
- //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
- }
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
+ }