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

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

fix missing newlines

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