User:Steevc/bsscript

From OpenStreetMap Wiki
Jump to navigation Jump to search

Intro

My script to download missing tiles for BSGPSPDA. Needs some work to make it generally useful. It's currently too hard-coded.

It may be possible to do the same with wget, but this was an interesting exercise and may ultimately give more flexibility

Code

#!/usr/bin/env python
"""
  Author:     Steve Clark and Jonas Svensson
  Program:    Fetch missing OSM tiles
  Date:       14/09/07, 13/10/07

  Description:  Reads a MissingTiles.txt file and downloads the tiles
                  into the appropriate directory structure
                  For now everything runs from the current directory
                  Also depends on the URLs always being the same format
                  e.g.
                  http://dev.openstreetmap.org/~ojw/Tiles/tile.php/13/4090/2709.png
"""
import urllib
import os

f = open('MissingTiles.txt', 'r')
while True:
    line = f.readline()
    if line == "": break
    line = line.strip()
    tilefile = os.path.basename(line)
    path1 = os.path.dirname(line)
    tilecolumn = os.path.basename(path1)
    path2 = os.path.dirname(path1)
    zoomlevel = os.path.basename(path2)
    if not os.path.isdir(zoomlevel):
       os.mkdir(zoomlevel)
       print "Made directory " + zoomlevel
    tiledir = zoomlevel+os.path.sep+tilecolumn
    if not os.path.isdir(tiledir):
       os.mkdir(tiledir)
       print "Made directory " + tiledir
    if not os.path.isfile(tiledir+os.path.sep+tilefile):
        urllib.urlretrieve(line, tiledir+os.path.sep+tilefile)
        print "Downloaded tile " + tilefile
f.close()