source: subversion/applications/rendering/pyrender/render_cairo_base.py @ 8165

Last change on this file since 8165 was 7803, checked in by ojw, 11 years ago

fix missing newlines

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