title: How do I convert the x, y, and zoom coordinates of a map tile to the corresponding coordinates in a quadtree map system?
body: <p>I am trying to access map tiles from a web service.</p>
  
  <p>The tiles are organized in a <a href="http://docs.overviewer.org/en/latest/design/designdoc/#quadtrees" rel="nofollow">quad-tree like this</a>. In the filesystem, they are organized so that <code>3/0/0/1/1/2.png</code>, for example, refers to the tile starting at the base, under the third quadrant, then the 0th quadrant, then the 0th, and so fourth.</p>
  
  <p>In my application, I am asked for the x, y, and zoom values of a map tile, and I have to return a URL. How do I convert these values to the file path format described above?</p>
  
  <p>I <a href="http://www.dzone.com/snippets/converting-between-2-google" rel="nofollow">found</a> this python script, which claims to turn x, y, and zoom values into google maps tile identifier that can be used somehow to access a given tile, but I cannot figure out how I would modify it to output in the format I want:</p>
  
  <pre><code>def quadtree(x,y, zoom):
      out = []
      m = {(0,0):'q', (0,1):'t', (1,0):'r', (1,1):'s'}
      for i in range(17-zoom):
          x, rx = divmod(x, 2)
          y, ry = divmod(y, 2)
          out.insert(0, m[(rx,ry)])
      return 't' + ''.join(out)
  </code></pre>
  
  <p>Heres an example execution of that:</p>
  
  <pre><code>&gt;&gt;&gt; quadtree(20,49,10)
  'tqtsqrqt'
  </code></pre>
  
  <p>What I would like to happen:</p>
  
  <pre><code>&gt;&gt;&gt; quadtree(5,3,3)
      '2/1/3'
  </code></pre>
  
  <p>This is obviously not near the output that I would like, but it seems to be a starting point (I guess).</p>
  
  <p>I will take a solution or suggestion in any coding language (or pseudocode).</p>