source: subversion/applications/rendering/pyrender/renderer_PIL.py @ 17724

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

1)encode the xml values properlyin OsmMerge? (quotation marks in values broke the XML previously) 2)store ways in a dict, so we store their id with them. Previously OsmMerge? would only output '%d' (literally) as id in the xml

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