none
Getting the accurate pixel location in (lat,lng) from the quadkey RRS feed

  • Question

  • Hallo,

    I need to get the accurate (lat,lng) location starting from a tile quadkey.

    I use the following functions (provided by Microsoft):

    -> tileXY[] = QuadKeyToTileXY(qk)

    -> tilePxXY[] = TileXYToPixelXY(tileXY[0],tileXY[1] )

    ->latLonTmp[ ] = veClient.PixelXYToLatLong(tilePxXY[0],tilePxXY[1], BingzoomLevel);

    For the QK = 311212020330, I get

    (lat,lng) [-28.767659105691237, 146.77734375]

    If I convert from WGS84 to Bing Pseudo mercator I get

    (bingX,bingY) = [1.6339179163964843E7 -3346107.3497460936]

    by using the following method (taken from here)

    double[] WGS84toGoogleBing(double lon, double lat) {
      double x = lon * 20037508.34 / 180;
      double y = Math.Log(Math.Tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
      y = y * 20037508.34 / 180;
      return new double[] {x, y};
    }

    The problem is that these Bing coordinates are shifted by some pixels up and to the right (I cannot tell the exact number, I would say from 1->3). What am I doing wrong? or is there any parameter I should add to make the conversion really precise (like in this post)?

    I posted a similar question here, the answer made me solve an issue I had in the code, but now I'm stuck with this systematic shift.

    I guess the problem could rely in the registration point as suggested by Tanoshimi in my previous post, but I'd like to understand better how to deal with it.

    thank you!



    • Edited by martina78 Friday, December 7, 2012 4:46 PM update due to similar posts
    Friday, December 7, 2012 4:00 PM

Answers

  • The math you need can be found here: http://msdn.microsoft.com/en-us/library/bb259689.aspx

    To get the top left coordinate of the tile do the following:

    QuadKeyToTileXY -> TileXYToPixelXY -> PixelXYToLatLong

    Only use the methods in this code, do not use any methods from the map control as this will mess things up. Note that the pixel to latlong logic in the map control using local pixel coordinates as this math sample uses global pixel coordinates where 90,-180 = 0,0 pixel.

    In terms of the lat/long values. There is room for error as tiles at high zoom levels have pixels that cover a large area. As such ensure that the whole calculation is done against data all at the same zoom level.


    http://rbrundritt.wordpress.com

    Monday, December 10, 2012 9:50 AM
  • Why are you using 1.6319611284726562E7 as the x-coord in your world file, when in your first post you calculated 1.6339179163964843E7 ?

    Also, I don't know anything about the QGIS Openlayer plugin, but if you're trying to take a Bing Maps tile image and import it into another application it sounds like you might be in breach of the Terms of Use anyway...Bing Maps imagery can only be displayed using one of the Bing Maps controls.


    twitter: @alastaira blog: http://alastaira.wordpress.com/ | Pro Spatial with SQL Server 2012

    • Marked as answer by martina78 Tuesday, December 11, 2012 4:12 PM
    Tuesday, December 11, 2012 3:52 PM
    Moderator

All replies

  • The math you need can be found here: http://msdn.microsoft.com/en-us/library/bb259689.aspx

    To get the top left coordinate of the tile do the following:

    QuadKeyToTileXY -> TileXYToPixelXY -> PixelXYToLatLong

    Only use the methods in this code, do not use any methods from the map control as this will mess things up. Note that the pixel to latlong logic in the map control using local pixel coordinates as this math sample uses global pixel coordinates where 90,-180 = 0,0 pixel.

    In terms of the lat/long values. There is room for error as tiles at high zoom levels have pixels that cover a large area. As such ensure that the whole calculation is done against data all at the same zoom level.


    http://rbrundritt.wordpress.com

    Monday, December 10, 2012 9:50 AM
  • Thank you for our reply!

    Yes, I'm using the correct methods.. 

    I understand that a little error could rise from the actual pixel resolution, but I would expect no more then 1 pixel shift.

    " As such ensure that the whole calculation is done against data all at the same zoom level."

    I get a shift even if I superimpose the tile against a Bing map layer..

    Monday, December 10, 2012 11:01 AM
  • What do you mean that you're getting a "shift"? What values are you comparing that you believe should line up, but are actually appearing 1 to 3 pixels out of place? And at what zoom levels are they appearing misaligned? (At zoom level 19, such misalignment would imply a difference of only a metre or so, whereas at zoom level 1 it would imply that your data was 100s of km out). Are you sure that your source data is defined using WGS84 and not a different datum?

    I notice your example is in Australia - lat/lng coordinates measured relative to the Australian Geodetic Datum can differ from WGS84 by up to several hundred metres...


    twitter: @alastaira blog: http://alastaira.wordpress.com/ | Pro Spatial with SQL Server 2012

    • Marked as answer by Ricky_Brundritt Tuesday, December 11, 2012 11:14 AM
    • Unmarked as answer by martina78 Tuesday, December 11, 2012 12:52 PM
    Monday, December 10, 2012 8:30 PM
    Moderator
  • I mean my final image is shifted upwards and to the right.

    Could you please tell me which are your (lat, lng) coordinates for QK =31121202032?

    I get  [ -28.767659105691234, 146.6015625]. The I use these values to compute the (3857_x, 3857_y):

    3857_x = Lng * 20037508.34/180;
    3857_y = Math.log(Math.tan((90 + Lat * Math.PI / 360)) / (Math.PI / 180);
    3857_y = 3857_y * 20037508.34 / 180;

    Zoom level is 11 and resolution is 66.49927017492764 m/px.

    I create a .tfw for it with 

    66.49927017492764
    0.0
    0.0
    -66.49927017492764
    1.6319611284726562E7
    -3346107.3497460936

    If I superimpose this tile to the Bing Layer provided by QGIS Openlayer plugin here comes the shift..

    Tuesday, December 11, 2012 3:01 PM
  • Why are you using 1.6319611284726562E7 as the x-coord in your world file, when in your first post you calculated 1.6339179163964843E7 ?

    Also, I don't know anything about the QGIS Openlayer plugin, but if you're trying to take a Bing Maps tile image and import it into another application it sounds like you might be in breach of the Terms of Use anyway...Bing Maps imagery can only be displayed using one of the Bing Maps controls.


    twitter: @alastaira blog: http://alastaira.wordpress.com/ | Pro Spatial with SQL Server 2012

    • Marked as answer by martina78 Tuesday, December 11, 2012 4:12 PM
    Tuesday, December 11, 2012 3:52 PM
    Moderator
  • In the first post I gave the Bing coordinates for QK= 311212020330, in the latter one for QK = 31121202032.. Sorry for that.

    Ops, I did not know I could not visualize a tile in another application. It was for debugging reasons but now that I know the Terms I wont do it again..

    Thank you for your help.

    Tuesday, December 11, 2012 4:07 PM