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

Last change on this file since 8183 was 8182, checked in by spaetz, 11 years ago

implement area filling in pyrender

File size: 3.3 KB
Line 
1#!/usr/bin/python
2#-----------------------------------------------------------------------------
3# Basic cairo renderer, that can plot OSM ways as coloured lines
4#-----------------------------------------------------------------------------
5# Copyright 2008, authors:
6# * Oliver White
7# * Sebastian Spaeth
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#----------------------------------------------------------------------------
22from render_cairo_base import OsmRenderBase
23
24roadColours = {
25  #tag=value:'r,g,b,width' with r,g,b between [0,1]
26  #a width of 0 means filling an area with a color
27  'highway=motorway':'0.5,0.5,1,9',
28  'highway=primary':'1,0.5,0.5,6',
29  'highway=secondary':'1,0.5,0,4',
30  'highway=tertiary':'1,0.5,0,2',
31  'highway=residential':'0.25,0.25,0.25,2',
32  'highway=unclassified':'0.25,0.25,0.25,2',
33  'railway=rail':'0,0,0,1',
34  'waterway=river':'1,0.2,0.2,4',
35  'landuse=forest':'0,0.7,0,0',
36  'natural=wood':'0,0.7,0,0',
37  'natural=water':'0,0,0.8,0',
38  }
39
40def wayStyle(tags):
41  for (ident, style) in roadColours.items():
42    (tag,value) = ident.split('=')
43    if(tags.get(tag,'default') == value):
44      return(style)
45  return(None) 
46
47
48class RenderClass(OsmRenderBase):
49 
50  # Specify the background for new tiles
51  def imageBackgroundColour(self):
52    return(1,0.95,0.85,0.8)  #r,g,b,a background color
53 
54  # Draw a tile
55  def draw(self):
56    # Ways
57    for w in self.osm.ways:
58      style = wayStyle(w['t'])
59     
60      if(style != None):
61        (r,g,b, width) = style.split(",")
62        width = int(width)
63        self.ctx.set_source_rgb(float(r),float(g),float(b))
64        self.ctx.set_line_width(width)
65        count = 0
66        for n in w['n']: 
67          # need to lookup that node's lat/long from the osm.nodes dictionary
68          (lat,lon) = self.osm.nodes[n]
69         
70          # project that into image coordinates
71          (x,y) = self.proj.project(lat,lon)
72         
73          # draw lines on the image
74          if(count == 0):
75            self.ctx.move_to(x, y)
76          else:
77            self.ctx.line_to(x, y)
78          count = count + 1
79        if width > 0:self.ctx.stroke()
80        else: self.ctx.fill()
81   
82    # POIs
83    if(0):
84      for poi in self.osm.poi:
85        n = poi['id']
86        (lat,lon) = self.osm.nodes[n]
87        (x,y) = self.proj.project(lat,lon)
88        s = 1
89        self.drawContext.rectangle((x-s,y-s,x+s,y+s),fill='blue')
90
91
92
93#-----------------------------------------------------------------
94# Test suite - call this file from the command-line to generate a
95# sample image
96if(__name__ == '__main__'):
97  a = RenderClass()
98  filename = "sample_"+__file__+".png"
99  a.RenderTile(17,65385,43658, filename)
100  print "------------------------------------"
101  print "Saved image to " + filename
Note: See TracBrowser for help on using the repository browser.