Tuesday 22 October 2019

javascript - OpenLayers: Use scales instead of resolutions


Venturing around in the OpenLayers source code, it seems like everything related to zoom-levels is done using resolutions. Fair enough since this related directly to screen space.


I would like though, to use scales instead, and further more explicitly define the allowed scales.

- Is this possible?


So far I have created a set of scales which I transform into resolutions:


var scales = [500, 1000, 2000, 4000, 10000, 25000, 50000];
var resolutions = [];
for(var i = 0; i < scales.length; i++) {
resolutions.push(OpenLayers.Util.getResolutionFromScale(scales[i], units));
}

map = new OpenLayers.Map('', {
...

minScale: scales[scales.length - 1],
maxScale: scales[0],
resolution: resolutions,
numZoomLevels: scales.length,
...
});

My problem is that the first and the last scale are respected, but in between the current resolution converted into scale is not among the ones I specified.
- What have I missed?



Answer




In order to use scales on your map:



  • Set only the 'scales' option. Do not set maxResolution, minResolution, maxScale, minScale, numZoomLevels, or any other scale related property. Set it to an array of scale denominators (or scales).

  • Configure a unit: note that for meters, this should be 'm', for degrees, 'degrees', etc.

  • You can set a maxExtent. Do not set a minExtent.


Once you do this, the scales should be used on your map. (Note that they will be ordered from most zoomed out -- highest scale denominator -- to most zoomed in -- smallest scale denominator.)


In your case, this should look exactly like:


map = new OpenLayers.Map('map', {                        
scales: scales,

units: 'm' // or whatever
});

Then add your layer.


Note that layer-based resolution options will 'control' the map; you should typically only specify these things on the map or the layers, not both.


Note that the base layer controls the overall scales of the map; overlays will be entirely controlled by base layers.


Note that OpenLayers uses a DPI assumption of 72. To control this DPI setting, you can set OpenLayers.DOTS_PER_INCH before creating your map. A typical value (used by the SLD spec) will be different, so if this assumption matters to you, you should change this configuration parameter to match your server.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...