[76] | 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/Control.js |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * Class: OpenLayers.Control.Graticule |
---|
| 12 | * The Graticule displays a grid of latitude/longitude lines reprojected on |
---|
| 13 | * the map. |
---|
| 14 | * |
---|
| 15 | * Inherits from: |
---|
| 16 | * - <OpenLayers.Control> |
---|
| 17 | * |
---|
| 18 | */ |
---|
| 19 | OpenLayers.Control.Graticule = OpenLayers.Class(OpenLayers.Control, { |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * APIProperty: autoActivate |
---|
| 23 | * {Boolean} Activate the control when it is added to a map. Default is |
---|
| 24 | * true. |
---|
| 25 | */ |
---|
| 26 | autoActivate: true, |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * APIProperty: intervals |
---|
| 30 | * {Array(Float)} A list of possible graticule widths in degrees. |
---|
| 31 | */ |
---|
| 32 | intervals: [ 45, 30, 20, 10, 5, 2, 1, |
---|
| 33 | 0.5, 0.2, 0.1, 0.05, 0.01, |
---|
| 34 | 0.005, 0.002, 0.001 ], |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | * APIProperty: displayInLayerSwitcher |
---|
| 38 | * {Boolean} Allows the Graticule control to be switched on and off by |
---|
| 39 | * LayerSwitcher control. Defaults is true. |
---|
| 40 | */ |
---|
| 41 | displayInLayerSwitcher: true, |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * APIProperty: visible |
---|
| 45 | * {Boolean} should the graticule be initially visible (default=true) |
---|
| 46 | */ |
---|
| 47 | visible: true, |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * APIProperty: numPoints |
---|
| 51 | * {Integer} The number of points to use in each graticule line. Higher |
---|
| 52 | * numbers result in a smoother curve for projected maps |
---|
| 53 | */ |
---|
| 54 | numPoints: 50, |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | * APIProperty: targetSize |
---|
| 58 | * {Integer} The maximum size of the grid in pixels on the map |
---|
| 59 | */ |
---|
| 60 | targetSize: 200, |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * APIProperty: layerName |
---|
| 64 | * {String} The name to be displayed in the layer switcher, default is set |
---|
| 65 | * by {<OpenLayers.Lang>}. |
---|
| 66 | */ |
---|
| 67 | layerName: null, |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * APIProperty: labelled |
---|
| 71 | * {Boolean} Should the graticule lines be labelled?. default=true |
---|
| 72 | */ |
---|
| 73 | labelled: true, |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * APIProperty: labelFormat |
---|
| 77 | * {String} the format of the labels, default = 'dm'. See |
---|
| 78 | * <OpenLayers.Util.getFormattedLonLat> for other options. |
---|
| 79 | */ |
---|
| 80 | labelFormat: 'dm', |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * APIProperty: lineSymbolizer |
---|
| 84 | * {symbolizer} the symbolizer used to render lines |
---|
| 85 | */ |
---|
| 86 | lineSymbolizer: { |
---|
| 87 | strokeColor: "#333", |
---|
| 88 | strokeWidth: 1, |
---|
| 89 | strokeOpacity: 0.5 |
---|
| 90 | }, |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * APIProperty: labelSymbolizer |
---|
| 94 | * {symbolizer} the symbolizer used to render labels |
---|
| 95 | */ |
---|
| 96 | labelSymbolizer: {}, |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * Property: gratLayer |
---|
| 100 | * {OpenLayers.Layer.Vector} vector layer used to draw the graticule on |
---|
| 101 | */ |
---|
| 102 | gratLayer: null, |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * Constructor: OpenLayers.Control.Graticule |
---|
| 106 | * Create a new graticule control to display a grid of latitude longitude |
---|
| 107 | * lines. |
---|
| 108 | * |
---|
| 109 | * Parameters: |
---|
| 110 | * options - {Object} An optional object whose properties will be used |
---|
| 111 | * to extend the control. |
---|
| 112 | */ |
---|
| 113 | initialize: function(options) { |
---|
| 114 | options = options || {}; |
---|
| 115 | options.layerName = options.layerName || OpenLayers.i18n("graticule"); |
---|
| 116 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
---|
| 117 | |
---|
| 118 | this.labelSymbolizer.stroke = false; |
---|
| 119 | this.labelSymbolizer.fill = false; |
---|
| 120 | this.labelSymbolizer.label = "${label}"; |
---|
| 121 | this.labelSymbolizer.labelAlign = "${labelAlign}"; |
---|
| 122 | this.labelSymbolizer.labelXOffset = "${xOffset}"; |
---|
| 123 | this.labelSymbolizer.labelYOffset = "${yOffset}"; |
---|
| 124 | }, |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * APIMethod: destroy |
---|
| 128 | */ |
---|
| 129 | destroy: function() { |
---|
| 130 | this.deactivate(); |
---|
| 131 | OpenLayers.Control.prototype.destroy.apply(this, arguments); |
---|
| 132 | if (this.gratLayer) { |
---|
| 133 | this.gratLayer.destroy(); |
---|
| 134 | this.gratLayer = null; |
---|
| 135 | } |
---|
| 136 | }, |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Method: draw |
---|
| 140 | * |
---|
| 141 | * initializes the graticule layer and does the initial update |
---|
| 142 | * |
---|
| 143 | * Returns: |
---|
| 144 | * {DOMElement} |
---|
| 145 | */ |
---|
| 146 | draw: function() { |
---|
| 147 | OpenLayers.Control.prototype.draw.apply(this, arguments); |
---|
| 148 | if (!this.gratLayer) { |
---|
| 149 | var gratStyle = new OpenLayers.Style({},{ |
---|
| 150 | rules: [new OpenLayers.Rule({'symbolizer': |
---|
| 151 | {"Point":this.labelSymbolizer, |
---|
| 152 | "Line":this.lineSymbolizer} |
---|
| 153 | })] |
---|
| 154 | }); |
---|
| 155 | this.gratLayer = new OpenLayers.Layer.Vector(this.layerName, { |
---|
| 156 | styleMap: new OpenLayers.StyleMap({'default':gratStyle}), |
---|
| 157 | visibility: this.visible, |
---|
| 158 | displayInLayerSwitcher: this.displayInLayerSwitcher |
---|
| 159 | }); |
---|
| 160 | } |
---|
| 161 | return this.div; |
---|
| 162 | }, |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * APIMethod: activate |
---|
| 166 | */ |
---|
| 167 | activate: function() { |
---|
| 168 | if (OpenLayers.Control.prototype.activate.apply(this, arguments)) { |
---|
| 169 | this.map.addLayer(this.gratLayer); |
---|
| 170 | this.map.events.register('moveend', this, this.update); |
---|
| 171 | this.update(); |
---|
| 172 | return true; |
---|
| 173 | } else { |
---|
| 174 | return false; |
---|
| 175 | } |
---|
| 176 | }, |
---|
| 177 | |
---|
| 178 | /** |
---|
| 179 | * APIMethod: deactivate |
---|
| 180 | */ |
---|
| 181 | deactivate: function() { |
---|
| 182 | if (OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { |
---|
| 183 | this.map.events.unregister('moveend', this, this.update); |
---|
| 184 | this.map.removeLayer(this.gratLayer); |
---|
| 185 | return true; |
---|
| 186 | } else { |
---|
| 187 | return false; |
---|
| 188 | } |
---|
| 189 | }, |
---|
| 190 | /** |
---|
| 191 | * Method: update |
---|
| 192 | * |
---|
| 193 | * calculates the grid to be displayed and actually draws it |
---|
| 194 | * |
---|
| 195 | * Returns: |
---|
| 196 | * {DOMElement} |
---|
| 197 | */ |
---|
| 198 | update: function() { |
---|
| 199 | //wait for the map to be initialized before proceeding |
---|
| 200 | var mapBounds = this.map.getExtent(); |
---|
| 201 | if (!mapBounds) { |
---|
| 202 | return; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | //clear out the old grid |
---|
| 206 | this.gratLayer.destroyFeatures(); |
---|
| 207 | |
---|
| 208 | //get the projection objects required |
---|
| 209 | var llProj = new OpenLayers.Projection("EPSG:4326"); |
---|
| 210 | var mapProj = this.map.getProjectionObject(); |
---|
| 211 | var mapRes = this.map.getResolution(); |
---|
| 212 | |
---|
| 213 | //if the map is in lon/lat, then the lines are straight and only one |
---|
| 214 | //point is required |
---|
| 215 | if (mapProj.proj && mapProj.proj.projName == "longlat") { |
---|
| 216 | this.numPoints = 1; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | //get the map center in EPSG:4326 |
---|
| 220 | var mapCenter = this.map.getCenter(); //lon and lat here are really map x and y |
---|
| 221 | var mapCenterLL = new OpenLayers.Pixel(mapCenter.lon, mapCenter.lat); |
---|
| 222 | OpenLayers.Projection.transform(mapCenterLL, mapProj, llProj); |
---|
| 223 | |
---|
| 224 | /* This block of code determines the lon/lat interval to use for the |
---|
| 225 | * grid by calculating the diagonal size of one grid cell at the map |
---|
| 226 | * center. Iterates through the intervals array until the diagonal |
---|
| 227 | * length is less than the targetSize option. |
---|
| 228 | */ |
---|
| 229 | //find lat/lon interval that results in a grid of less than the target size |
---|
| 230 | var testSq = this.targetSize*mapRes; |
---|
| 231 | testSq *= testSq; //compare squares rather than doing a square root to save time |
---|
| 232 | var llInterval; |
---|
| 233 | for (var i=0; i<this.intervals.length; ++i) { |
---|
| 234 | llInterval = this.intervals[i]; //could do this for both x and y?? |
---|
| 235 | var delta = llInterval/2; |
---|
| 236 | var p1 = mapCenterLL.offset(new OpenLayers.Pixel(-delta, -delta)); //test coords in EPSG:4326 space |
---|
| 237 | var p2 = mapCenterLL.offset(new OpenLayers.Pixel( delta, delta)); |
---|
| 238 | OpenLayers.Projection.transform(p1, llProj, mapProj); // convert them back to map projection |
---|
| 239 | OpenLayers.Projection.transform(p2, llProj, mapProj); |
---|
| 240 | var distSq = (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y); |
---|
| 241 | if (distSq <= testSq) { |
---|
| 242 | break; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | //alert(llInterval); |
---|
| 246 | |
---|
| 247 | //round the LL center to an even number based on the interval |
---|
| 248 | mapCenterLL.x = Math.floor(mapCenterLL.x/llInterval)*llInterval; |
---|
| 249 | mapCenterLL.y = Math.floor(mapCenterLL.y/llInterval)*llInterval; |
---|
| 250 | //TODO adjust for minutses/seconds? |
---|
| 251 | |
---|
| 252 | /* The following 2 blocks calculate the nodes of the grid along a |
---|
| 253 | * line of constant longitude (then latitiude) running through the |
---|
| 254 | * center of the map until it reaches the map edge. The calculation |
---|
| 255 | * goes from the center in both directions to the edge. |
---|
| 256 | */ |
---|
| 257 | //get the central longitude line, increment the latitude |
---|
| 258 | var iter = 0; |
---|
| 259 | var centerLonPoints = [mapCenterLL.clone()]; |
---|
| 260 | var newPoint = mapCenterLL.clone(); |
---|
| 261 | var mapXY; |
---|
| 262 | do { |
---|
| 263 | newPoint = newPoint.offset(new OpenLayers.Pixel(0,llInterval)); |
---|
| 264 | mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); |
---|
| 265 | centerLonPoints.unshift(newPoint); |
---|
| 266 | } while (mapBounds.containsPixel(mapXY) && ++iter<1000); |
---|
| 267 | newPoint = mapCenterLL.clone(); |
---|
| 268 | do { |
---|
| 269 | newPoint = newPoint.offset(new OpenLayers.Pixel(0,-llInterval)); |
---|
| 270 | mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); |
---|
| 271 | centerLonPoints.push(newPoint); |
---|
| 272 | } while (mapBounds.containsPixel(mapXY) && ++iter<1000); |
---|
| 273 | |
---|
| 274 | //get the central latitude line, increment the longitude |
---|
| 275 | iter = 0; |
---|
| 276 | var centerLatPoints = [mapCenterLL.clone()]; |
---|
| 277 | newPoint = mapCenterLL.clone(); |
---|
| 278 | do { |
---|
| 279 | newPoint = newPoint.offset(new OpenLayers.Pixel(-llInterval, 0)); |
---|
| 280 | mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); |
---|
| 281 | centerLatPoints.unshift(newPoint); |
---|
| 282 | } while (mapBounds.containsPixel(mapXY) && ++iter<1000); |
---|
| 283 | newPoint = mapCenterLL.clone(); |
---|
| 284 | do { |
---|
| 285 | newPoint = newPoint.offset(new OpenLayers.Pixel(llInterval, 0)); |
---|
| 286 | mapXY = OpenLayers.Projection.transform(newPoint.clone(), llProj, mapProj); |
---|
| 287 | centerLatPoints.push(newPoint); |
---|
| 288 | } while (mapBounds.containsPixel(mapXY) && ++iter<1000); |
---|
| 289 | |
---|
| 290 | //now generate a line for each node in the central lat and lon lines |
---|
| 291 | //first loop over constant longitude |
---|
| 292 | var lines = []; |
---|
| 293 | for(var i=0; i < centerLatPoints.length; ++i) { |
---|
| 294 | var lon = centerLatPoints[i].x; |
---|
| 295 | var pointList = []; |
---|
| 296 | var labelPoint = null; |
---|
| 297 | var latEnd = Math.min(centerLonPoints[0].y, 90); |
---|
| 298 | var latStart = Math.max(centerLonPoints[centerLonPoints.length - 1].y, -90); |
---|
| 299 | var latDelta = (latEnd - latStart)/this.numPoints; |
---|
| 300 | var lat = latStart; |
---|
| 301 | for(var j=0; j<= this.numPoints; ++j) { |
---|
| 302 | var gridPoint = new OpenLayers.Geometry.Point(lon,lat); |
---|
| 303 | gridPoint.transform(llProj, mapProj); |
---|
| 304 | pointList.push(gridPoint); |
---|
| 305 | lat += latDelta; |
---|
| 306 | if (gridPoint.y >= mapBounds.bottom && !labelPoint) { |
---|
| 307 | labelPoint = gridPoint; |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | if (this.labelled) { |
---|
| 311 | //keep track of when this grid line crosses the map bounds to set |
---|
| 312 | //the label position |
---|
| 313 | //labels along the bottom, add 10 pixel offset up into the map |
---|
| 314 | //TODO add option for labels on top |
---|
| 315 | var labelPos = new OpenLayers.Geometry.Point(labelPoint.x,mapBounds.bottom); |
---|
| 316 | var labelAttrs = { |
---|
| 317 | value: lon, |
---|
| 318 | label: this.labelled?OpenLayers.Util.getFormattedLonLat(lon, "lon", this.labelFormat):"", |
---|
| 319 | labelAlign: "cb", |
---|
| 320 | xOffset: 0, |
---|
| 321 | yOffset: 2 |
---|
| 322 | }; |
---|
| 323 | this.gratLayer.addFeatures(new OpenLayers.Feature.Vector(labelPos,labelAttrs)); |
---|
| 324 | } |
---|
| 325 | var geom = new OpenLayers.Geometry.LineString(pointList); |
---|
| 326 | lines.push(new OpenLayers.Feature.Vector(geom)); |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | //now draw the lines of constant latitude |
---|
| 330 | for (var j=0; j < centerLonPoints.length; ++j) { |
---|
| 331 | lat = centerLonPoints[j].y; |
---|
| 332 | if (lat<-90 || lat>90) { //latitudes only valid between -90 and 90 |
---|
| 333 | continue; |
---|
| 334 | } |
---|
| 335 | var pointList = []; |
---|
| 336 | var lonStart = centerLatPoints[0].x; |
---|
| 337 | var lonEnd = centerLatPoints[centerLatPoints.length - 1].x; |
---|
| 338 | var lonDelta = (lonEnd - lonStart)/this.numPoints; |
---|
| 339 | var lon = lonStart; |
---|
| 340 | var labelPoint = null; |
---|
| 341 | for(var i=0; i <= this.numPoints ; ++i) { |
---|
| 342 | var gridPoint = new OpenLayers.Geometry.Point(lon,lat); |
---|
| 343 | gridPoint.transform(llProj, mapProj); |
---|
| 344 | pointList.push(gridPoint); |
---|
| 345 | lon += lonDelta; |
---|
| 346 | if (gridPoint.x < mapBounds.right) { |
---|
| 347 | labelPoint = gridPoint; |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | if (this.labelled) { |
---|
| 351 | //keep track of when this grid line crosses the map bounds to set |
---|
| 352 | //the label position |
---|
| 353 | //labels along the right, 30 pixel offset left into the map |
---|
| 354 | //TODO add option for labels on left |
---|
| 355 | var labelPos = new OpenLayers.Geometry.Point(mapBounds.right, labelPoint.y); |
---|
| 356 | var labelAttrs = { |
---|
| 357 | value: lat, |
---|
| 358 | label: this.labelled?OpenLayers.Util.getFormattedLonLat(lat, "lat", this.labelFormat):"", |
---|
| 359 | labelAlign: "rb", |
---|
| 360 | xOffset: -2, |
---|
| 361 | yOffset: 2 |
---|
| 362 | }; |
---|
| 363 | this.gratLayer.addFeatures(new OpenLayers.Feature.Vector(labelPos,labelAttrs)); |
---|
| 364 | } |
---|
| 365 | var geom = new OpenLayers.Geometry.LineString(pointList); |
---|
| 366 | lines.push(new OpenLayers.Feature.Vector(geom)); |
---|
| 367 | } |
---|
| 368 | this.gratLayer.addFeatures(lines); |
---|
| 369 | }, |
---|
| 370 | |
---|
| 371 | CLASS_NAME: "OpenLayers.Control.Graticule" |
---|
| 372 | }); |
---|
| 373 | |
---|