source: subversion/applications/routing/pyroute_webGUI/tile_base.py @ 17289

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

remove xml dependancy

File size: 2.7 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------------------
3# Base class for rendering tile-style images
4#----------------------------------------------------------------------------
5# Copyright 2008, authors:
6# * Sebastian Spaeth
7# * Oliver White
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#---------------------------------------------------------------------------
22import StringIO
23import cairo
24from tilenames import *
25
26class proj:
27  """Simple projection class for geographic data.  Converts lat/long to pixel position"""
28  def __init__(self, tx,ty,tz, to):
29    """Setup a projection. 
30    tx,ty,tz is the slippy map tile number. 
31    to is the (width,height) of an image to render onto
32    """ 
33    (S,W,N,E) = tileEdges(tx,ty,tz)
34    self.to = to
35    self.S = S
36    self.N = N
37    self.E = E
38    self.W = W
39    self.dLat = N - S
40    self.dLon = E - W
41    self.x1 = 0
42    self.y1 = 0
43    self.x2 = to[0]
44    self.y2 = to[1]
45    self.dx = self.x2 - self.x1
46    self.dy = self.y2 - self.y1
47  def project(self,lat,lon):
48    """Project lat/long (in degrees) into pixel position on the tile"""
49    pLat = (lat - self.S) / self.dLat
50    pLon = (lon - self.W) / self.dLon
51    x = self.x1 + pLon * self.dx
52    y = self.y2 - pLat * self.dy
53    return(x,y)
54
55class TileBase:
56  def draw(self):
57    pass # Override this function
58   
59  def RenderTile(self, z,x,y, outputFile):
60   
61    # Create the image
62    im = cairo.ImageSurface (cairo.FORMAT_ARGB32, 256, 256)
63    self.proj = proj(x,y,z,(256,256))  # create a projection for this tile
64    self.ctx = cairo.Context(im)  # create a drawing context
65
66    # Call some overloaded draw function from a derived class
67    self.draw()
68
69    # Either save the result to a file
70    if(outputFile):
71      im.write_to_png(outputFile) # Output to PNG
72      return
73    else:
74      # Or return it in a string
75      f = StringIO.StringIO()
76      im.write_to_png(f)
77      data = f.getvalue()
78      return(data)
79
80if(__name__ == '__main__'):
81  # Test suite: render a tile in hersham, and save it to a file
82  a = OsmRenderBase()
83  a.RenderTile(17,65385,43658, "sample2.png")
Note: See TracBrowser for help on using the repository browser.