source: subversion/applications/routing/pyroutelib2/tiledata.py @ 34645

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

data supply for a new version of pyroute library

File size: 2.0 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
25
26def DownloadLevel():
27  """All primary downloads are done at a particular zoom level"""
28  return(15)
29
30def GetOsmTileData(z,x,y):
31  """Download OSM data for the region covering a slippy-map tile"""
32  if(x < 0 or y < 0 or z < 0 or z > 25):
33    print "Disallowed %d,%d at %d" % (x,y,z)
34    return
35 
36  directory = 'cache/%d/%d/%d' % (z,x,y)
37  filename = '%s/data.osm' % (directory)
38  if(not os.path.exists(directory)):
39    os.makedirs(directory)
40 
41  if(z == DownloadLevel()):
42    # Download the data
43    URL = 'http://dev.openstreetmap.org/~ojw/api/?/map/%d/%d/%d' % (z,x,y)
44     
45    if(not os.path.exists(filename)): # TODO: allow expiry of old data
46      urlretrieve(URL, filename)
47    return(filename)
48   
49  elif(z > DownloadLevel()):
50    # use larger tile
51    while(z > DownloadLevel()):
52      z = z - 1
53      x = int(x / 2)
54      y = int(y / 2)
55    return(GetOsmTileData(z,x,y))
56  return("")
57
58if(__name__ == "__main__"):
59  """test mode"""
60  print GetOsmTileData(15, 16218, 10741)
Note: See TracBrowser for help on using the repository browser.