source: subversion/applications/rendering/genericHeatmapRender/KMLGenerator.py @ 30554

Last change on this file since 30554 was 16116, checked in by stevefaeembra, 10 years ago

initial version.. very basic heatmapping code using Python/Matplotlib?

File size: 3.5 KB
Line 
1# -*- coding: UTF8 -*-
2
3'''
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with this program; if not, write to the Free Software Foundation, Inc.,
16    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17'''
18
19import string
20import time
21import xml.etree.ElementTree as ET
22from locationFinder import locationFinder
23
24class KMLGenerator(object):
25    '''
26    Class to generate a KML overlay file
27    '''
28   
29    def __init__(self):
30        pass
31   
32    def render(self, locator ,PNGfilename, KMLfilename):
33        '''
34        render a KML file.
35        pass
36        - an instance of a locationFinder subclass
37        - the PNG filename (generated by heatmap PNG generator)
38        - a filename to save the KML to
39        '''
40        self.setup()
41        names=locator.getNames()
42        x=locator.getX()
43        y=locator.getY()
44        z=locator.getZ()
45        num=len(x)
46        for ix in range(0,num):
47            self.addPinpoint(names[ix], y[ix], x[ix], z[ix])
48        w,s,e,n=locator.getBounds()
49        self.addOverlay(PNGfilename, "title", (n,s,e,w))
50        self.createKML(KMLfilename)
51   
52    def setup(self):
53        self.kml=ET.Element("kml", xmlns="http://www.opengis.net/kml/2.2")
54        self.doc=ET.SubElement(self.kml, "Document")
55        self.ptsfolder=ET.SubElement(self.doc, "Folder")
56        ET.SubElement(self.ptsfolder,"name").text="Pinpoints"
57        self.olfolder=ET.SubElement(self.doc, "Folder")
58        ET.SubElement(self.olfolder, "name").text="Heatmap"
59       
60    def addPinpoint(self,name,lat,lon,count):
61        place=ET.SubElement(self.ptsfolder, "Placemark")
62        namenode=ET.SubElement(place, "name")
63        namenode.text=name
64        desc=ET.SubElement(place, "description")
65        desc.text="%s - %d photos" % (name,count)
66        point=ET.SubElement(place, "Point")
67        coords=ET.SubElement(point, "coordinates")
68        coords.text="%.6f,%.6f,0.0" % (lon,lat)
69
70    def addOverlay(self,filename,url,coords):
71        _n,_s,_e,_w = coords
72        self.overlay = ET.SubElement(self.olfolder, "GroundOverlay")
73        self.name=ET.SubElement(self.overlay, "name")
74        self.name.text="Most-photographed heatmap"
75        self.desc=ET.SubElement(self.overlay, "description")
76        self.desc.text="Flickr most photographed areas heatmap for URL %s" % url
77        self.icon=ET.SubElement(self.overlay, "Icon")
78        self.href=ET.SubElement(self.icon, "href")
79        self.href.text=filename
80        self.bbox=ET.SubElement(self.overlay, "LatLonBox")
81        self.n=ET.SubElement(self.bbox, "north")
82        self.n.text=str(_n)
83        self.s=ET.SubElement(self.bbox, "south")
84        self.s.text=str(_s)
85        self.e=ET.SubElement(self.bbox, "east")
86        self.e.text=str(_e)
87        self.w=ET.SubElement(self.bbox, "west")
88        self.w.text=str(_w)
89       
90    def createKML(self,outfilename):
91        tree = ET.ElementTree(self.kml)
92        tree.write(outfilename)
93        print "Written KML to %s" % outfilename
94   
Note: See TracBrowser for help on using the repository browser.