source: subversion/applications/rendering/pyrender/OsmTileData.py @ 8207

Last change on this file since 8207 was 8193, checked in by spaetz, 11 years ago

bomb out of downloading for all exceptions rather than only IOError

File size: 3.3 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------------------
3# Download OSM data covering the area of a slippy-map tile
4#
5# Features:
6#  * Recursive (downloads are all at z15, and merged if necessary to get
7#    a larger area)
8#  * Cached (all downloads stored in cache/z/x/y/data.osm)
9#
10# DON'T RUN THIS ON LARGE AREAS WITHOUT ASKING THE OPERATOR OF THE
11# API SERVER.  Currently it's limited to downloading a z-13 area or smaller
12#----------------------------------------------------------------------------
13# Copyright 2008, Oliver White
14#
15# This program is free software: you can redistribute it and/or modify
16# it under the terms of the GNU General Public License as published by
17# the Free Software Foundation, either version 3 of the License, or
18# (at your option) any later version.
19#
20# This program is distributed in the hope that it will be useful,
21# but WITHOUT ANY WARRANTY; without even the implied warranty of
22# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23# GNU General Public License for more details.
24#
25# You should have received a copy of the GNU General Public License
26# along with this program.  If not, see <http://www.gnu.org/licenses/>.
27#---------------------------------------------------------------------------
28from tilenames import *
29from urllib import *
30from OsmMerge import OsmMerge
31import os
32import os.path
33
34def GetOsmTileData(z,x,y, AllowSplit = False):
35  """Download OSM data for the region covering a slippy-map tile"""
36  if(x < 0 or y < 0 or z < 0 or z > 25):
37    print "Disallowed %d,%d at %d" % (x,y,z)
38    return
39 
40  DownloadLevel = 15  # All primary downloads are done at a particular zoom level
41 
42  MergeLevels = 2  # How many layers 'below' the download level to go
43 
44  directory = 'cache/%d/%d' % (z,x)
45  filename = '%s/%d.osm' % (directory,y)
46  if(not os.path.exists(directory)):
47    os.makedirs(directory)
48 
49  if(z == DownloadLevel):
50    # Download the data
51    (S,W,N,E) = tileEdges(x,y,z)
52   
53   
54    # Which API to use
55    if(1): 
56      URL = 'http://%s/api/0.5/map?bbox=%f,%f,%f,%f' % ('api.openstreetmap.org',W,S,E,N)
57    else:
58      URL = 'http://%s/api/0.5/*[bbox=%f,%f,%f,%f]' % ('www.informationfreeway.org',W,S,E,N)
59   
60    if(not os.path.exists(filename)): # TODO: allow expiry of old data
61      print "Downloading %s\n  from %s" % (filename, URL)
62      try:
63        urlretrieve(URL, filename)
64        print "Done"
65      except:
66        print "Error downloading " + filename
67        unlink(filename)
68        return
69    else:
70      print "Using cached %s" % filename
71    return(filename)
72   
73  elif(z < DownloadLevel - MergeLevels):
74    print "Zoom %d not allowed" % z
75    return
76 
77  elif(z < DownloadLevel): 
78    # merge smaller tiles
79    filenames = []
80    for i in (0,1):
81      for j in (0,1):
82        lx = x * 2 + i
83        ly = y * 2 + j
84        lz = z + 1
85        print "Downloading subtile %d,%d at %d" % (x,y,z)
86        # download (or otherwise obtain) each subtile
87        filenames.append(GetOsmTileData(lz,lx,ly,AllowSplit))
88    # merge them together
89    print "Merging tiles together"
90    OsmMerge(filename, filenames)
91    return(filename)
92   
93  else: 
94    # use larger tile
95    while(z > DownloadLevel):
96      z = z - 1
97      x = int(x / 2)
98      y = int(y / 2)
99    return(GetOsmTileData(z,x,y))
100
101if(__name__ == "__main__"):
102  """test mode"""
103  GetOsmTileData(14,7788,6360, True)
104 
Note: See TracBrowser for help on using the repository browser.