IMDb updated to new website layout
[enigma2-plugins.git] / googlemaps / src / KMLlib.py
1 from globalmaptiles import GlobalMercator
2 from xml.dom.minidom import parse
3 from os import listdir
4
5 class KmlPlace:
6     def __init__(self,kmlnode):
7         self.kmlnode = kmlnode
8         self.name = kmlnode.getElementsByTagName('name')[0].firstChild.data.encode("utf-8")
9         lons = kmlnode.getElementsByTagName('LookAt')[0].getElementsByTagName('longitude')[0].firstChild.data.encode("utf-8")
10         lats = kmlnode.getElementsByTagName('LookAt')[0].getElementsByTagName('latitude')[0].firstChild.data.encode("utf-8")
11
12         lat = float(lats)
13         lon = float(lons)
14         if lat<0.0:
15             lat = lat*(-1.0)
16         if lon<0.0:
17             lon=lon*(-1.0)
18         self.lat = lat
19         self.lon = lon
20
21     def getTile(self,zoomlevel):
22         mercator = GlobalMercator()
23         mx, my = mercator.LatLonToMeters( self.lat, self.lon )
24         tminx, tminy = mercator.MetersToTile( mx, my, zoomlevel )
25         gx, gy = mercator.GoogleTile(tminx, tminy, zoomlevel)#+1?
26         return gx,gy,zoomlevel
27
28     def __str__(self):
29         return "KmlPlace ('"+self.name+"','"+str(self.lat)+"','"+str(self.lon)+"')"
30
31 class KmlFolder:
32     parent = None
33     def __init__(self,kmlnode):
34         self.kmlnode = kmlnode
35         self.name = kmlnode.getElementsByTagName('name')[0].firstChild.data.encode("utf-8")
36
37     def getFolders(self):
38         list = []
39         for i in self.kmlnode.getElementsByTagName('Folder'):
40             folder = KmlFolder(i)
41             folder.parent = self
42             list.append(folder)
43         #list.pop(0)
44         return list
45
46     def getPlacemarks(self):
47         list = []
48         for i in self.kmlnode.getElementsByTagName('Placemark'):
49             point = KmlPlace(i)
50             try: # test if we can handle this coords
51                 point.getTile(15)# 15 is just a zoomlevel in the middle :)
52                 list.append(point)
53             except ValueError,e:
54                 print "Import Error: ",point.name,e
55         return list
56
57 class RootFolder:
58     extension = '.kml'
59     def __init__(self):
60         pass
61
62     def getFolderFromFile(self,filepath):
63         return KmlFolder(self.parseFile(filepath))
64
65     def parseFile(self,filepath):
66         print "parsing ",filepath
67         return parse(filepath)
68
69     def getFiles(self,path):
70         list = []
71         for file in listdir(path):
72             if file.endswith(self.extension):
73                 list.append((file.split('.')[0],path+file))
74         return list
75