source: subversion/applications/rendering/pyrender/renderer_sample.py @ 7788

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

rename the sample rendering engine

File size: 3.4 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------# Sample rendering engine
3#
4# Copy and modify this file to create your own rendering engine
5# - please keep this file clean so it's easy to understand how to
6#   derive copies of it
7#
8# To serve tiles using your new rendering engine, change the line at
9# the top of server.py, from:
10#   import renderer_simple as RenderModule
11# to:
12#   import renderer_yourNewModule as RenderModule
13#
14# ..then run server.py and view it on http://localhost:1280/ as usual
15#-----------------------------------------------------------------------------# Copyright 2008, Oliver White
16#
17# This program is free software: you can redistribute it and/or modify
18# it under the terms of the GNU General Public License as published by
19# the Free Software Foundation, either version 3 of the License, or
20# (at your option) any later version.
21#
22# This program is distributed in the hope that it will be useful,
23# but WITHOUT ANY WARRANTY; without even the implied warranty of
24# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25# GNU General Public License for more details.
26#
27# You should have received a copy of the GNU General Public License
28# along with this program.  If not, see <http://www.gnu.org/licenses/>.
29#----------------------------------------------------------------------------
30from render_base import OsmRenderBase
31
32class RenderClass(OsmRenderBase):
33 
34  # Specify the background for new tiles
35  def imageBackgroundColour(self):
36    return("white")
37   
38  # Draw a tile
39  def draw(self):
40    # Certain things have already been created for you at this point:
41    #
42    #  * self.osm contains the OpenStreetMap data for your tile
43    #     - w is a list of ways
44    #     - poi is a list of points-of-interest
45    #     - nodes is a hash of lat/long values for nodes
46    #
47    #  * self.drawContext is a PIL object (Python Imageing library)
48    #     - Documentation is available at:
49    #       http://www.pythonware.com/library/pil/handbook/imagedraw.htm
50    #
51    #  * self.proj is a projection module, which you can pass lat/long
52    #    values and get image coordinates back, like:
53    #    (x,y) = self.proj.project(lat,lon)
54   
55    # Example of parsing through all the ways
56    for w in self.osm.ways:
57      last = (0,0,False)
58     
59      # way['n'] contains the list of its nodes
60      for n in w['n']: 
61       
62        # need to lookup that node's lat/long from the osm.nodes dictionary
63        (lat,lon) = self.osm.nodes[n]
64       
65        # project that into image coordinates
66        (x,y) = self.proj.project(lat,lon)
67       
68        # draw lines on the image
69        if(last[2]):
70          self.drawContext.line((last[0], last[1], x, y), fill='black')
71        last = (x,y,True)
72   
73    # Similar code for points of interest
74    # (they have just one node, as ['id'])
75    for poi in self.osm.poi:
76      n = poi['id']
77      (lat,lon) = self.osm.nodes[n]
78      (x,y) = self.proj.project(lat,lon)
79      s = 1
80      self.drawContext.rectangle((x-s,y-s,x+s,y+s),fill='blue')
81
82
83
84#-----------------------------------------------------------------
85# Test suite - call this file from the command-line to generate a
86# sample image
87if(__name__ == '__main__'):
88  a = RenderClass()
89  filename = "sample_"+__file__+".png"
90  a.RenderTile(17,65385,43658, filename)
91  print "------------------------------------"
92  print "Saved image to " + filename
Note: See TracBrowser for help on using the repository browser.