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

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

New COWS distribution. See [4005] for description.

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