source: subversion/applications/rendering/pyrender/render_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, 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#---------------------------------------------------------------------------
23import Image
24import StringIO
25import ImageDraw
26import xml.sax
27from OsmTileData import *
28from parseOsm import *
29from tilenames import *
30
31class proj:
32  """Simple projection class for geographic data.  Converts lat/long to pixel position"""
33  def __init__(self, tx,ty,tz, to):
34    """Setup a projection. 
35    tx,ty,tz is the slippy map tile number. 
36    to is the (width,height) of an image to render onto
37    """ 
38    (S,W,N,E) = tileEdges(tx,ty,tz)
39    self.to = to
40    self.S = S
41    self.N = N
42    self.E = E
43    self.W = W
44    self.dLat = N - S
45    self.dLon = E - W
46    self.x1 = 0
47    self.y1 = 0
48    self.x2 = to[0]
49    self.y2 = to[1]
50    self.dx = self.x2 - self.x1
51    self.dy = self.y2 - self.y1
52  def project(self,lat,lon):
53    """Project lat/long (in degrees) into pixel position on the tile"""
54    pLat = (lat - self.S) / self.dLat
55    pLon = (lon - self.W) / self.dLon
56    x = self.x1 + pLon * self.dx
57    y = self.y2 - pLat * self.dy
58    return(x,y)
59
60class OsmRenderBase:
61 
62  def imageBackgroundColour(self):
63    return("blue") # Override this function
64 
65  def draw(self):
66    pass # Override this function
67   
68  def Render(self, im,filename,tx,ty,tz,layer):
69    """Render an OSM tile
70    im - an image to render onto
71    filename - location of OSM data for the tile
72    tx,ty,tz - the tile number
73    layer - which map style to use
74    """
75   
76    self.osm = parseOsm(filename)  # get OSM data into memory
77    self.proj = proj(tx,ty,tz,im.size)  # create a projection for this tile
78    self.drawContext = ImageDraw.Draw(im)  # create a drawing context
79   
80    # Call the draw function
81    self.draw()
82   
83    del self.drawContext # cleanup 
84 
85  def RenderTile(self, z,x,y, outputFile):
86    """Render an OSM tile
87    z,x,y - slippy map tilename
88    outputFile - optional file to save to
89    otherwise returns PNG image data"""
90   
91    # Create the image
92    im = Image.new('RGBA',(256,256), self.imageBackgroundColour())
93 
94    # Get some OSM data for the area, and return which file it's stored in
95    filename = GetOsmTileData(z,x,y)
96    if(filename == None):
97      return(None)
98 
99    # Render the map
100    self.Render(im,filename,x,y,z,'default')
101 
102    # Either save the result to a file
103    if(outputFile):
104      im.save(outputFile)
105      return
106    else:
107      # Or return it in a string
108      f = StringIO.StringIO()
109      im.save(f, "PNG")
110      data = f.getvalue()
111      return(data)
112
113if(__name__ == '__main__'):
114  # Test suite: render a tile in hersham, and save it to a file
115  a = OsmRenderBase()
116  a.RenderTile(17,65385,43658, "sample2.png")
Note: See TracBrowser for help on using the repository browser.