2

Openlayers ScaleLine component has a way to convert from resolution to scale, but what about the other way around?

Our customers are choosing a scale for each layer, indicating the scale where the layer should be visible. That's why I have to convert from scale to resolution.

I have tried the following, where the ScaleLine is showing "50m" Expected result would be for the two resolutions to be the same. Output:

Expected: 0.5964957884324376
Resolution: 0.013229166680160416

Code

const scale = 50; // 50 meters

const view = this.map.getView();
const center = view.getCenter();
const projection = view.getProjection();
const inchesPrMeter = 39.3700787;
const dpi = 96; // What about ol.has.DEVICE_PIXEL_RATIO ?

const expectedResolution = view.getResolution();
const resolution = scale / (projection.getMetersPerUnit() * inchesPrMeter * dpi);
console.log('Expected:', expectedResolution);
console.log('Resolution:', resolution);

I also don't like the hardcoded DPI.

Stsje
  • 31
  • 1
  • 3
  • Which projection are you using? If web mercator the view resolution is the nominal resolution of the projection and is only true at the equator. The true resolution at any point can be obtained using ol.proj.getPointResolution(). The scaleline control uses the map center for its calculation. To avoid hardcoded dpi see https://stackoverflow.com/questions/279749/detecting-the-system-dpi-ppi-from-js-css – Mike Dec 11 '18 at 09:49
  • @Mike We're using EPSG:25832. Defined with proj4js as `proj4js.defs('EPSG:25832', '+proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs'); ol.proj.proj4.register(proj4js);` Any suggestions as to how we should proceed? – Stsje Dec 11 '18 at 14:33
  • point resolution should be close to constant across a UTM projection so that's not going to be a problem. But I don't understand why you are trying to compare with the scaleline display. A 50m scale line of variable length drawn on the map simply indicates a 50m distance on the ground and doesn't relate to a map scale of, for example, 1:50. – Mike Dec 11 '18 at 17:37

0 Answers0