Wanting to know the screen resolution (pixels per inch) of my device, I displayed my map resolution and scale using this openlayers code:

console.log("resolution: " + map.getResolution());
console.log("scale: " +map.getScale());

which gave the result below:

"resolution: 611.4962261962891" mapTest.html:119
"scale: 1733376.0653978775"

Then I used this formula (from https://msdn.microsoft.com/en-us/library/aa940990.aspx):

Map scale = 1 : (ScreenRes pixels/inch * 39.37 inches/meter * 156543.04 meters/pixel * cos(latitude) / (2 ^ zoomlevel))

With my values:

1733376.065 = (ScreenRes ppi * 39.37 inches/meter * 611.496 meters/pixel * cos(51.02)) / (2^8)

The latitude I used is 51.20. But the formula produced 28889.6. I think this is pixels per meter so I divided by 39.37 to get pixels per inch. But that resulted in 733.797 which is an unrealistic pixels per inch. I was expecting value between 72 to 96.

What is going on here? Did I do something wrong or is the formula is wrong? The image is an OpenStreetMap image and the projection used is 900913. I hope some one can help.

  • Can you edit your question to show the formula with the numbers you used where you used them? I'm having trouble replicating your math, and you don't mention a zoom level. Also you're applying a Microsoft constant (m/p) to OSM data. Your true screen resolution is a property of the hardware itself - resolution / size = pixel density. 733 is definitely too high, as the highest res phone displays right now are in the 500 ppi range. Typical monitors are going to be in the 80-200 range. That 72-96 range you're expecting is mostly a software property. – Chris W Jan 30 '15 at 1:42
  • thank you Chris, It is zoom level 8. OpenLayers get Resolution method returned 611.496 this and you can also see here:msdn.microsoft.com/en-us/library/bb259689.aspx, getScale method gave me this number:1733376.065. Therefore in this case I replaced 156543m/pixel by 611.496m/p,and scale by scale. I'm using desktop/laptop computer.So to repeat the formula:1733376.065=(ScreenRes pixels/inch * 39.37 inches/meter * 611.496 meters/pixel * cos(51.02))/2^8. Unknown is ScreenRes pixels/inch. – Enkosa Jan 30 '15 at 4:57

You have indeed made some errors in your calculation. 156543.04 meters/pixel is a constant "based on the diameter of the Earth and the equations Microsoft used to set the zoom levels" used in two equations on the page you reference - it's not a variable. Your mistake was in substituting the map resolution your openlayers code returned for the constant. The first equation is:

Map resolution = 156543.04 meters/pixel * cos(latitude) / (2 ^ zoomlevel)

In theory, according to the table provided at the page you linked, this would be 611.5 m/p at zoom level 8, at the equator. That's almost exactly the number you were given by your openlayers code, but if you solve that equation at 51.02 lat it comes out to be closer to 384.7 m/p. That matches up with the sentence under their chart, which gives an example (at a different zoom level) for a lat other than the equator. However it is a bit odd that your openlayers code is returning 611.5 for zoom level 8 at 51.02 and not 384.7. That suggests to me that the equation may not hold for openlayers [actually it looks like very similar numbers in the openlayers docs] - the page you link to is specific to Bing maps and as noted above is based on their equations to set zoom levels. Or it may just be that code returns the at-the-equator value, regardless of where you're actually looking, while the displayed scalebar takes the lat into account.

The formula you actually want to solve (theoretically, with your values) should be:

1733376.065 = [(ScreenRes ppi * 39.37 inches/meter * 156543.04 meters/pixel * cos(51.02)) / (2^8)]

Now, as I mentioned in my comment, your true screen resolution in terms of ppi is hardware/device dependent and varies from display to display. If you want to find it, it may be a published specification or you may have to calculate it. You first need the native 'resolution' of the display. This would be more properly termed pixel dimensions, because resolution is actually the density of pixels (ppi) - but we use resolution interchangeably.

So let's say 1920x1080, a standard HD display (and note this needs to be the native number, NOT what it's being run at). Since we need the diagonal pixel dimension, simple a^2 + b^2 = c^2 gives us 2202.9 diagonal pixels. Now we need the actual diagonal dimension of the display. For instance a 24" display may only be 23.8" - this is pretty much always in the specs (usually as 'viewable' or some similar adjective). Diagonal pixels divided by diagonal inches gives us 92.6 ppi. Were it a 27" display at the same resolution, it would be 81.6 ppi. A 5" gives 440.6 ppi. I found a handy calculator on the web that can do this math for you if you have the right numbers.

This will give you a much more accurate (possibly even correct) result than trying to use the Microsoft equation and should be easier. The relationship, per your question title, is somewhat complex - we're talking about scaled scales essentially. And if you run a resolution (or rather pixel dimension) different than native, it gets even more complicated.

Go back to what scale is - unit x on map representing unit y in the real world. In computers there is no scale relative to anything but pixels. That's the first equation, what they term map resolution. Say 100 pixels represents 600m. On a phone display those 100 pixels may only be 0.25 inch, while on a 30" computer monitor they might be 1 inch - but they still both represent 600m. That's why the page has the second equation - because most people don't think of map scale in terms of pixels, they want a inch : mile ratio (or whatever units). The problem is that ratio changes depending on what display you're looking at while the pixel : real world unit ratio doesn't, assuming the same pixel dimensions and zoom on the image.

| improve this answer | |
  • Hi chris, I think the substitution is important because I used the corresponding scale 1733376.065. If I have to use 156543.04,I must use the scale at this resolution. On the other hand, you are right that OpenLayers should not return 611.5 resolution at the given latitude. Do you think that the "1:" has significance in this calculation? – Enkosa Jan 31 '15 at 1:28
  • One important thing to me that I was staring at is the difference in scale displayed on scale bar and that returned by openlayers get Scale method.For example at zoom level 7 of my map the getScale method returned 3466752.130795755 but the scale bar of the getScaleLine method shows 50km. What is this, two scale information for same map? some explanation please. – Enkosa Jan 31 '15 at 1:29
  • Hi, right you are, 156543.04 must be constant with changing zoom level, thank you. – Enkosa Jan 31 '15 at 12:03
  • @Enkosa The 1: is significant, but you're right in that it doesn't need to be in the equation and I goofed that part up. As for the "two different scales" issue, I think that probably relates to units (and rounding), though I'm not all that familiar with openlayers. I looked at the docs, and the scale bar can have different units than the actual map, which is always in projection units. I just don't know what input numbers and equations it's using when you call those methods. – Chris W Jan 31 '15 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.