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/Request/XMLHttpRequest.js |
---|
8 | * @requires OpenLayers/Layer/Grid.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Layer.MapGuide |
---|
13 | * Instances of OpenLayers.Layer.MapGuide are used to display |
---|
14 | * data from a MapGuide OS instance. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Layer.Grid> |
---|
18 | */ |
---|
19 | OpenLayers.Layer.MapGuide = OpenLayers.Class(OpenLayers.Layer.Grid, { |
---|
20 | |
---|
21 | /** |
---|
22 | * APIProperty: isBaseLayer |
---|
23 | * {Boolean} Treat this layer as a base layer. Default is true. |
---|
24 | **/ |
---|
25 | isBaseLayer: true, |
---|
26 | |
---|
27 | /** |
---|
28 | * APIProperty: useHttpTile |
---|
29 | * {Boolean} use a tile cache exposed directly via a webserver rather than the |
---|
30 | * via mapguide server. This does require extra configuration on the Mapguide Server, |
---|
31 | * and will only work when singleTile is false. The url for the layer must be set to the |
---|
32 | * webserver path rather than the Mapguide mapagent. |
---|
33 | * See http://trac.osgeo.org/mapguide/wiki/CodeSamples/Tiles/ServingTilesViaHttp |
---|
34 | **/ |
---|
35 | useHttpTile: false, |
---|
36 | |
---|
37 | /** |
---|
38 | * APIProperty: singleTile |
---|
39 | * {Boolean} use tile server or request single tile image. |
---|
40 | **/ |
---|
41 | singleTile: false, |
---|
42 | |
---|
43 | /** |
---|
44 | * APIProperty: useOverlay |
---|
45 | * {Boolean} flag to indicate if the layer should be retrieved using |
---|
46 | * GETMAPIMAGE (default) or using GETDYNAMICOVERLAY requests. |
---|
47 | **/ |
---|
48 | useOverlay: false, |
---|
49 | |
---|
50 | /** |
---|
51 | * APIProperty: useAsyncOverlay |
---|
52 | * {Boolean} indicates if the MapGuide site supports the asynchronous |
---|
53 | * GETDYNAMICOVERLAY requests which is available in MapGuide Enterprise 2010 |
---|
54 | * and MapGuide Open Source v2.0.3 or higher. The newer versions of MG |
---|
55 | * is called asynchronously, allows selections to be drawn separately from |
---|
56 | * the map and offers styling options. |
---|
57 | * |
---|
58 | * With older versions of MapGuide, set useAsyncOverlay=false. Note that in |
---|
59 | * this case a synchronous AJAX call is issued and the mapname and session |
---|
60 | * parameters must be used to initialize the layer, not the mapdefinition |
---|
61 | * parameter. Also note that this will issue a synchronous AJAX request |
---|
62 | * before the image request can be issued so the users browser may lock |
---|
63 | * up if the MG Web tier does not respond in a timely fashion. |
---|
64 | **/ |
---|
65 | useAsyncOverlay: true, |
---|
66 | |
---|
67 | /** |
---|
68 | * Constant: TILE_PARAMS |
---|
69 | * {Object} Hashtable of default parameter key/value pairs for tiled layer |
---|
70 | */ |
---|
71 | TILE_PARAMS: { |
---|
72 | operation: 'GETTILEIMAGE', |
---|
73 | version: '1.2.0' |
---|
74 | }, |
---|
75 | |
---|
76 | /** |
---|
77 | * Constant: SINGLE_TILE_PARAMS |
---|
78 | * {Object} Hashtable of default parameter key/value pairs for untiled layer |
---|
79 | */ |
---|
80 | SINGLE_TILE_PARAMS: { |
---|
81 | operation: 'GETMAPIMAGE', |
---|
82 | format: 'PNG', |
---|
83 | locale: 'en', |
---|
84 | clip: '1', |
---|
85 | version: '1.0.0' |
---|
86 | }, |
---|
87 | |
---|
88 | /** |
---|
89 | * Constant: OVERLAY_PARAMS |
---|
90 | * {Object} Hashtable of default parameter key/value pairs for untiled layer |
---|
91 | */ |
---|
92 | OVERLAY_PARAMS: { |
---|
93 | operation: 'GETDYNAMICMAPOVERLAYIMAGE', |
---|
94 | format: 'PNG', |
---|
95 | locale: 'en', |
---|
96 | clip: '1', |
---|
97 | version: '2.0.0' |
---|
98 | }, |
---|
99 | |
---|
100 | /** |
---|
101 | * Constant: FOLDER_PARAMS |
---|
102 | * {Object} Hashtable of parameter key/value pairs which describe |
---|
103 | * the folder structure for tiles as configured in the mapguide |
---|
104 | * serverconfig.ini section [TileServiceProperties] |
---|
105 | */ |
---|
106 | FOLDER_PARAMS: { |
---|
107 | tileColumnsPerFolder: 30, |
---|
108 | tileRowsPerFolder: 30, |
---|
109 | format: 'png', |
---|
110 | querystring: null |
---|
111 | }, |
---|
112 | |
---|
113 | /** |
---|
114 | * Property: defaultSize |
---|
115 | * {<OpenLayers.Size>} Tile size as produced by MapGuide server |
---|
116 | **/ |
---|
117 | defaultSize: new OpenLayers.Size(300,300), |
---|
118 | |
---|
119 | /** |
---|
120 | * Constructor: OpenLayers.Layer.MapGuide |
---|
121 | * Create a new Mapguide layer, either tiled or untiled. |
---|
122 | * |
---|
123 | * For tiled layers, the 'groupName' and 'mapDefinition' values |
---|
124 | * must be specified as parameters in the constructor. |
---|
125 | * |
---|
126 | * For untiled base layers, specify either combination of 'mapName' and |
---|
127 | * 'session', or 'mapDefinition' and 'locale'. |
---|
128 | * |
---|
129 | * For older versions of MapGuide and overlay layers, set useAsyncOverlay |
---|
130 | * to false and in this case mapName and session are required parameters |
---|
131 | * for the constructor. |
---|
132 | * |
---|
133 | * NOTE: MapGuide OS uses a DPI value and degrees to meters conversion |
---|
134 | * factor that are different than the defaults used in OpenLayers, |
---|
135 | * so these must be adjusted accordingly in your application. |
---|
136 | * See the MapGuide example for how to set these values for MGOS. |
---|
137 | * |
---|
138 | * Parameters: |
---|
139 | * name - {String} Name of the layer displayed in the interface |
---|
140 | * url - {String} Location of the MapGuide mapagent executable |
---|
141 | * (e.g. http://localhost:8008/mapguide/mapagent/mapagent.fcgi) |
---|
142 | * params - {Object} hashtable of additional parameters to use. Some |
---|
143 | * parameters may require additional code on the server. The ones that |
---|
144 | * you may want to use are: |
---|
145 | * - mapDefinition - {String} The MapGuide resource definition |
---|
146 | * (e.g. Library://Samples/Gmap/Maps/gmapTiled.MapDefinition) |
---|
147 | * - locale - Locale setting |
---|
148 | * (for untiled overlays layers only) |
---|
149 | * - mapName - {String} Name of the map as stored in the MapGuide session. |
---|
150 | * (for untiled layers with a session parameter only) |
---|
151 | * - session - { String} MapGuide session ID |
---|
152 | * (for untiled overlays layers only) |
---|
153 | * - basemaplayergroupname - {String} GroupName for tiled MapGuide layers only |
---|
154 | * - format - Image format to be returned (for untiled overlay layers only) |
---|
155 | * - showLayers - {String} A comma separated list of GUID's for the |
---|
156 | * layers to display eg: 'cvc-xcv34,453-345-345sdf'. |
---|
157 | * - hideLayers - {String} A comma separated list of GUID's for the |
---|
158 | * layers to hide eg: 'cvc-xcv34,453-345-345sdf'. |
---|
159 | * - showGroups - {String} A comma separated list of GUID's for the |
---|
160 | * groups to display eg: 'cvc-xcv34,453-345-345sdf'. |
---|
161 | * - hideGroups - {String} A comma separated list of GUID's for the |
---|
162 | * groups to hide eg: 'cvc-xcv34,453-345-345sdf' |
---|
163 | * - selectionXml - {String} A selection xml string Some server plumbing |
---|
164 | * is required to read such a value. |
---|
165 | * options - {Ojbect} Hashtable of extra options to tag onto the layer; |
---|
166 | * will vary depending if tiled or untiled maps are being requested |
---|
167 | */ |
---|
168 | initialize: function(name, url, params, options) { |
---|
169 | |
---|
170 | OpenLayers.Layer.Grid.prototype.initialize.apply(this, arguments); |
---|
171 | |
---|
172 | // unless explicitly set in options, if the layer is transparent, |
---|
173 | // it will be an overlay |
---|
174 | if (options == null || options.isBaseLayer == null) { |
---|
175 | this.isBaseLayer = ((this.transparent != "true") && |
---|
176 | (this.transparent != true)); |
---|
177 | } |
---|
178 | |
---|
179 | if (options && options.useOverlay!=null) { |
---|
180 | this.useOverlay = options.useOverlay; |
---|
181 | } |
---|
182 | |
---|
183 | //initialize for untiled layers |
---|
184 | if (this.singleTile) { |
---|
185 | if (this.useOverlay) { |
---|
186 | OpenLayers.Util.applyDefaults( |
---|
187 | this.params, |
---|
188 | this.OVERLAY_PARAMS |
---|
189 | ); |
---|
190 | if (!this.useAsyncOverlay) { |
---|
191 | this.params.version = "1.0.0"; |
---|
192 | } |
---|
193 | } else { |
---|
194 | OpenLayers.Util.applyDefaults( |
---|
195 | this.params, |
---|
196 | this.SINGLE_TILE_PARAMS |
---|
197 | ); |
---|
198 | } |
---|
199 | } else { |
---|
200 | //initialize for tiled layers |
---|
201 | if (this.useHttpTile) { |
---|
202 | OpenLayers.Util.applyDefaults( |
---|
203 | this.params, |
---|
204 | this.FOLDER_PARAMS |
---|
205 | ); |
---|
206 | } else { |
---|
207 | OpenLayers.Util.applyDefaults( |
---|
208 | this.params, |
---|
209 | this.TILE_PARAMS |
---|
210 | ); |
---|
211 | } |
---|
212 | this.setTileSize(this.defaultSize); |
---|
213 | } |
---|
214 | }, |
---|
215 | |
---|
216 | /** |
---|
217 | * Method: clone |
---|
218 | * Create a clone of this layer |
---|
219 | * |
---|
220 | * Returns: |
---|
221 | * {<OpenLayers.Layer.MapGuide>} An exact clone of this layer |
---|
222 | */ |
---|
223 | clone: function (obj) { |
---|
224 | if (obj == null) { |
---|
225 | obj = new OpenLayers.Layer.MapGuide(this.name, |
---|
226 | this.url, |
---|
227 | this.params, |
---|
228 | this.getOptions()); |
---|
229 | } |
---|
230 | //get all additions from superclasses |
---|
231 | obj = OpenLayers.Layer.Grid.prototype.clone.apply(this, [obj]); |
---|
232 | |
---|
233 | return obj; |
---|
234 | }, |
---|
235 | |
---|
236 | /** |
---|
237 | * Method: addTile |
---|
238 | * Creates a tile, initializes it, and adds it to the layer div. |
---|
239 | * |
---|
240 | * Parameters: |
---|
241 | * bounds - {<OpenLayers.Bounds>} |
---|
242 | * position - {<OpenLayers.Pixel>} |
---|
243 | * |
---|
244 | * Returns: |
---|
245 | * {<OpenLayers.Tile.Image>} The added OpenLayers.Tile.Image |
---|
246 | */ |
---|
247 | addTile:function(bounds,position) { |
---|
248 | return new OpenLayers.Tile.Image(this, position, bounds, |
---|
249 | null, this.tileSize); |
---|
250 | }, |
---|
251 | |
---|
252 | /** |
---|
253 | * Method: getURL |
---|
254 | * Return a query string for this layer |
---|
255 | * |
---|
256 | * Parameters: |
---|
257 | * bounds - {<OpenLayers.Bounds>} A bounds representing the bbox |
---|
258 | * for the request |
---|
259 | * |
---|
260 | * Returns: |
---|
261 | * {String} A string with the layer's url and parameters and also |
---|
262 | * the passed-in bounds and appropriate tile size specified |
---|
263 | * as parameters. |
---|
264 | */ |
---|
265 | getURL: function (bounds) { |
---|
266 | var url; |
---|
267 | var center = bounds.getCenterLonLat(); |
---|
268 | var mapSize = this.map.getSize(); |
---|
269 | |
---|
270 | if (this.singleTile) { |
---|
271 | //set up the call for GETMAPIMAGE or GETDYNAMICMAPOVERLAY with |
---|
272 | //dynamic map parameters |
---|
273 | var params = { |
---|
274 | setdisplaydpi: OpenLayers.DOTS_PER_INCH, |
---|
275 | setdisplayheight: mapSize.h*this.ratio, |
---|
276 | setdisplaywidth: mapSize.w*this.ratio, |
---|
277 | setviewcenterx: center.lon, |
---|
278 | setviewcentery: center.lat, |
---|
279 | setviewscale: this.map.getScale() |
---|
280 | }; |
---|
281 | |
---|
282 | if (this.useOverlay && !this.useAsyncOverlay) { |
---|
283 | //first we need to call GETVISIBLEMAPEXTENT to set the extent |
---|
284 | var getVisParams = {}; |
---|
285 | getVisParams = OpenLayers.Util.extend(getVisParams, params); |
---|
286 | getVisParams.operation = "GETVISIBLEMAPEXTENT"; |
---|
287 | getVisParams.version = "1.0.0"; |
---|
288 | getVisParams.session = this.params.session; |
---|
289 | getVisParams.mapName = this.params.mapName; |
---|
290 | getVisParams.format = 'text/xml'; |
---|
291 | url = this.getFullRequestString( getVisParams ); |
---|
292 | |
---|
293 | OpenLayers.Request.GET({url: url, async: false}); |
---|
294 | } |
---|
295 | //construct the full URL |
---|
296 | url = this.getFullRequestString( params ); |
---|
297 | } else { |
---|
298 | |
---|
299 | //tiled version |
---|
300 | var currentRes = this.map.getResolution(); |
---|
301 | var colidx = Math.floor((bounds.left-this.maxExtent.left)/currentRes); |
---|
302 | colidx = Math.round(colidx/this.tileSize.w); |
---|
303 | var rowidx = Math.floor((this.maxExtent.top-bounds.top)/currentRes); |
---|
304 | rowidx = Math.round(rowidx/this.tileSize.h); |
---|
305 | |
---|
306 | if (this.useHttpTile){ |
---|
307 | url = this.getImageFilePath( |
---|
308 | { |
---|
309 | tilecol: colidx, |
---|
310 | tilerow: rowidx, |
---|
311 | scaleindex: this.resolutions.length - this.map.zoom - 1 |
---|
312 | }); |
---|
313 | |
---|
314 | } else { |
---|
315 | url = this.getFullRequestString( |
---|
316 | { |
---|
317 | tilecol: colidx, |
---|
318 | tilerow: rowidx, |
---|
319 | scaleindex: this.resolutions.length - this.map.zoom - 1 |
---|
320 | }); |
---|
321 | } |
---|
322 | } |
---|
323 | return url; |
---|
324 | }, |
---|
325 | |
---|
326 | /** |
---|
327 | * Method: getFullRequestString |
---|
328 | * getFullRequestString on MapGuide layers is special, because we |
---|
329 | * do a regular expression replace on ',' in parameters to '+'. |
---|
330 | * This is why it is subclassed here. |
---|
331 | * |
---|
332 | * Parameters: |
---|
333 | * altUrl - {String} Alternative base URL to use. |
---|
334 | * |
---|
335 | * Returns: |
---|
336 | * {String} A string with the layer's url appropriately encoded for MapGuide |
---|
337 | */ |
---|
338 | getFullRequestString:function(newParams, altUrl) { |
---|
339 | // use layer's url unless altUrl passed in |
---|
340 | var url = (altUrl == null) ? this.url : altUrl; |
---|
341 | |
---|
342 | // if url is not a string, it should be an array of strings, |
---|
343 | // in which case we will randomly select one of them in order |
---|
344 | // to evenly distribute requests to different urls. |
---|
345 | if (typeof url == "object") { |
---|
346 | url = url[Math.floor(Math.random()*url.length)]; |
---|
347 | } |
---|
348 | // requestString always starts with url |
---|
349 | var requestString = url; |
---|
350 | |
---|
351 | // create a new params hashtable with all the layer params and the |
---|
352 | // new params together. then convert to string |
---|
353 | var allParams = OpenLayers.Util.extend({}, this.params); |
---|
354 | allParams = OpenLayers.Util.extend(allParams, newParams); |
---|
355 | // ignore parameters that are already in the url search string |
---|
356 | var urlParams = OpenLayers.Util.upperCaseObject( |
---|
357 | OpenLayers.Util.getParameters(url)); |
---|
358 | for(var key in allParams) { |
---|
359 | if(key.toUpperCase() in urlParams) { |
---|
360 | delete allParams[key]; |
---|
361 | } |
---|
362 | } |
---|
363 | var paramsString = OpenLayers.Util.getParameterString(allParams); |
---|
364 | |
---|
365 | /* MapGuide needs '+' seperating things like bounds/height/width. |
---|
366 | Since typically this is URL encoded, we use a slight hack: we |
---|
367 | depend on the list-like functionality of getParameterString to |
---|
368 | leave ',' only in the case of list items (since otherwise it is |
---|
369 | encoded) then do a regular expression replace on the , characters |
---|
370 | to '+' */ |
---|
371 | paramsString = paramsString.replace(/,/g, "+"); |
---|
372 | |
---|
373 | if (paramsString != "") { |
---|
374 | var lastServerChar = url.charAt(url.length - 1); |
---|
375 | if ((lastServerChar == "&") || (lastServerChar == "?")) { |
---|
376 | requestString += paramsString; |
---|
377 | } else { |
---|
378 | if (url.indexOf('?') == -1) { |
---|
379 | //serverPath has no ? -- add one |
---|
380 | requestString += '?' + paramsString; |
---|
381 | } else { |
---|
382 | //serverPath contains ?, so must already have paramsString at the end |
---|
383 | requestString += '&' + paramsString; |
---|
384 | } |
---|
385 | } |
---|
386 | } |
---|
387 | return requestString; |
---|
388 | }, |
---|
389 | |
---|
390 | /** |
---|
391 | * Method: getImageFilePath |
---|
392 | * special handler to request mapguide tiles from an http exposed tilecache |
---|
393 | * |
---|
394 | * Parameters: |
---|
395 | * altUrl - {String} Alternative base URL to use. |
---|
396 | * |
---|
397 | * Returns: |
---|
398 | * {String} A string with the url for the tile image |
---|
399 | */ |
---|
400 | getImageFilePath:function(newParams, altUrl) { |
---|
401 | // use layer's url unless altUrl passed in |
---|
402 | var url = (altUrl == null) ? this.url : altUrl; |
---|
403 | |
---|
404 | // if url is not a string, it should be an array of strings, |
---|
405 | // in which case we will randomly select one of them in order |
---|
406 | // to evenly distribute requests to different urls. |
---|
407 | if (typeof url == "object") { |
---|
408 | url = url[Math.floor(Math.random()*url.length)]; |
---|
409 | } |
---|
410 | // requestString always starts with url |
---|
411 | var requestString = url; |
---|
412 | |
---|
413 | var tileRowGroup = ""; |
---|
414 | var tileColGroup = ""; |
---|
415 | |
---|
416 | if (newParams.tilerow < 0) { |
---|
417 | tileRowGroup = '-'; |
---|
418 | } |
---|
419 | |
---|
420 | if (newParams.tilerow == 0 ) { |
---|
421 | tileRowGroup += '0'; |
---|
422 | } else { |
---|
423 | tileRowGroup += Math.floor(Math.abs(newParams.tilerow/this.params.tileRowsPerFolder)) * this.params.tileRowsPerFolder; |
---|
424 | } |
---|
425 | |
---|
426 | if (newParams.tilecol < 0) { |
---|
427 | tileColGroup = '-'; |
---|
428 | } |
---|
429 | |
---|
430 | if (newParams.tilecol == 0) { |
---|
431 | tileColGroup += '0'; |
---|
432 | } else { |
---|
433 | tileColGroup += Math.floor(Math.abs(newParams.tilecol/this.params.tileColumnsPerFolder)) * this.params.tileColumnsPerFolder; |
---|
434 | } |
---|
435 | |
---|
436 | var tilePath = '/S' + Math.floor(newParams.scaleindex) |
---|
437 | + '/' + this.params.basemaplayergroupname |
---|
438 | + '/R' + tileRowGroup |
---|
439 | + '/C' + tileColGroup |
---|
440 | + '/' + (newParams.tilerow % this.params.tileRowsPerFolder) |
---|
441 | + '_' + (newParams.tilecol % this.params.tileColumnsPerFolder) |
---|
442 | + '.' + this.params.format; |
---|
443 | |
---|
444 | if (this.params.querystring) { |
---|
445 | tilePath += "?" + this.params.querystring; |
---|
446 | } |
---|
447 | |
---|
448 | requestString += tilePath; |
---|
449 | return requestString; |
---|
450 | }, |
---|
451 | |
---|
452 | /** |
---|
453 | * Method: calculateGridLayout |
---|
454 | * Generate parameters for the grid layout. This |
---|
455 | * |
---|
456 | * Parameters: |
---|
457 | * bounds - {<OpenLayers.Bound>} |
---|
458 | * extent - {<OpenLayers.Bounds>} |
---|
459 | * resolution - {Number} |
---|
460 | * |
---|
461 | * Returns: |
---|
462 | * Object containing properties tilelon, tilelat, tileoffsetlat, |
---|
463 | * tileoffsetlat, tileoffsetx, tileoffsety |
---|
464 | */ |
---|
465 | calculateGridLayout: function(bounds, extent, resolution) { |
---|
466 | var tilelon = resolution * this.tileSize.w; |
---|
467 | var tilelat = resolution * this.tileSize.h; |
---|
468 | |
---|
469 | var offsetlon = bounds.left - extent.left; |
---|
470 | var tilecol = Math.floor(offsetlon/tilelon) - this.buffer; |
---|
471 | var tilecolremain = offsetlon/tilelon - tilecol; |
---|
472 | var tileoffsetx = -tilecolremain * this.tileSize.w; |
---|
473 | var tileoffsetlon = extent.left + tilecol * tilelon; |
---|
474 | |
---|
475 | var offsetlat = extent.top - bounds.top + tilelat; |
---|
476 | var tilerow = Math.floor(offsetlat/tilelat) - this.buffer; |
---|
477 | var tilerowremain = tilerow - offsetlat/tilelat; |
---|
478 | var tileoffsety = tilerowremain * this.tileSize.h; |
---|
479 | var tileoffsetlat = extent.top - tilelat*tilerow; |
---|
480 | |
---|
481 | return { |
---|
482 | tilelon: tilelon, tilelat: tilelat, |
---|
483 | tileoffsetlon: tileoffsetlon, tileoffsetlat: tileoffsetlat, |
---|
484 | tileoffsetx: tileoffsetx, tileoffsety: tileoffsety |
---|
485 | }; |
---|
486 | }, |
---|
487 | |
---|
488 | CLASS_NAME: "OpenLayers.Layer.MapGuide" |
---|
489 | }); |
---|