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

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

ability to use multiple styles for a way. This actually looks rather nice.

File size: 3.6 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,0,0,13'),
28  ('highway=motorway','0.5,0.5,1,9'),
29  ('highway=primary','0,0,0,10'),    # primary casing
30  ('highway=primary','1,0.5,0.5,6'), # primary core
31  ('highway=secondary','0,0,0,8'),   # secondary casing
32  ('highway=secondary','1,0.5,0,4'),
33  ('highway=tertiary','0,0,0,6'),    # tertiary casing
34  ('highway=tertiary','1,0.5,0,2'),
35  ('highway=residential','0,0,0,4'),
36  ('highway=residential','0.75,0.75,0.75,2'),
37  ('highway=unclassified','0,0,0,4'),
38  ('highway=unclassified','0.75,0.75,0.75,2'),
39  ('railway=rail','0,0,0,1'),
40  ('waterway=river','1,0.2,0.2,4'),
41  ('landuse=forest','0.1,0.7,0.1,0'),
42  ('natural=wood','0.1,0.7,0.1,0'),
43  ('natural=water','0.1,0.1,0.8,0'),
44  ]
45
46def wayStyles(tags):
47  styles = []
48  for (ident, style) in roadColours:
49    (tag,value) = ident.split('=')
50    if(tags.get(tag,'default') == value):
51      styles.append(style)
52  return(styles) 
53
54
55class RenderClass(OsmRenderBase):
56 
57  # Specify the background for new tiles
58  def imageBackgroundColour(self):
59    return(1,0.95,0.85,0.8)  #r,g,b,a background color
60 
61  # Draw a tile
62  def draw(self):
63    # Ways
64    for w in self.osm.ways.values():
65      for style in wayStyles(w['t']):
66        (r,g,b, width) = style.split(",")
67        width = int(width)
68        self.ctx.set_source_rgb(float(r),float(g),float(b))
69        self.ctx.set_line_width(width)
70        count = 0
71        for n in w['n']: 
72          # need to lookup that node's lat/long from the osm.nodes dictionary
73          (lat,lon) = self.osm.nodes[n]
74         
75          # project that into image coordinates
76          (x,y) = self.proj.project(lat,lon)
77         
78          # draw lines on the image
79          if(count == 0):
80            self.ctx.move_to(x, y)
81          else:
82            self.ctx.line_to(x, y)
83          count = count + 1
84        if width > 0:self.ctx.stroke()
85        else: self.ctx.fill()
86   
87    # POIs
88    if(0):
89      for poi in self.osm.poi:
90        n = poi['id']
91        (lat,lon) = self.osm.nodes[n]
92        (x,y) = self.proj.project(lat,lon)
93        s = 1
94        self.drawContext.rectangle((x-s,y-s,x+s,y+s),fill='blue')
95
96
97
98#-----------------------------------------------------------------
99# Test suite - call this file from the command-line to generate a
100# sample image
101if(__name__ == '__main__'):
102  a = RenderClass()
103  filename = "sample_"+__file__+".png"
104  a.RenderTile(17,65385,43658, filename)
105  print "------------------------------------"
106  print "Saved image to " + filename
Note: See TracBrowser for help on using the repository browser.