source: subversion/applications/routing/pyroute/tiles.py @ 5721

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

Move the map tile code into its own module

Add headers and usage instructions to a bunch of modules

File size: 2.8 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------
3# Tile image handler (download, cache, and display tiles)
4#
5# Usage:
6#   (library code for pyroute GUI, not for direct use)
7#-----------------------------------------------------------------------------
8# Copyright 2007, 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 base import pyrouteModule
24from tilenames import *
25import urllib
26import os
27import cairo
28
29class tileHandler(pyrouteModule):
30  def __init__(self, modules):
31    pyrouteModule.__init__(self, modules)
32    self.images = {}
33   
34  def imageName(self,x,y,z):
35    return("%d_%d_%d" % (z,x,y))
36 
37  def loadImage(self,x,y,z):
38    name = self.imageName(x,y,z)
39    if name in self.images.keys():
40      return
41    filename = "cache/%s.png" % name
42    if not os.path.exists(filename):
43      print "downloading %s"%name
44      url = tileURL(x,y,z)
45      urllib.urlretrieve(url, filename)
46    else:
47      print "loading %s from cache"%name
48    self.images[name]  = cairo.ImageSurface.create_from_png(filename)
49   
50  def drawImage(self,cr, tile, bbox):
51    name = self.imageName(tile[0],tile[1],tile[2])
52    if not name in self.images.keys():
53      return
54    cr.save()
55    cr.translate(bbox[0],bbox[1])
56    cr.scale((bbox[2] - bbox[0]) / 256.0, (bbox[3] - bbox[1]) / 256.0)
57    cr.set_source_surface(self.images[name],0,0)
58    cr.paint()
59    cr.restore()
60   
61  def zoomFromScale(self,scale):
62    if(scale > 0.046):
63      return(10)
64    if(scale > 0.0085):
65      return(13)
66    if(scale > 0.0026):
67      return(15)
68    return(17)
69   
70  def tileZoom(self):
71    return(self.zoomFromScale(self.m['projection'].scale))
72 
73  def draw(self, cr):
74    proj = self.m['projection']
75    z = self.tileZoom()
76    view_x1,view_y1 = latlon2xy(proj.N, proj.W, z)
77    view_x2,view_y2 = latlon2xy(proj.S, proj.E, z)
78    for x in range(int(floor(view_x1)), int(ceil(view_x2))):
79      for y in range(int(floor(view_y1)), int(ceil(view_y2))):
80        S,W,N,E = tileEdges(x,y,z) 
81        x1,y1 = proj.ll2xy(N,W)
82        x2,y2 = proj.ll2xy(S,E)
83        self.loadImage(x,y,z)
84        self.drawImage(cr,(x,y,z),(x1,y1,x2,y2))
Note: See TracBrowser for help on using the repository browser.