Setting tag myplc-5.3-5
[myplc.git] / bin / plc-map.py
1 #!/usr/bin/env plcsh
2  
3 # this is a very rustic script for generating png maps from a model
4 # the model is expected in /var/www/html/sites/map.png
5 # and the output is located in the same dir as livemap.png
6 #
7 # this has many drawbacks, as it needs a cylindric projection map
8 # (there's almost no such map out there) and manual calibration
9 # you want to use the kml-based googlemap applet instead
10
11 import Image, ImageDraw
12
13 ####### first - rustic - linear positioning on a map
14 def circle (image, percentX, percentY, radiusX, radiusY, colorIn, colorOut):
15
16     imageX, imageY = image.size
17     centerX = int(imageX*percentX)
18     centerY = int(imageY*percentY)
19     x = max (0, min (centerX,imageX))
20     y = max (0, min (centerY,imageY))
21
22     x1 = x - radiusX
23     x2 = x + radiusX
24     y1 = y - radiusY
25     y2 = y + radiusY
26
27     draw = ImageDraw.Draw (image)
28     draw.chord((x1,y1,x2,y2), 0, 360, fill=colorIn, outline=colorOut )
29     del draw
30
31 latitude={'top':65.,
32           'bottom': 35.5}
33 longitude={'left':-11.,
34            'right':58.}
35     
36 def render_site (site, image, sx, sy, cIn, cOut):
37     if site['longitude'] is not None and site['latitude'] is not None:
38         px=float(longitude['left']-site['longitude'])/float(longitude['left']-longitude['right'])
39         py=float(latitude['top']-site['latitude'])/float(latitude['top']-latitude['bottom'])
40         if (px<0 or py<0 or px>1 or py>1):
41             return
42         
43         circle(image,px,py,sx,sy,cIn,cOut)
44     
45 def make_image():
46     path = '/var/www/html/sites/'
47     original = path + 'map.png'
48     live = path + 'livemap.png'
49
50     # map characteristics, in degrees.
51     # latitude : positive is north
52     # longitude : positive is east 
53
54     # circle radius in pixels
55     sxLocal,syLocal=7,7
56     # circle in and out colors
57     cInLocal , cOutLocal = '#566b8a','#bbbbbb'
58
59     # same for federating / foreign sites
60     sxForeign,syForeign=6,6
61     cInForeign , cOutForeign = '#acb3a4', '#444444'
62  
63     image = Image.open(original)
64
65     for site in GetSites({'~peer_id':None,'enabled':True}):
66         render_site (site, image, sxForeign, syForeign, cInForeign , cOutForeign)
67     # local sites go last to be more visible
68     for site in GetSites({'peer_id':None,'enabled':True}):
69         render_site (site, image, sxLocal, syLocal, cInLocal , cOutLocal)
70         
71     image.save (live)
72
73 def main ():
74     make_image ()
75
76 if __name__ == '__main__':
77     main ()