Project

General

Profile

1
/*
2
Author:       Gertjan van Oosten gertjan at west dot nl
3
License:      LGPL as per: http://www.gnu.org/copyleft/lesser.html
4

    
5
$Id: OverviewMap.js 3887 2008-02-27 18:18:53Z ahocevar $
6
*/
7

    
8
// Ensure this object's dependancies are loaded.
9
mapbuilder.loadScript(baseDir+"/util/openlayers/OpenLayers.js");
10
mapbuilder.loadScript(baseDir+"/widget/WidgetBase.js");
11

    
12
/**
13
 * Widget to add an OL OverviewMap to a main map.
14
 * @constructor
15
 * @base WidgetBase
16
 * @param widgetNode  The widget's XML object node from the configuration document.
17
 * @param model       The model object that this widget belongs to.
18
 */
19
function OverviewMap(widgetNode, model) {
20
  WidgetBase.apply(this,new Array(widgetNode, model));
21

    
22
  var width = this.getProperty("mb:width");
23
  if (width) {
24
    this.width = new Number(width);
25
  }
26
  var height = this.getProperty("mb:height");
27
  if (height) {
28
    this.height = new Number(height);
29
  }
30
  var minRatio = this.getProperty("mb:minRatio");
31
  if (minRatio) {
32
    this.minRatio = new Number(minRatio);
33
  }
34
  var maxRatio = this.getProperty("mb:maxRatio");
35
  if (maxRatio) {
36
    this.maxRatio = new Number(maxRatio);
37
  }
38

    
39
  var layersNode = widgetNode.selectSingleNode("mb:layers");
40
  if (layersNode) {
41
    this.layerNames = new Array();
42
    var layers = layersNode.childNodes;
43
    for (var i = 0; i < layers.length; i++) {
44
      if (layers[i].firstChild) {
45
        this.layerNames.push(getNodeValue(layers[i]));
46
      }
47
    }
48
  }
49

    
50
  this.model.addListener("refresh", this.addOverviewMap, this);
51
}
52

    
53
/**
54
 * Add an overview map to the map in this widget's model.
55
 * If layers have been specified in the config, clone these layers for the
56
 * overview map, otherwise just use the base layer from the main map.
57
 * If any of the layers to use are WMS layers it uses an untiled version of them.
58
 * @param objRef Pointer to widget object.
59
 */
60
OverviewMap.prototype.addOverviewMap = function(objRef) {
61
  if (objRef.model && objRef.model.map) {
62
    var map = objRef.model.map;
63
    
64
    /** reference to the OpenLayers OverviewMap control */
65
    this.control = null
66

    
67
    // Specify div and layers for overview map.
68
    var options = {
69
      div: objRef.getNode(),
70
      objRef: this,
71
      destroy: function() {
72
        OpenLayers.Control.OverviewMap.prototype.destroy.apply(this, arguments);
73
        this.div = null;
74
        objRef.control = null;
75
        objRef = null;
76
      },
77
      layers: new Array()
78
    };
79
    
80
    if (objRef.minRatio) options.minRatio = objRef.minRatio;
81
    if (objRef.maxRatio) options.maxRatio = objRef.maxRatio;
82

    
83
    // Clone the base layer. This is not really the OpenLayers base layer, but
84
    // the lowest layer in the Mapbuilder layers stack.
85
    if (!objRef.layerNames) {
86
      for (var i in map.mbMapPane.oLlayers) {
87
        var oLlayer = map.mbMapPane.oLlayers[i];
88
        if (oLlayer) {
89
          var baseLayer = objRef.getClonedLayer(oLlayer, true);
90
          options.layers.push(baseLayer);
91
          break;
92
        } 
93
      }
94
    }
95

    
96
    // Check for specifically requested layers
97
    var isBaseLayer = true;
98
    if (objRef.layerNames) {
99
      for (var i = 0; i < objRef.layerNames.length; i++) {
100
        var oLlayer = map.mbMapPane.getLayer(map.mbMapPane, objRef.layerNames[i]);
101
        if (oLlayer) {
102
          options.layers.push(objRef.getClonedLayer(oLlayer, isBaseLayer));
103
          isBaseLayer = false;
104
        }
105
      }
106
    }
107
    
108
    // Determine size:
109
    // - if width and height are both set, use these as the size;
110
    // - if only width or height is set, take aspect ratio of main map into account;
111
    // - otherwise, OL defaults to (180,90).
112
    var extent = map.getExtent();
113
    if (objRef.width && objRef.height) {
114
      options.size = new OpenLayers.Size(objRef.width, objRef.height);
115
    }
116
    else if (objRef.width) {
117
      options.size = new OpenLayers.Size(
118
        objRef.width,
119
        objRef.width * extent.getHeight() / extent.getWidth());
120
    }
121
    else if (objRef.height) {
122
      options.size = new OpenLayers.Size(
123
        objRef.height * extent.getWidth() / extent.getHeight(),
124
        objRef.height);
125
    }
126

    
127
    // Add the overview to the main map
128
    if (!objRef.control) {
129
      objRef.control = new OpenLayers.Control.OverviewMap(options);
130
      objRef.control.mapOptions = {theme: null};
131
      map.addControl(objRef.control);
132
    }
133

    
134
    // make all layers visible
135
    for (var i=0; i<options.layers.length; i++) {
136
      options.layers[i].setVisibility(true);
137
    }
138
  }
139
}
140

    
141
/**
142
 * Clone a map layer (OpenLayers.Layer subclass).
143
 * If the layer is a WMS layer it returns an untiled version of it.
144
 * @param layer Pointer to layer object.
145
 * @param isBaseLayer {Boolean} optional parameter: should the layer become
146
 * baselayer on the overview map?
147
 */
148
OverviewMap.prototype.getClonedLayer = function(layer, isBaseLayer) {
149
  if (layer == null) {
150
    return null;
151
  }
152
  
153
  isBaseLayer = isBaseLayer ? true : false;
154

    
155
  if (layer instanceof OpenLayers.Layer.WMS) {
156
    // make an untiled wms layer, with ratio 1
157
    var layerOptions = {
158
      units: layer.units,
159
      projection: layer.projection,
160
      maxExtent: layer.maxExtent,
161
      maxResolution: "auto",
162
      ratio: 1,
163
      singleTile: true,
164
      isBaseLayer: isBaseLayer
165
    };
166

    
167
    return new OpenLayers.Layer.WMS(layer.name,
168
      layer.url, {
169
        layers: layer.params.LAYERS,
170
        format: layer.params.FORMAT,
171
        transparent: layer.params.TRANSPARENT,
172
        sld: layer.params.SLD,
173
        sld_body: layer.params.SLD_BODY,
174
        styles: layer.params.STYLES
175
      }, layerOptions);
176
  }
177
  else {
178
    // take the layer as-is and clone it
179
    var clonedLayer = layer.clone();
180
    clonedLayer.setVisibility(true);
181
    return clonedLayer;
182
  }
183
}
184
  
(96-96/145)