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

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

Changed print statements to loggging calls where appropriate.

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