source: subversion/applications/rendering/pyrender/render_base.py @ 7786

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

Separate rendering functionality from base class, and make it easy for
the server to choose which rendering engine it wants to use

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