source: cows/trunk/cows/service/imps/pointrenderer.py @ 5424

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/service/imps/pointrenderer.py@5424
Revision 5424, 2.9 KB checked in by spascoe, 11 years ago (diff)

Removing tabs from python source

Line 
1# BSD Licence
2# Copyright (c) 2009, Science & Technology Facilities Council (STFC)
3# All rights reserved.
4#
5# See the LICENSE file in the source distribution of this software for
6# the full license text.
7
8# Adapted for numpy/ma/cdms2 by convertcdms.py
9"""
10Implementation of station point rendering classe.
11
12@author: Arif Shaon
13"""
14
15import os
16import sys
17import ImageColor, Image, ImageDraw, ImageFont
18from cows.service.imps.StationCollection import StationCollection
19from cows.bbox_util import geoToPixel
20
21import pylons
22
23import logging
24log = logging.getLogger(__name__)
25
26class PointRenderer():
27
28
29    mimeType = 'image/png'
30
31    def renderPoint(self, bbox, stations, width, height, cmap, icon ):
32        """Creates an RGBA PNG with each station within given BBox represented by an icon.
33        """
34
35        # calculate BBox area
36        bboxW = bbox[2] - bbox[0]
37        bboxH = bbox[3] - bbox[1]
38        bboxA = bboxW * bboxH
39        log.debug("bbox area %s" % bboxA)
40
41        #load the primary icon using the icon path
42        point = Image.open(icon)
43
44        # create a transparent background image
45        tileImg = Image.new('RGBA', (width, height), (0,0,0,0))
46
47        #go through each station in the station list paste its location on to the tileimg
48
49        for station in stations:
50                # if station contains data between 2006-1-1 and 2006-12-31 (
51                # csmlGrapher is designed to handle datasets between these dates)
52                if station.checkDates("2006-01-01","2006-12-31"):
53                        # then load a different icon to identify that, else keep the original icon
54                        point = Image.open(icon+'1')
55                else:
56                        point = Image.open(icon)
57                # convert lon, lat to x, y coordiate of the image
58                ox,oy = geoToPixel(station.lon, station.lat, bbox, width, height)
59                # if bbox area is less than 100 then print name of each station next to its icon
60                if bboxA < 100:
61                        tileImg.paste(self.txt2img(station.desc, "lubR08.pil"),(ox+16, oy+5) )
62
63               
64                tileImg.paste(point, (ox-10, oy-9))
65
66        return tileImg
67
68
69    def txt2img(self, label, fontname="courB08.pil", imgformat="PNG", fgcolor=(0,0,0), bgcolor=(255,255,255),rotate_angle=0):
70
71        """Render text as image."""
72        fontpath = pylons.config['pilfonts_dir']
73        font = ImageFont.load(os.path.join(fontpath,fontname))
74        imgOut = Image.new("RGBA", (20,49), (0,0,0,0))
75
76        # calculate space needed to render text
77        draw = ImageDraw.Draw(imgOut)
78        sizex, sizey = draw.textsize(label, font=font)
79
80        imgOut = imgOut.resize((sizex,sizey))
81
82        # render text into image draw area
83        draw = ImageDraw.Draw(imgOut)
84        draw.text((0, 0), label, fill=fgcolor, font=font)
85
86        if rotate_angle:
87                imgOut = imgOut.rotate(rotate_angle)
88
89        return imgOut
Note: See TracBrowser for help on using the repository browser.