forked from Esri/esri-leaflet
/
DynamicMapLayer.js
205 lines (167 loc) · 5.49 KB
/
DynamicMapLayer.js
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
import { Util } from 'leaflet';
import { RasterLayer } from './RasterLayer';
import { getUrlParams } from '../Util';
import mapService from '../Services/MapService';
export var DynamicMapLayer = RasterLayer.extend({
options: {
updateInterval: 150,
layers: false,
layerDefs: false,
timeOptions: false,
format: 'png24',
transparent: true,
f: 'json'
},
initialize: function (options) {
options = getUrlParams(options);
this.service = mapService(options);
this.service.addEventParent(this);
if ((options.proxy || options.token) && options.f !== 'json') {
options.f = 'json';
}
Util.setOptions(this, options);
},
getDynamicLayers: function () {
return this.options.dynamicLayers;
},
setDynamicLayers: function (dynamicLayers) {
this.options.dynamicLayers = dynamicLayers;
this._update();
return this;
},
getLayers: function () {
return this.options.layers;
},
setLayers: function (layers) {
this.options.layers = layers;
this._update();
return this;
},
getLayerDefs: function () {
return this.options.layerDefs;
},
setLayerDefs: function (layerDefs) {
this.options.layerDefs = layerDefs;
this._update();
return this;
},
getTimeOptions: function () {
return this.options.timeOptions;
},
setTimeOptions: function (timeOptions) {
this.options.timeOptions = timeOptions;
this._update();
return this;
},
query: function () {
return this.service.query();
},
identify: function () {
return this.service.identify();
},
find: function () {
return this.service.find();
},
_getPopupData: function (e) {
var callback = Util.bind(function (error, featureCollection, response) {
if (error) { return; } // we really can't do anything here but authenticate or requesterror will fire
setTimeout(Util.bind(function () {
this._renderPopup(e.latlng, error, featureCollection, response);
}, this), 300);
}, this);
var identifyRequest;
if (this.options.popup) {
identifyRequest = this.options.popup.on(this._map).at(e.latlng);
} else {
identifyRequest = this.identify().on(this._map).at(e.latlng);
}
// remove extraneous vertices from response features if it has not already been done
identifyRequest.params.maxAllowableOffset ? true : identifyRequest.simplify(this._map, 0.5);
if (!(this.options.popup && this.options.popup.params && this.options.popup.params.layers)) {
if (this.options.layers) {
identifyRequest.layers('visible:' + this.options.layers.join(','));
} else {
identifyRequest.layers('visible');
}
}
// if present, pass layer ids and sql filters through to the identify task
if (this.options.layerDefs && typeof this.options.layerDefs !== 'string' && !identifyRequest.params.layerDefs) {
for (var id in this.options.layerDefs) {
if (this.options.layerDefs.hasOwnProperty(id)) {
identifyRequest.layerDef(id, this.options.layerDefs[id]);
}
}
}
identifyRequest.run(callback);
// set the flags to show the popup
this._shouldRenderPopup = true;
this._lastClick = e.latlng;
},
_buildExportParams: function () {
var sr = parseInt(this._map.options.crs.code.split(':')[1], 10);
var params = {
bbox: this._calculateBbox(),
size: this._calculateImageSize(),
dpi: 96,
format: this.options.format,
transparent: this.options.transparent,
bboxSR: sr,
imageSR: sr
};
if (this.options.dynamicLayers) {
params.dynamicLayers = this.options.dynamicLayers;
}
if (this.options.layers) {
if (this.options.layers.length === 0) {
return;
} else {
params.layers = 'show:' + this.options.layers.join(',');
}
}
if (this.options.layerDefs) {
params.layerDefs = typeof this.options.layerDefs === 'string' ? this.options.layerDefs : JSON.stringify(this.options.layerDefs);
}
if (this.options.timeOptions) {
params.timeOptions = JSON.stringify(this.options.timeOptions);
}
if (this.options.from && this.options.to) {
params.time = this.options.from.valueOf() + ',' + this.options.to.valueOf();
}
if (this.service.options.token) {
params.token = this.service.options.token;
}
if (this.options.proxy) {
params.proxy = this.options.proxy;
}
// use a timestamp to bust server cache
if (this.options.disableCache) {
params._ts = Date.now();
}
return params;
},
_requestExport: function (params, bounds) {
if (this.options.f === 'json') {
this.service.request('export', params, function (error, response) {
if (error) { return; } // we really can't do anything here but authenticate or requesterror will fire
if (this.options.token && response.href) {
response.href += ('?token=' + this.options.token);
}
if (this.options.proxy && response.href) {
response.href = this.options.proxy + '?' + response.href;
}
if (response.href) {
this._renderImage(response.href, bounds);
} else {
this._renderImage(response.imageData, bounds, response.contentType);
}
}, this);
} else {
params.f = 'image';
this._renderImage(this.options.url + 'export' + Util.getParamString(params), bounds);
}
}
});
export function dynamicMapLayer (url, options) {
return new DynamicMapLayer(url, options);
}
export default dynamicMapLayer;