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

Last change on this file since 8329 was 8329, checked in by ojw, 12 years ago

When merging tiles together, remove nodes which aren't a junction, or
which are within 2 pixels of the previous node in the way

File size: 2.6 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  """Download OSM data for the region covering a slippy-map tile"""
33  if(x < 0 or y < 0 or z < 0 or z > 25):
34    print "Disallowed %d,%d at %d" % (x,y,z)
35    return
36 
37  directory = 'cache/%d/%d/%d' % (z,x,y)
38  filename = '%s/data.osm' % (directory)
39  if(not os.path.exists(directory)):
40    os.makedirs(directory)
41
42  if(z < 4):
43    return
44  elif(z == DownloadLevel()):
45    # Download the data
46    URL = 'http://dev.openstreetmap.org/~ojw/api/?/map/%d/%d/%d' % (z,x,y)
47     
48    if(not os.path.exists(filename)): # TODO: allow expiry of old data
49      print "Downloading %d/%d/%d from network" % (z,x,y)
50      urlretrieve(URL, filename)
51    return(filename)
52   
53  elif(z > DownloadLevel()):
54    # use larger tile
55    while(z > DownloadLevel()):
56      z = z - 1
57      x = int(x / 2)
58      y = int(y / 2)
59    return(GetOsmTileData(z,x,y))
60
61  elif(z < DownloadLevel()):
62    # merge smaller tiles
63    filenames = []
64    for i in (0,1):
65      for j in (0,1):
66        lx = x * 2 + i
67        ly = y * 2 + j
68        lz = z + 1
69        #print "Downloading subtile %d,%d at %d" % (x,y,z)
70        # download (or otherwise obtain) each subtile
71        filenames.append(GetOsmTileData(lz,lx,ly))
72    # merge them together
73    OsmMerge(filename, z, filenames)
74    return(filename)
75   
76  print "Below download level"
77  return(None)
78
79if(__name__ == "__main__"):
80  """test mode"""
81  print GetOsmTileData(15, 16218, 10741)
Note: See TracBrowser for help on using the repository browser.