Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How do you convert a bing maps quad key, such as "03200320023" to coordinates in mercator virtual earth (epsg 3857) coordinate system?

I've got an algorithm to convert to coordinates in latitude longitude (e.g. the quad key above comes to "-84.19921875,33.7243385314941,-84.0234375,33.8704147338867"). I could then convert from latitude longitude to mercator coordinate, but that smells far too expensive in processing power.

The mercator virtual earth result I'm looking for given the above quadkey is: "-9373014,4011415,-9353446,4030983"

Also a question on stackoverflow

share|improve this question
    
Quadkeys aren't a coordinate reference system. Are you trying to get the center of a quad? –  Sean Apr 27 '11 at 17:50
1  
Have you reviewed Microsoft's Bing tiling scheme documentation ? –  Kirk Kuykendall Apr 27 '11 at 18:36
    
I've read all the bing maps docs. All I want to do is calculate the extents for a quadkey in epsg:3857 so I can pass a web request to mapserver for the location. –  Nick R Apr 27 '11 at 18:57

1 Answer 1

up vote 3 down vote accepted

The code below comes close. Maybe this can get you started. Update: notice the amount of error equals the last offset size.

quadkey: 03200320023
0 offset: 10018754.1713946 lod 0
3 offset: 5009377.08569731 lod 1
2 offset: 2504688.54284866 lod 2
0 offset: 1252344.27142433 lod 3
0 offset: 626172.135712164 lod 4
3 offset: 313086.067856082 lod 5
2 offset: 156543.033928041 lod 6
0 offset: 78271.5169640205 lod 7
0 offset: 39135.7584820102 lod 8
2 offset: 19567.8792410051 lod 9
3 offset: 9783.93962050256 lod 10
Mercator: x -9363230.21682095, y 4001631.30478555
error x 9783.78317905217 y-9783.69521445315

Here's the code:

private void BingTest()
{
    const double EarthRadius = 6378137;

    string qdkey = "03200320023";
    double x = 0;
    double y = 0;
    double offset = EarthRadius * Math.PI / 2.0;
    Debug.Print("quadkey: {0}", qdkey);
    for (int i = 0; i < qdkey.Length; i++)
    {
        string s = qdkey.Substring(i, 1);
        Debug.Print("{0} offset: {1} lod {2}",s, offset,i);
        switch (s)
        {
            case "0":  x -= offset; y += offset; break;
            case "1":  x += offset; y += offset; break;
            case "2":  x -= offset; y -= offset; break;
            case "3":  x += offset; y -= offset; break;
            default: throw new Exception("bad quadkey");
        }
        offset *= 0.5;
    }

    // compare with expected result
    double x0 = -9373014;
    double y0 = 4011415;
    Debug.Print("Mercator: x {0}, y {1}", x, y);
    Debug.Print("error x {0} y{1}", x - x0, y - y0);            
}
share|improve this answer
    
Superb - thank you! –  Nick R Apr 28 '11 at 5:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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