Debian Codegraph @

dojox/geo/openlayers/Map.js

raw symbols SCIP
1 define([
2 "dojo/_base/kernel",
3 "dojo/_base/declare",
4 "dojo/_base/lang",
5 "dojo/_base/array",
6 "dojo/_base/json",
7 "dojo/dom",
8 "dojo/dom-style",
9 "./_base",
10 "./TouchInteractionSupport",
11 "./Layer",
12 "./Patch"
13 ], function(kernel, declare, lang, array, json, dom, style, openlayers, TouchInteractionSupport, Layer, Patch){
14
15 kernel.experimental("dojox.geo.openlayers.Map");
16
17
18 Patch.patchGFX();
19
20 /*=====
21 dojox.geo.openlayers.__MapArgs = {
22 // summary:
23 // The keyword arguments that can be passed in a Map constructor.
24 // baseLayerType: String
25 // type of the base layer. Can be any of
26 //
27 // - `dojox.geo.openlayers.BaseLayerType.OSM`: Open Street Map base layer
28 // - `dojox.geo.openlayers.BaseLayerType.Transport`: Open Street Map Transport base layer (opencyclemap.org)
29 // - `dojox.geo.openlayers.BaseLayerType.WMS`: Web Map Service layer
30 // - `dojox.geo.openlayers.BaseLayerType.GOOGLE`: Google layer
31 // - `dojox.geo.openlayers.BaseLayerType.VIRTUAL_EARTH`: Virtual Earth layer
32 // - `dojox.geo.openlayers.BaseLayerType.BING`: Bing layer
33 // - `dojox.geo.openlayers.BaseLayerType.YAHOO`: Yahoo layer
34 // - `dojox.geo.openlayers.BaseLayerType.ARCGIS`: ESRI ArgGIS layer
35 // baseLayerName: String
36 // The name of the base layer.
37 // baseLayerUrl: String
38 // Some layer may need an url such as Web Map Server.
39 // baseLayerOptions: String
40 // Additional specific options passed to OpensLayers layer, such as The list of layer to display, for Web Map Server layer.
41 };
42 =====*/
43
44 return declare("dojox.geo.openlayers.Map", null, {
45 // summary:
46 // A map viewer based on the OpenLayers library.
47 //
48 // description:
49 // The `dojox.geo.openlayers.Map` object allows to view maps from various map providers.
50 // It encapsulates an `OpenLayers.Map` object on which most operations are delegated.
51 // GFX layers can be added to display GFX georeferenced shapes as well as Dojo widgets.
52 // Parameters can be passed as argument at construction time to define the base layer
53 // type and the base layer parameters such as url or options depending on the type
54 // specified. These parameters can be any of:
55 //
56 // _baseLayerType_: type of the base layer. Can be any of:
57 //
58 // - `dojox.geo.openlayers.BaseLayerType.OSM`: Open Street Map base layer
59 // - `dojox.geo.openlayers.BaseLayerType.Transport`: Open Street Map Transport base layer (opencyclemap.org)
60 // - `dojox.geo.openlayers.BaseLayerType.WMS`: Web Map Service layer
61 // - `dojox.geo.openlayers.BaseLayerType.GOOGLE`: Google layer
62 // - `dojox.geo.openlayers.BaseLayerType.VIRTUAL_EARTH`: Virtual Earth layer
63 // - `dojox.geo.openlayers.BaseLayerType.BING`: Bing layer
64 // - `dojox.geo.openlayers.BaseLayerType.YAHOO`: Yahoo layer
65 // - `dojox.geo.openlayers.BaseLayerType.ARCGIS`: ESRI ArgGIS layer
66 //
67 // Note that access to commercial server such as Google, Virtual Earth or Yahoo may need specific licencing.
68 //
69 // The parameters value also include:
70 //
71 // - `baseLayerName`: The name of the base layer.
72 // - `baseLayerUrl`: Some layer may need an url such as Web Map Server
73 // - `baseLayerOptions`: Additional specific options passed to OpensLayers layer,
74 // such as The list of layer to display, for Web Map Server layer.
75 //
76 // example:
77 // | var map = new dojox.geo.openlayers.widget.Map(div, {
78 // | baseLayerType: dojox.geo.openlayers.BaseLayerType.OSM,
79 // | baseLayerName: 'Open Street Map Layer'
80 // | });
81
82 // olMap: OpenLayers.Map
83 // The underlying OpenLayers.Map object.
84 // Should be accessed on read mode only.
85 olMap: null,
86
87 _tp: null,
88
89 constructor: function(div, options){
90 // summary:
91 // Constructs a new Map object
92 if(!options){
93 options = {};
94 }
95
96 div = dom.byId(div);
97
98 this._tp = {
99 x: 0,
100 y: 0
101 };
102
103 var opts = options.openLayersMapOptions;
104
105 if(!opts){
106 opts = {
107 controls: [new OpenLayers.Control.ScaleLine({
108 maxWidth: 200
109 }), new OpenLayers.Control.Navigation()]
110 };
111 }
112 if(options.accessible){
113 var kbd = new OpenLayers.Control.KeyboardDefaults();
114 if(!opts.controls){
115 opts.controls = [];
116 }
117 opts.controls.push(kbd);
118 }
119 var baseLayerType = options.baseLayerType;
120 if(!baseLayerType){
121 baseLayerType = openlayers.BaseLayerType.OSM;
122 }
123 var map = new OpenLayers.Map(div, opts);
124 this.olMap = map;
125
126 this._layerDictionary = {
127 olLayers: [],
128 layers: []
129 };
130
131 if(options.touchHandler){
132 this._touchControl = new TouchInteractionSupport(map);
133 }
134
135 var base = this._createBaseLayer(options);
136 this.addLayer(base);
137
138 this.initialFit(options);
139 },
140
141 initialFit: function(params){
142 // summary:
143 // Performs an initial fit to contents.
144 // tags:
145 // protected
146 var o = params.initialLocation;
147 if(!o){
148 o = [-160, 70, 160, -70];
149 }
150 this.fitTo(o);
151 },
152
153 setBaseLayerType: function(type){
154 // summary:
155 // Set the base layer type, replacing the existing base layer
156 // type: dojox/geo/openlayers.BaseLayerType
157 // base layer type
158 // returns:
159 // The newly created layer.
160 if(type == this.baseLayerType){
161 return null; // Layer
162 }
163
164 var o = null;
165 if(typeof type == "string"){
166 o = {
167 baseLayerName: type,
168 baseLayerType: type
169 };
170 this.baseLayerType = type;
171 }else if(typeof type == "object"){
172 o = type;
173 this.baseLayerType = o.baseLayerType;
174 }
175 var bl = null;
176 if(o != null){
177 bl = this._createBaseLayer(o);
178 if(bl != null){
179 var olm = this.olMap;
180 var ob = olm.getZoom();
181 var oc = olm.getCenter();
182 var recenter = !!oc && !!olm.baseLayer && !!olm.baseLayer.map;
183
184 if(recenter){
185 var proj = olm.getProjectionObject();
186 if(proj != null){
187 oc = oc.transform(proj, openlayers.EPSG4326);
188 }
189 }
190 var old = olm.baseLayer;
191 if(old != null){
192 var l = this._getLayer(old);
193 this.removeLayer(l);
194 }
195 if(bl != null){
196 this.addLayer(bl);
197 }
198 if(recenter){
199 proj = olm.getProjectionObject();
200 if(proj != null){
201 oc = oc.transform(openlayers.EPSG4326, proj);
202 }
203 olm.setCenter(oc, ob);
204 }
205 }
206 }
207 return bl;
208 },
209
210 getBaseLayerType: function(){
211 // summary:
212 // Returns the base layer type.
213 // returns:
214 // The current base layer type.
215 return this.baseLayerType; // openlayers.BaseLayerType
216 },
217
218 getScale: function(geodesic){
219 // summary:
220 // Returns the current scale
221 // geodesic: Boolean
222 // Tell if geodesic calculation should be performed. If set to
223 // true, the scale will be calculated based on the horizontal size of the
224 // pixel in the center of the map viewport.
225 var scale = null;
226 var om = this.olMap;
227 if(geodesic){
228 var units = om.getUnits();
229 if(!units){
230 return null; // Number
231 }
232 var inches = OpenLayers.INCHES_PER_UNIT;
233 scale = (om.getGeodesicPixelSize().w || 0.000001) * inches["km"] * OpenLayers.DOTS_PER_INCH;
234 }else{
235 scale = om.getScale();
236 }
237 return scale; // Number
238 },
239
240 getOLMap: function(){
241 // summary:
242 // gets the underlying OpenLayers map object.
243 // returns:
244 // The underlying OpenLayers map object.
245 return this.olMap; // OpenLayers.Map
246 },
247
248 _createBaseLayer: function(params){
249 // summary:
250 // Creates the base layer.
251 // tags:
252 // private
253 var base = null;
254 var type = params.baseLayerType;
255 var url = params.baseLayerUrl;
256 var name = params.baseLayerName;
257 var options = params.baseLayerOptions;
258
259 if(!name){
260 name = type;
261 }
262 if(!options){
263 options = {};
264 }
265 switch(type){
266 case openlayers.BaseLayerType.OSM:
267 options.transitionEffect = "resize";
268 // base = new OpenLayers.Layer.OSM(name, url, options);
269 base = new Layer(name, {
270 olLayer: new OpenLayers.Layer.OSM(name, url, options)
271 });
272 break;
273 case openlayers.BaseLayerType.Transport:
274 options.transitionEffect = "resize";
275 base = new Layer(name, {
276 olLayer: new OpenLayers.Layer.OSM.TransportMap(name, url, options)
277 });
278 break;
279 case openlayers.BaseLayerType.WMS:
280 if(!url){
281 url = "http://labs.metacarta.com/wms/vmap0";
282 if(!options.layers){
283 options.layers = "basic";
284 }
285 }
286 base = new Layer(name, {
287 olLayer: new OpenLayers.Layer.WMS(name, url, options, {
288 transitionEffect: "resize"
289 })
290 });
291 break;
292 case openlayers.BaseLayerType.GOOGLE:
293 base = new Layer(name, {
294 olLayer: new OpenLayers.Layer.Google(name, options)
295 });
296 break;
297 case openlayers.BaseLayerType.VIRTUAL_EARTH:
298 base = new Layer(name, {
299 olLayer: new OpenLayers.Layer.VirtualEarth(name, options)
300 });
301 break;
302 case openlayers.BaseLayerType.YAHOO:
303 // base = new OpenLayers.Layer.Yahoo(name);
304 base = new Layer(name, {
305 olLayer: new OpenLayers.Layer.Yahoo(name, options)
306 });
307 break;
308 case openlayers.BaseLayerType.ARCGIS:
309 if(!url){
310 url = "http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer/export";
311 }
312 base = new Layer(name, {
313 olLayer: new OpenLayers.Layer.ArcGIS93Rest(name, url, options, {})
314 });
315
316 break;
317 }
318
319 if(base == null){
320 if(type instanceof OpenLayers.Layer){
321 base = type;
322 }else{
323 options.transitionEffect = "resize";
324 base = new Layer(name, {
325 olLayer: new OpenLayers.Layer.OSM(name, url, options)
326 });
327 this.baseLayerType = openlayers.BaseLayerType.OSM;
328 }
329 }
330
331 return base;
332 },
333
334 removeLayer: function(layer){
335 // summary:
336 // Remove the specified layer from the map.
337 // layer: Layer
338 // The layer to remove from the map.
339 var om = this.olMap;
340 var i = array.indexOf(this._layerDictionary.layers, layer);
341 if(i > 0){
342 this._layerDictionary.layers.splice(i, 1);
343 }
344 var oll = layer.olLayer;
345 var j = array.indexOf(this._layerDictionary.olLayers, oll);
346 if(j > 0){
347 this._layerDictionary.olLayers.splice(i, j);
348 }
349 om.removeLayer(oll, false);
350 },
351
352 layerIndex: function(layer, index){
353 // summary:
354 // Set or retrieve the layer index.
355 // description:
356 // Set or get the layer index, that is the z-order of the layer.
357 // if the index parameter is provided, the layer index is set to
358 // this value. If the index parameter is not provided, the index of
359 // the layer is returned.
360 // layer: Layer
361 // the layer to retrieve the index.
362 // index: int?
363 // index of the layer
364 // returns:
365 // the index of the layer.
366 var olm = this.olMap;
367 if(!index){
368 return olm.getLayerIndex(layer.olLayer);
369 }
370 //olm.raiseLayer(layer.olLayer, index);
371 olm.setLayerIndex(layer.olLayer, index);
372
373 this._layerDictionary.layers.sort(function(l1, l2){
374 return olm.getLayerIndex(l1.olLayer) - olm.getLayerIndex(l2.olLayer);
375 });
376 this._layerDictionary.olLayers.sort(function(l1, l2){
377 return olm.getLayerIndex(l1) - olm.getLayerIndex(l2);
378 });
379
380 return index; // Number
381 },
382
383 addLayer: function(layer){
384 // summary:
385 // Add the specified layer to the map.
386 // layer: Layer
387 // The layer to add to the map.
388 layer.dojoMap = this;
389 var om = this.olMap;
390 var ol = layer.olLayer;
391 this._layerDictionary.olLayers.push(ol);
392 this._layerDictionary.layers.push(layer);
393 om.addLayer(ol);
394 layer.added();
395 },
396
397 _getLayer: function(/*OpenLayer.Layer */ol){
398 // summary:
399 // Retrieve the dojox.geo.openlayer.Layer from the OpenLayer.Layer
400 // tags:
401 // private
402 var i = array.indexOf(this._layerDictionary.olLayers, ol);
403 if(i != -1){
404 return this._layerDictionary.layers[i];
405 }
406 return null;
407 },
408
409 getLayer: function(property, value){
410 // summary:
411 // Returns the layer whose property matches the value.
412 // property: String
413 // The property to check
414 // value: Object
415 // The value to match
416 // returns:
417 // The layer(s) matching the property's value. Since multiple layers
418 // match the property's value the return value is an array.
419 // example:
420 // var layers = map.getLayer("name", "Layer Name");
421 var om = this.olMap;
422 var ols = om.getBy("layers", property, value);
423 var ret = new Array(); //[];
424 array.forEach(ols, function(ol){
425 ret.push(this._getLayer(ol));
426 }, this);
427 return ret; // Layer[]
428 },
429
430 getLayerCount: function(){
431 // summary:
432 // Returns the count of layers of this map.
433 // returns:
434 // The number of layers of this map.
435 var om = this.olMap;
436 if(om.layers == null){
437 return 0;
438 }
439 return om.layers.length; // Number
440 },
441
442 fitTo: function(o){
443 // summary:
444 // Fits the map on a point,or an area
445 // description:
446 // Fits the map on the point or extent specified as parameter.
447 // o: Object
448 // Object with key values fit parameters or a JSON string.
449 // example:
450 // Examples of arguments passed to the fitTo function:
451 // | null
452 // The map is fit on full extent
453 //
454 // | {
455 // | bounds: [ulx, uly, lrx, lry]
456 // | }
457 // The map is fit on the specified bounds expressed as decimal degrees latitude and longitude.
458 // The bounds are defined with their upper left and lower right corners coordinates.
459 //
460 // | {
461 // | position: [longitude, latitude],
462 // | extent: degrees
463 // | }
464 // The map is fit on the specified position showing the extent `<extent>` around
465 // the specified center position.
466
467 var map = this.olMap;
468 var from = openlayers.EPSG4326;
469
470 if(o == null){
471 var c = this.transformXY(0, 0, from);
472 map.setCenter(new OpenLayers.LonLat(c.x, c.y));
473 return;
474 }
475 var b = null;
476 if(typeof o == "string"){
477 var j = json.fromJson(o);
478 }else{
479 j = o;
480 }
481 var ul;
482 var lr;
483 if(j.hasOwnProperty("bounds")){
484 var a = j.bounds;
485 b = new OpenLayers.Bounds();
486 ul = this.transformXY(a[0], a[1], from);
487 b.left = ul.x;
488 b.top = ul.y;
489 lr = this.transformXY(a[2], a[3], from);
490 b.right = lr.x;
491 b.bottom = lr.y;
492 }
493 if(b == null){
494 if(j.hasOwnProperty("position")){
495 var p = j.position;
496 var e = j.hasOwnProperty("extent") ? j.extent : 1;
497 if(typeof e == "string"){
498 e = parseFloat(e);
499 }
500 b = new OpenLayers.Bounds();
501 ul = this.transformXY(p[0] - e, p[1] + e, from);
502 b.left = ul.x;
503 b.top = ul.y;
504 lr = this.transformXY(p[0] + e, p[1] - e, from);
505 b.right = lr.x;
506 b.bottom = lr.y;
507 }
508 }
509 if(b == null){
510 if(o.length == 4){
511 b = new OpenLayers.Bounds();
512 // TODO Choose the correct method
513 if(false){
514 b.left = o[0];
515 b.top = o[1];
516
517 b.right = o[2];
518 b.bottom = o[3];
519 }else{
520 ul = this.transformXY(o[0], o[1], from);
521 b.left = ul.x;
522 b.top = ul.y;
523 lr = this.transformXY(o[2], o[3], from);
524 b.right = lr.x;
525 b.bottom = lr.y;
526 }
527 }
528 }
529 if(b != null){
530 map.zoomToExtent(b, true);
531 }
532 },
533
534 transform: function(p, from, to){
535 // summary:
536 // Transforms the point passed as argument, expressed in the <em>from</em>
537 // coordinate system to the map coordinate system.
538 // description:
539 // Transforms the point passed as argument without modifying it. The point is supposed to be expressed
540 // in the <em>from</em> coordinate system and is transformed to the map coordinate system.
541 // p: Object {x, y}
542 // The point to transform
543 // from: OpenLayers.Projection
544 // The projection in which the point is expressed.
545 return this.transformXY(p.x, p.y, from, to);
546 },
547
548 transformXY: function(x, y, from, to){
549 // summary:
550 // Transforms the coordinates passed as argument, expressed in the <em>from</em>
551 // coordinate system to the map coordinate system.
552 // description:
553 // Transforms the coordinates passed as argument. The coordinate are supposed to be expressed
554 // in the <em>from</em> coordinate system and are transformed to the map coordinate system.
555 // x: Number
556 // The longitude coordinate to transform.
557 // y: Number
558 // The latitude coordinate to transform.
559 // from: OpenLayers.Projection?
560 // The projection in which the point is expressed, or EPSG4326 is not specified.
561 // to: OpenLayers.Projection?
562 // The projection in which the point is converted to. In not specifed, the map projection is used.
563 // returns:
564 // The transformed coordinate as an {x,y} Object.
565
566 var tp = this._tp;
567 tp.x = x;
568 tp.y = y;
569 if(!from){
570 from = openlayers.EPSG4326;
571 }
572 if(!to){
573 to = this.olMap.getProjectionObject();
574 }
575 tp = OpenLayers.Projection.transform(tp, from, to);
576 return tp; // Object
577 }
578
579 });
580
581 });