source: subversion/applications/rendering/pyrender/tiledata.py @ 8305

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

tiledata module may have been missed from upload?

File size: 2.7 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------------------
3# Download OSM data covering the area of a slippy-map tile
4#
5# Features:
6#  * Cached (all downloads stored in cache/z/x/y/data.osm)
7#----------------------------------------------------------------------------
8# Copyright 2008, Oliver White
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22#---------------------------------------------------------------------------
23from urllib import *
24import os
25from OsmMerge import OsmMerge
26
27def DownloadLevel():
28  """All primary downloads are done at a particular zoom level"""
29  return(15)
30
31def GetOsmTileData(z,x,y):
32  print "Downloading for %d,%d,%d" % (z,x,y)
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  directory = 'cache/%d/%d/%d' % (z,x,y)
39  filename = '%s/data.osm' % (directory)
40  if(not os.path.exists(directory)):
41    os.makedirs(directory)
42
43  if(z < 4):
44    return
45  elif(z == DownloadLevel()):
46    # Download the data
47    URL = 'http://dev.openstreetmap.org/~ojw/api/?/map/%d/%d/%d' % (z,x,y)
48     
49    if(not os.path.exists(filename)): # TODO: allow expiry of old data
50      print "Downloading from %s" % URL
51      urlretrieve(URL, filename)
52
53    print "Filename %s" % filename
54    return(filename)
55   
56  elif(z > DownloadLevel()):
57    # use larger tile
58    while(z > DownloadLevel()):
59      z = z - 1
60      x = int(x / 2)
61      y = int(y / 2)
62    return(GetOsmTileData(z,x,y))
63
64  elif(z < DownloadLevel()):
65    # merge smaller tiles
66    filenames = []
67    for i in (0,1):
68      for j in (0,1):
69        lx = x * 2 + i
70        ly = y * 2 + j
71        lz = z + 1
72        print "Downloading subtile %d,%d at %d" % (x,y,z)
73        # download (or otherwise obtain) each subtile
74        filenames.append(GetOsmTileData(lz,lx,ly))
75    # merge them together
76    print "Merging tiles together"
77    OsmMerge(filename, filenames)
78    return(filename)
79   
80  print "Below download level"
81  return(None)
82
83if(__name__ == "__main__"):
84  """test mode"""
85  print GetOsmTileData(15, 16218, 10741)
Note: See TracBrowser for help on using the repository browser.