Repository navigation
Expand file tree
/
Copy pathImageMapGuide.js
More file actions
258 lines (227 loc) · 7.3 KB
/
Copy pathImageMapGuide.js
File metadata and controls
258 lines (227 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @module ol/source/ImageMapGuide
*/
import ImageWrapper from '../Image.js';
import {listen} from '../events.js';
import EventType from '../events/EventType.js';
import {containsExtent, getCenter, getHeight, getWidth, scaleFromCenter} from '../extent.js';
import {assign} from '../obj.js';
import ImageSource, {defaultImageLoadFunction} from './Image.js';
import {appendParams} from '../uri.js';
/**
* @typedef {Object} Options
* @property {string} [url] The mapagent url.
* @property {null|string} [crossOrigin] The `crossOrigin` attribute for loaded images. Note that
* you must provide a `crossOrigin` value if you want to access pixel data with the Canvas renderer.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_enabled_image for more detail.
* @property {number} [displayDpi=96] The display resolution.
* @property {number} [metersPerUnit=1] The meters-per-unit value.
* @property {boolean} [hidpi=true] Use the `ol/Map#pixelRatio` value when requesting
* the image from the remote server.
* @property {boolean} [useOverlay] If `true`, will use `GETDYNAMICMAPOVERLAYIMAGE`.
* @property {import("../proj.js").ProjectionLike} projection Projection.
* @property {number} [ratio=1] Ratio. `1` means image requests are the size of the map viewport, `2` means
* twice the width and height of the map viewport, and so on. Must be `1` or higher.
* @property {Array<number>} [resolutions] Resolutions.
* If specified, requests will be made for these resolutions only.
* @property {import("../Image.js").LoadFunction} [imageLoadFunction] Optional function to load an image given a URL.
* @property {Object} [params] Additional parameters.
*/
/**
* @classdesc
* Source for images from Mapguide servers
*
* @fires module:ol/source/Image.ImageSourceEvent
* @api
*/
class ImageMapGuide extends ImageSource {
/**
* @param {Options} options ImageMapGuide options.
*/
constructor(options) {
super({
projection: options.projection,
resolutions: options.resolutions
});
/**
* @private
* @type {?string}
*/
this.crossOrigin_ =
options.crossOrigin !== undefined ? options.crossOrigin : null;
/**
* @private
* @type {number}
*/
this.displayDpi_ = options.displayDpi !== undefined ?
options.displayDpi : 96;
/**
* @private
* @type {!Object}
*/
this.params_ = options.params || {};
/**
* @private
* @type {string|undefined}
*/
this.url_ = options.url;
/**
* @private
* @type {import("../Image.js").LoadFunction}
*/
this.imageLoadFunction_ = options.imageLoadFunction !== undefined ?
options.imageLoadFunction : defaultImageLoadFunction;
/**
* @private
* @type {boolean}
*/
this.hidpi_ = options.hidpi !== undefined ? options.hidpi : true;
/**
* @private
* @type {number}
*/
this.metersPerUnit_ = options.metersPerUnit !== undefined ?
options.metersPerUnit : 1;
/**
* @private
* @type {number}
*/
this.ratio_ = options.ratio !== undefined ? options.ratio : 1;
/**
* @private
* @type {boolean}
*/
this.useOverlay_ = options.useOverlay !== undefined ?
options.useOverlay : false;
/**
* @private
* @type {import("../Image.js").default}
*/
this.image_ = null;
/**
* @private
* @type {number}
*/
this.renderedRevision_ = 0;
}
/**
* Get the user-provided params, i.e. those passed to the constructor through
* the "params" option, and possibly updated using the updateParams method.
* @return {Object} Params.
* @api
*/
getParams() {
return this.params_;
}
/**
* @inheritDoc
*/
getImageInternal(extent, resolution, pixelRatio, projection) {
resolution = this.findNearestResolution(resolution);
pixelRatio = this.hidpi_ ? pixelRatio : 1;
let image = this.image_;
if (image &&
this.renderedRevision_ == this.getRevision() &&
image.getResolution() == resolution &&
image.getPixelRatio() == pixelRatio &&
containsExtent(image.getExtent(), extent)) {
return image;
}
if (this.ratio_ != 1) {
extent = extent.slice();
scaleFromCenter(extent, this.ratio_);
}
const width = getWidth(extent) / resolution;
const height = getHeight(extent) / resolution;
const size = [width * pixelRatio, height * pixelRatio];
if (this.url_ !== undefined) {
const imageUrl = this.getUrl(this.url_, this.params_, extent, size,
projection);
image = new ImageWrapper(extent, resolution, pixelRatio,
imageUrl, this.crossOrigin_,
this.imageLoadFunction_);
listen(image, EventType.CHANGE,
this.handleImageChange, this);
} else {
image = null;
}
this.image_ = image;
this.renderedRevision_ = this.getRevision();
return image;
}
/**
* Return the image load function of the source.
* @return {import("../Image.js").LoadFunction} The image load function.
* @api
*/
getImageLoadFunction() {
return this.imageLoadFunction_;
}
/**
* Update the user-provided params.
* @param {Object} params Params.
* @api
*/
updateParams(params) {
assign(this.params_, params);
this.changed();
}
/**
* @param {string} baseUrl The mapagent url.
* @param {Object<string, string|number>} params Request parameters.
* @param {import("../extent.js").Extent} extent Extent.
* @param {import("../size.js").Size} size Size.
* @param {import("../proj/Projection.js").default} projection Projection.
* @return {string} The mapagent map image request URL.
*/
getUrl(baseUrl, params, extent, size, projection) {
const scale = getScale(extent, size,
this.metersPerUnit_, this.displayDpi_);
const center = getCenter(extent);
const baseParams = {
'OPERATION': this.useOverlay_ ? 'GETDYNAMICMAPOVERLAYIMAGE' : 'GETMAPIMAGE',
'VERSION': '2.0.0',
'LOCALE': 'en',
'CLIENTAGENT': 'ol/source/ImageMapGuide source',
'CLIP': '1',
'SETDISPLAYDPI': this.displayDpi_,
'SETDISPLAYWIDTH': Math.round(size[0]),
'SETDISPLAYHEIGHT': Math.round(size[1]),
'SETVIEWSCALE': scale,
'SETVIEWCENTERX': center[0],
'SETVIEWCENTERY': center[1]
};
assign(baseParams, params);
return appendParams(baseUrl, baseParams);
}
/**
* Set the image load function of the MapGuide source.
* @param {import("../Image.js").LoadFunction} imageLoadFunction Image load function.
* @api
*/
setImageLoadFunction(imageLoadFunction) {
this.image_ = null;
this.imageLoadFunction_ = imageLoadFunction;
this.changed();
}
}
/**
* @param {import("../extent.js").Extent} extent The map extents.
* @param {import("../size.js").Size} size The viewport size.
* @param {number} metersPerUnit The meters-per-unit value.
* @param {number} dpi The display resolution.
* @return {number} The computed map scale.
*/
function getScale(extent, size, metersPerUnit, dpi) {
const mcsW = getWidth(extent);
const mcsH = getHeight(extent);
const devW = size[0];
const devH = size[1];
const mpp = 0.0254 / dpi;
if (devH * mcsW > devW * mcsH) {
return mcsW * metersPerUnit / (devW * mpp); // width limited
} else {
return mcsH * metersPerUnit / (devH * mpp); // height limited
}
}
export default ImageMapGuide;