how to set zoom level given the resolution and the latitude?

# how to set zoom level given the resolution and the latitude?

• Thursday, August 02, 2012 4:56 PM

Dear Bing maps users,

i'm new to Bing Maps tile system. I wrote a code for fast registration of satellite images by using Bing tiles as reference. I would like this process to be automatic given the image to register (sensed image). In few words, I know the resolution (RES1) I would like the Bing tiles to be (I know they wont be exactly this resolution..) and I need to get the zoomlevel which gives a ground resolution as closer as possible to RES1. I reverted this formula

`Map resolution = 156543.04 meters/pixel * cos(latitude) / (2 ^ zoomlevel) ( from http://msdn.microsoft.com/en-us/library/aa9409900but it does not work all the times. For example, I need to get the zoomlevel for a RES1= 0.5 m and a latitude of 8.15, so I would expect to get zoomlevel= 18, but instead I got 11. I guess this is due to the fact the Bing maps do not cover all the latitude with all resolution. BUT if I fix by hand zoomlevel to 18 I got the correct tiles.Is it impossible what I'm asking for? or am I missing something fundamental? Thank you so much in advance.`

### All Replies

• Thursday, August 02, 2012 8:26 PM
Moderator

I'm not quite sure how you're solving that equation to get zoom level 11, because when I plug in your numbers I get the resolution at latitude 8.15 to be 0.351m/pixel, which indeed is the closest to your target value of 0.5m/pixel...?

• Friday, August 03, 2012 9:11 AM

I try to explain myself better.

Starting from equation

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

I reverse it, in order to get an equation where the the unknown quantity to retrieve is ﻿zoomlevel, not resolution

`  zoomLevel = ln(abs(156543.04*cos(lat)/RES1))/ ln2`

where ln is the natural log (base e). If I substitute  RES1 = 0.5 and lat = ﻿﻿8.158350 I get zoomlevel = 16 (no more 11, I fix an error) still not the zoomlevel I need...

There should be some silly error in the math reversion or maybe is a precision matter..

Any suggestions?

Thank you

• Edited by Friday, August 03, 2012 12:48 PM
•
• Friday, August 03, 2012 12:23 PM
Moderator

Zoom Level = log<sub>2</sub> { 156543.04 * cos(lat) / Resolution }

For the latitude 8.15835, this gives you zoom level = 17.37934

• Friday, August 03, 2012 12:42 PM

.. I know it is log in base 2, but in Java there is not a math function for log base 2, so I need to compute

log <sub>2</sub> X = ln X / ln (2)

where ln  is the natural logarithm (base e), that is why you do not see simply log<sub>2</sub> :-)

I guess my problem is just a matter of precision, what do you think?

ps. I edited the formula for zoomlevel, there was a mistype, it is not LOG2E but LOGE2 (in the formula now is ln2)

• Edited by Friday, August 03, 2012 12:52 PM
•
• Friday, August 03, 2012 1:27 PM
Moderator

It would help if the forum would let us type proper mathematical notation!

The typo explains why I thought your maths was wonky (thought you were doing log₂e rather than logₑ2, i.e. ln2), but that still doesn't explain the discrepancy in your results. I don't use Java myself, but I can't believe there's that much error in it or else nobody would use it!

Both Python and MS Excel give me exact matching results of 17.3793394649, so there must be something in your implementation that is incorrect.

• Friday, August 03, 2012 1:32 PM
Moderator

One thought - are you supplying the input latitude in degrees or radians? (I don't know what the Java cosine function expects)... i.e. have you tried supplying 8.15835 * 2pi as the latitude?

• Marked As Answer by Friday, August 03, 2012 1:55 PM
•
• Friday, August 03, 2012 1:54 PM

YES! Thank you for the hint!

The Java Math.cos() expects the angle in radians not in degrees, so if I supply (8.15835 * pi/180) I get 18.24153, exactly what I need :-)

Have a nice we!

Martina

• Marked As Answer by Friday, August 03, 2012 1:54 PM
• Unmarked As Answer by Friday, August 03, 2012 1:54 PM
•