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

Last change on this file since 7800 was 7800, checked in by ojw, 11 years ago

outline of how to use OSMXAPI for downloads (was very very slow when
tested)

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