source: subversion/applications/rendering/pyrender/renderer_sample_cairo.py @ 7797

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

merge the cairo changes in

File size: 3.3 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, authors:
16# * Sebastian Spaeth
17# * 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_cairo_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.ctx is a Cairo drawing context
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    self.ctx.set_source_rgb(0, 0, 0)
56    # Example of parsing through all the ways
57    for w in self.osm.ways:
58      count = 0
59     
60      # way['n'] contains the list of its nodes
61      for n in w['n']: 
62       
63        # need to lookup that node's lat/long from the osm.nodes dictionary
64        (lat,lon) = self.osm.nodes[n]
65       
66        # project that into image coordinates
67        (x,y) = self.proj.project(lat,lon)
68       
69        # draw lines on the image
70        if(count == 0):
71          self.ctx.move_to(x, y)
72        else:
73          self.ctx.line_to(x, y)
74       
75        count = count + 1
76      self.ctx.stroke()
77   
78    # Similar code for points of interest
79    # (they have just one node, as ['id'])
80    if(0):
81      for poi in self.osm.poi:
82        n = poi['id']
83        (lat,lon) = self.osm.nodes[n]
84        (x,y) = self.proj.project(lat,lon)
85        # TODO: draw icon/text/whatever at x,y
86
87
88#-----------------------------------------------------------------
89# Test suite - call this file from the command-line to generate a
90# sample image
91if(__name__ == '__main__'):
92  a = RenderClass()
93  filename = "sample_"+__file__+".png"
94  a.RenderTile(17,65385,43658, filename)
95  print "------------------------------------"
96  print "Saved image to " + filename
Note: See TracBrowser for help on using the repository browser.