adding a Google Maps Client
[vuplus_dvbapp-plugin] / googlemaps / src / KMLlib.py
1 ###############################################################################
2 # Copyright (c) 2008 Rico Schulte, 3c5x9. All rights reserved.
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the "Software"),
6 # to deal in the Software without restriction, including without limitation
7 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 # DEALINGS IN THE SOFTWARE.
21 ###############################################################################
22
23 from globalmaptiles import GlobalMercator
24 from xml.dom.minidom import parse
25 from os import listdir
26     
27 class KmlPlace:
28     def __init__(self,kmlnode):
29         self.kmlnode = kmlnode
30         self.name = kmlnode.getElementsByTagName('name')[0].firstChild.data.encode("utf-8")
31         lons = kmlnode.getElementsByTagName('LookAt')[0].getElementsByTagName('longitude')[0].firstChild.data.encode("utf-8")
32         lats = kmlnode.getElementsByTagName('LookAt')[0].getElementsByTagName('latitude')[0].firstChild.data.encode("utf-8")
33                     
34         lat = float(lats)
35         lon = float(lons)
36         if lat<0.0:
37             lat = lat*(-1.0)
38         if lon<0.0:
39             lon=lon*(-1.0)
40         self.lat = lat
41         self.lon = lon
42     
43     def getTile(self,zoomlevel):
44         mercator = GlobalMercator()
45         mx, my = mercator.LatLonToMeters( self.lat, self.lon )
46         tminx, tminy = mercator.MetersToTile( mx, my, zoomlevel )
47         gx, gy = mercator.GoogleTile(tminx, tminy, zoomlevel)#+1?
48         return gx,gy,zoomlevel
49
50     def __str__(self):
51         return "KmlPlace ('"+self.name+"','"+str(self.lat)+"','"+str(self.lon)+"')"
52     
53 class KmlFolder:
54     parent = None
55     def __init__(self,kmlnode):
56         self.kmlnode = kmlnode
57         self.name = kmlnode.getElementsByTagName('name')[0].firstChild.data.encode("utf-8")
58     
59     def getFolders(self):
60         list = []
61         for i in self.kmlnode.getElementsByTagName('Folder'):
62             folder = KmlFolder(i)
63             folder.parent = self
64             list.append(folder)
65         #list.pop(0)
66         return list
67     
68     def getPlacemarks(self):
69         list = []
70         for i in self.kmlnode.getElementsByTagName('Placemark'):
71             point = KmlPlace(i)
72             try: # test if we can handle this coords
73                 point.getTile(15)# 15 is just a zoomlevel in the middle :)
74                 list.append(point)
75             except ValueError,e:
76                 print "Import Error: ",point.name,e
77         return list
78             
79 class RootFolder:
80     extension = '.kml' 
81     def __init__(self):
82         pass
83     
84     def getFolderFromFile(self,filepath):
85         return KmlFolder(self.parseFile(filepath))
86     
87     def parseFile(self,filepath):
88         print "parsing ",filepath
89         return parse(filepath)        
90
91     def getFiles(self,path):
92         list = []
93         for file in listdir(path):
94             if file.endswith(self.extension):
95                 list.append((file.split('.')[0],path+file))
96         return list
97