1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Layer/Grid.js |
---|
8 | * @requires OpenLayers/Tile/Image.js |
---|
9 | * @requires OpenLayers/Format/ArcXML.js |
---|
10 | * @requires OpenLayers/Request.js |
---|
11 | */ |
---|
12 | |
---|
13 | /** |
---|
14 | * Class: OpenLayers.Layer.ArcIMS |
---|
15 | * Instances of OpenLayers.Layer.ArcIMS are used to display data from ESRI ArcIMS |
---|
16 | * Mapping Services. Create a new ArcIMS layer with the <OpenLayers.Layer.ArcIMS> |
---|
17 | * constructor. |
---|
18 | * |
---|
19 | * Inherits from: |
---|
20 | * - <OpenLayers.Layer.Grid> |
---|
21 | */ |
---|
22 | OpenLayers.Layer.ArcIMS = OpenLayers.Class(OpenLayers.Layer.Grid, { |
---|
23 | |
---|
24 | /** |
---|
25 | * Constant: DEFAULT_PARAMS |
---|
26 | * {Object} Default query string parameters. |
---|
27 | */ |
---|
28 | DEFAULT_PARAMS: { |
---|
29 | ClientVersion: "9.2", |
---|
30 | ServiceName: '' |
---|
31 | }, |
---|
32 | |
---|
33 | /** |
---|
34 | * APIProperty: tileSize |
---|
35 | * {<OpenLayers.Size>} Size for tiles. Default is 512x512. |
---|
36 | */ |
---|
37 | tileSize: null, |
---|
38 | |
---|
39 | /** |
---|
40 | * APIProperty: featureCoordSys |
---|
41 | * {String} Code for feature coordinate system. Default is "4326". |
---|
42 | */ |
---|
43 | featureCoordSys: "4326", |
---|
44 | |
---|
45 | /** |
---|
46 | * APIProperty: filterCoordSys |
---|
47 | * {String} Code for filter coordinate system. Default is "4326". |
---|
48 | */ |
---|
49 | filterCoordSys: "4326", |
---|
50 | |
---|
51 | /** |
---|
52 | * APIProperty: layers |
---|
53 | * {Array} An array of objects with layer properties. |
---|
54 | */ |
---|
55 | layers: null, |
---|
56 | |
---|
57 | /** |
---|
58 | * APIProperty: async |
---|
59 | * {Boolean} Request images asynchronously. Default is true. |
---|
60 | */ |
---|
61 | async: true, |
---|
62 | |
---|
63 | /** |
---|
64 | * APIProperty: name |
---|
65 | * {String} Layer name. Default is "ArcIMS". |
---|
66 | */ |
---|
67 | name: "ArcIMS", |
---|
68 | |
---|
69 | /** |
---|
70 | * APIProperty: isBaseLayer |
---|
71 | * {Boolean} The layer is a base layer. Default is true. |
---|
72 | */ |
---|
73 | isBaseLayer: true, |
---|
74 | |
---|
75 | /** |
---|
76 | * Constant: DEFAULT_OPTIONS |
---|
77 | * {Object} Default layers properties. |
---|
78 | */ |
---|
79 | DEFAULT_OPTIONS: { |
---|
80 | tileSize: new OpenLayers.Size(512, 512), |
---|
81 | featureCoordSys: "4326", |
---|
82 | filterCoordSys: "4326", |
---|
83 | layers: null, |
---|
84 | isBaseLayer: true, |
---|
85 | async: true, |
---|
86 | name: "ArcIMS" |
---|
87 | }, |
---|
88 | |
---|
89 | /** |
---|
90 | * Constructor: OpenLayers.Layer.ArcIMS |
---|
91 | * Create a new ArcIMS layer object. |
---|
92 | * |
---|
93 | * Example: |
---|
94 | * (code) |
---|
95 | * var arcims = new OpenLayers.Layer.ArcIMS( |
---|
96 | * "Global Sample", |
---|
97 | * "http://sample.avencia.com/servlet/com.esri.esrimap.Esrimap", |
---|
98 | * { |
---|
99 | * service: "OpenLayers_Sample", |
---|
100 | * layers: [ |
---|
101 | * // layers to manipulate |
---|
102 | * {id: "1", visible: true} |
---|
103 | * ] |
---|
104 | * } |
---|
105 | * ); |
---|
106 | * (end) |
---|
107 | * |
---|
108 | * Parameters: |
---|
109 | * name - {String} A name for the layer |
---|
110 | * url - {String} Base url for the ArcIMS server |
---|
111 | * options - {Object} Optional object with properties to be set on the |
---|
112 | * layer. |
---|
113 | */ |
---|
114 | initialize: function(name, url, options) { |
---|
115 | |
---|
116 | this.tileSize = new OpenLayers.Size(512, 512); |
---|
117 | |
---|
118 | // parameters |
---|
119 | this.params = OpenLayers.Util.applyDefaults( |
---|
120 | {ServiceName: options.serviceName}, |
---|
121 | this.DEFAULT_PARAMS |
---|
122 | ); |
---|
123 | this.options = OpenLayers.Util.applyDefaults( |
---|
124 | options, this.DEFAULT_OPTIONS |
---|
125 | ); |
---|
126 | |
---|
127 | OpenLayers.Layer.Grid.prototype.initialize.apply( |
---|
128 | this, [name, url, this.params, options] |
---|
129 | ); |
---|
130 | |
---|
131 | //layer is transparent |
---|
132 | if (this.transparent) { |
---|
133 | |
---|
134 | // unless explicitly set in options, make layer an overlay |
---|
135 | if (!this.isBaseLayer) { |
---|
136 | this.isBaseLayer = false; |
---|
137 | } |
---|
138 | |
---|
139 | // jpegs can never be transparent, so intelligently switch the |
---|
140 | // format, depending on the browser's capabilities |
---|
141 | if (this.format == "image/jpeg") { |
---|
142 | this.format = OpenLayers.Util.alphaHack() ? "image/gif" : "image/png"; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | // create an empty layer list if no layers specified in the options |
---|
147 | if (this.options.layers === null) { |
---|
148 | this.options.layers = []; |
---|
149 | } |
---|
150 | }, |
---|
151 | |
---|
152 | |
---|
153 | /** |
---|
154 | * Method: destroy |
---|
155 | * Destroy this layer |
---|
156 | */ |
---|
157 | destroy: function() { |
---|
158 | // for now, nothing special to do here. |
---|
159 | OpenLayers.Layer.Grid.prototype.destroy.apply(this, arguments); |
---|
160 | }, |
---|
161 | |
---|
162 | |
---|
163 | /** |
---|
164 | * Method: getURL |
---|
165 | * Return an image url this layer. |
---|
166 | * |
---|
167 | * Parameters: |
---|
168 | * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox for the |
---|
169 | * request. |
---|
170 | * |
---|
171 | * Returns: |
---|
172 | * {String} A string with the map image's url. |
---|
173 | */ |
---|
174 | getURL: function(bounds) { |
---|
175 | var url = ""; |
---|
176 | bounds = this.adjustBounds(bounds); |
---|
177 | |
---|
178 | // create an arcxml request to generate the image |
---|
179 | var axlReq = new OpenLayers.Format.ArcXML( |
---|
180 | OpenLayers.Util.extend(this.options, { |
---|
181 | requesttype: "image", |
---|
182 | envelope: bounds.toArray(), |
---|
183 | tileSize: this.tileSize |
---|
184 | }) |
---|
185 | ); |
---|
186 | |
---|
187 | // create a synchronous ajax request to get an arcims image |
---|
188 | var req = new OpenLayers.Request.POST({ |
---|
189 | url: this.getFullRequestString(), |
---|
190 | data: axlReq.write(), |
---|
191 | async: false |
---|
192 | }); |
---|
193 | |
---|
194 | // if the response exists |
---|
195 | if (req != null) { |
---|
196 | var doc = req.responseXML; |
---|
197 | |
---|
198 | if (!doc || !doc.documentElement) { |
---|
199 | doc = req.responseText; |
---|
200 | } |
---|
201 | |
---|
202 | // create a new arcxml format to read the response |
---|
203 | var axlResp = new OpenLayers.Format.ArcXML(); |
---|
204 | var arcxml = axlResp.read(doc); |
---|
205 | url = this.getUrlOrImage(arcxml.image.output); |
---|
206 | } |
---|
207 | |
---|
208 | return url; |
---|
209 | }, |
---|
210 | |
---|
211 | |
---|
212 | /** |
---|
213 | * Method: getURLasync |
---|
214 | * Get an image url this layer asynchronously, and execute a callback |
---|
215 | * when the image url is generated. |
---|
216 | * |
---|
217 | * Parameters: |
---|
218 | * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox for the |
---|
219 | * request. |
---|
220 | * scope - {Object} The scope of the callback method. |
---|
221 | * prop - {String} The name of the property in the scoped object to |
---|
222 | * recieve the image url. |
---|
223 | * callback - {Function} Function to call when image url is retrieved. |
---|
224 | */ |
---|
225 | getURLasync: function(bounds, scope, prop, callback) { |
---|
226 | bounds = this.adjustBounds(bounds); |
---|
227 | |
---|
228 | // create an arcxml request to generate the image |
---|
229 | var axlReq = new OpenLayers.Format.ArcXML( |
---|
230 | OpenLayers.Util.extend(this.options, { |
---|
231 | requesttype: "image", |
---|
232 | envelope: bounds.toArray(), |
---|
233 | tileSize: this.tileSize |
---|
234 | }) |
---|
235 | ); |
---|
236 | |
---|
237 | // create an asynchronous ajax request to get an arcims image |
---|
238 | OpenLayers.Request.POST({ |
---|
239 | url: this.getFullRequestString(), |
---|
240 | async: true, |
---|
241 | data: axlReq.write(), |
---|
242 | callback: function(req) { |
---|
243 | // process the response from ArcIMS, and call the callback function |
---|
244 | // to set the image URL |
---|
245 | var doc = req.responseXML; |
---|
246 | if (!doc || !doc.documentElement) { |
---|
247 | doc = req.responseText; |
---|
248 | } |
---|
249 | |
---|
250 | // create a new arcxml format to read the response |
---|
251 | var axlResp = new OpenLayers.Format.ArcXML(); |
---|
252 | var arcxml = axlResp.read(doc); |
---|
253 | |
---|
254 | scope[prop] = this.getUrlOrImage(arcxml.image.output); |
---|
255 | |
---|
256 | // call the callback function to recieve the updated property on the |
---|
257 | // scoped object |
---|
258 | callback.apply(scope); |
---|
259 | }, |
---|
260 | scope: this |
---|
261 | }); |
---|
262 | }, |
---|
263 | |
---|
264 | /** |
---|
265 | * Method: getUrlOrImage |
---|
266 | * Extract a url or image from the ArcXML image output. |
---|
267 | * |
---|
268 | * Parameters: |
---|
269 | * output - {Object} The image.output property of the object returned from |
---|
270 | * the ArcXML format read method. |
---|
271 | * |
---|
272 | * Returns: |
---|
273 | * {String} A URL for an image (potentially with the data protocol). |
---|
274 | */ |
---|
275 | getUrlOrImage: function(output) { |
---|
276 | var ret = ""; |
---|
277 | if(output.url) { |
---|
278 | // If the image response output url is a string, then the image |
---|
279 | // data is not inline. |
---|
280 | ret = output.url; |
---|
281 | } else if(output.data) { |
---|
282 | // The image data is inline and base64 encoded, create a data |
---|
283 | // url for the image. This will only work for small images, |
---|
284 | // due to browser url length limits. |
---|
285 | ret = "data:image/" + output.type + |
---|
286 | ";base64," + output.data; |
---|
287 | } |
---|
288 | return ret; |
---|
289 | }, |
---|
290 | |
---|
291 | /** |
---|
292 | * Method: setLayerQuery |
---|
293 | * Set the query definition on this layer. Query definitions are used to |
---|
294 | * render parts of the spatial data in an image, and can be used to |
---|
295 | * filter features or layers in the ArcIMS service. |
---|
296 | * |
---|
297 | * Parameters: |
---|
298 | * id - {String} The ArcIMS layer ID. |
---|
299 | * queryDef - {Object} The query definition to apply to this layer. |
---|
300 | */ |
---|
301 | setLayerQuery: function(id, querydef) { |
---|
302 | // find the matching layer, if it exists |
---|
303 | for (var lyr = 0; lyr < this.options.layers.length; lyr++) { |
---|
304 | if (id == this.options.layers[lyr].id) { |
---|
305 | // replace this layer definition |
---|
306 | this.options.layers[lyr].query = querydef; |
---|
307 | return; |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | // no layer found, create a new definition |
---|
312 | this.options.layers.push({id: id, visible: true, query: querydef}); |
---|
313 | }, |
---|
314 | |
---|
315 | /** |
---|
316 | * Method: getFeatureInfo |
---|
317 | * Get feature information from ArcIMS. Using the applied geometry, apply |
---|
318 | * the options to the query (buffer, area/envelope intersection), and |
---|
319 | * query the ArcIMS service. |
---|
320 | * |
---|
321 | * A note about accuracy: |
---|
322 | * ArcIMS interprets the accuracy attribute in feature requests to be |
---|
323 | * something like the 'modulus' operator on feature coordinates, |
---|
324 | * applied to the database geometry of the feature. It doesn't round, |
---|
325 | * so your feature coordinates may be up to (1 x accuracy) offset from |
---|
326 | * the actual feature coordinates. If the accuracy of the layer is not |
---|
327 | * specified, the accuracy will be computed to be approximately 1 |
---|
328 | * feature coordinate per screen pixel. |
---|
329 | * |
---|
330 | * Parameters: |
---|
331 | * geometry - {<OpenLayers.LonLat>} or {<OpenLayers.Geometry.Polygon>} The |
---|
332 | * geometry to use when making the query. This should be a closed |
---|
333 | * polygon for behavior approximating a free selection. |
---|
334 | * layer - {Object} The ArcIMS layer definition. This is an anonymous object |
---|
335 | * that looks like: |
---|
336 | * (code) |
---|
337 | * { |
---|
338 | * id: "ArcXML layer ID", // the ArcXML layer ID |
---|
339 | * query: { |
---|
340 | * where: "STATE = 'PA'", // the where clause of the query |
---|
341 | * accuracy: 100 // the accuracy of the returned feature |
---|
342 | * } |
---|
343 | * } |
---|
344 | * (end) |
---|
345 | * options - {Object} Object with non-default properties to set on the layer. |
---|
346 | * Supported properties are buffer, callback, scope, and any other |
---|
347 | * properties applicable to the ArcXML format. Set the 'callback' and |
---|
348 | * 'scope' for an object and function to recieve the parsed features |
---|
349 | * from ArcIMS. |
---|
350 | */ |
---|
351 | getFeatureInfo: function(geometry, layer, options) { |
---|
352 | // set the buffer to 1 unit (dd/m/ft?) by default |
---|
353 | var buffer = options.buffer || 1; |
---|
354 | // empty callback by default |
---|
355 | var callback = options.callback || function() {}; |
---|
356 | // default scope is window (global) |
---|
357 | var scope = options.scope || window; |
---|
358 | |
---|
359 | // apply these option to the request options |
---|
360 | var requestOptions = {}; |
---|
361 | OpenLayers.Util.extend(requestOptions, this.options); |
---|
362 | |
---|
363 | // this is a feature request |
---|
364 | requestOptions.requesttype = "feature"; |
---|
365 | |
---|
366 | if (geometry instanceof OpenLayers.LonLat) { |
---|
367 | // create an envelope if the geometry is really a lon/lat |
---|
368 | requestOptions.polygon = null; |
---|
369 | requestOptions.envelope = [ |
---|
370 | geometry.lon - buffer, |
---|
371 | geometry.lat - buffer, |
---|
372 | geometry.lon + buffer, |
---|
373 | geometry.lat + buffer |
---|
374 | ]; |
---|
375 | } else if (geometry instanceof OpenLayers.Geometry.Polygon) { |
---|
376 | // use the polygon assigned, and empty the envelope |
---|
377 | requestOptions.envelope = null; |
---|
378 | requestOptions.polygon = geometry; |
---|
379 | } |
---|
380 | |
---|
381 | // create an arcxml request to get feature requests |
---|
382 | var arcxml = new OpenLayers.Format.ArcXML(requestOptions); |
---|
383 | |
---|
384 | // apply any get feature options to the arcxml request |
---|
385 | OpenLayers.Util.extend(arcxml.request.get_feature, options); |
---|
386 | |
---|
387 | arcxml.request.get_feature.layer = layer.id; |
---|
388 | if (typeof layer.query.accuracy == "number") { |
---|
389 | // set the accuracy if it was specified |
---|
390 | arcxml.request.get_feature.query.accuracy = layer.query.accuracy; |
---|
391 | } else { |
---|
392 | // guess that the accuracy is 1 per screen pixel |
---|
393 | var mapCenter = this.map.getCenter(); |
---|
394 | var viewPx = this.map.getViewPortPxFromLonLat(mapCenter); |
---|
395 | viewPx.x++; |
---|
396 | var mapOffCenter = this.map.getLonLatFromPixel(viewPx); |
---|
397 | arcxml.request.get_feature.query.accuracy = mapOffCenter.lon - mapCenter.lon; |
---|
398 | } |
---|
399 | |
---|
400 | // set the get_feature query to be the same as the layer passed in |
---|
401 | arcxml.request.get_feature.query.where = layer.query.where; |
---|
402 | |
---|
403 | // use area_intersection |
---|
404 | arcxml.request.get_feature.query.spatialfilter.relation = "area_intersection"; |
---|
405 | |
---|
406 | // create a new asynchronous request to get the feature info |
---|
407 | OpenLayers.Request.POST({ |
---|
408 | url: this.getFullRequestString({'CustomService': 'Query'}), |
---|
409 | data: arcxml.write(), |
---|
410 | callback: function(request) { |
---|
411 | // parse the arcxml response |
---|
412 | var response = arcxml.parseResponse(request.responseText); |
---|
413 | |
---|
414 | if (!arcxml.iserror()) { |
---|
415 | // if the arcxml is not an error, call the callback with the features parsed |
---|
416 | callback.call(scope, response.features); |
---|
417 | } else { |
---|
418 | // if the arcxml is an error, return null features selected |
---|
419 | callback.call(scope, null); |
---|
420 | } |
---|
421 | } |
---|
422 | }); |
---|
423 | }, |
---|
424 | |
---|
425 | /** |
---|
426 | * Method: clone |
---|
427 | * Create a clone of this layer |
---|
428 | * |
---|
429 | * Returns: |
---|
430 | * {<OpenLayers.Layer.ArcIMS>} An exact clone of this layer |
---|
431 | */ |
---|
432 | clone: function (obj) { |
---|
433 | |
---|
434 | if (obj == null) { |
---|
435 | obj = new OpenLayers.Layer.ArcIMS(this.name, |
---|
436 | this.url, |
---|
437 | this.getOptions()); |
---|
438 | } |
---|
439 | |
---|
440 | //get all additions from superclasses |
---|
441 | obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); |
---|
442 | |
---|
443 | // copy/set any non-init, non-simple values here |
---|
444 | |
---|
445 | return obj; |
---|
446 | }, |
---|
447 | |
---|
448 | /** |
---|
449 | * Method: addTile |
---|
450 | * addTile creates a tile, initializes it, and adds it to the layer div. |
---|
451 | * |
---|
452 | * Parameters: |
---|
453 | * bounds - {<OpenLayers.Bounds>} |
---|
454 | * position - {<OpenLayers.Pixel>} |
---|
455 | * |
---|
456 | * Returns: |
---|
457 | * {<OpenLayers.Tile.Image>} The added image tile. |
---|
458 | */ |
---|
459 | addTile:function(bounds,position) { |
---|
460 | return new OpenLayers.Tile.Image( |
---|
461 | this, position, bounds, null, this.tileSize |
---|
462 | ); |
---|
463 | }, |
---|
464 | |
---|
465 | CLASS_NAME: "OpenLayers.Layer.ArcIMS" |
---|
466 | }); |
---|