source: subversion/applications/routing/pyroute/poi_geonames.py @ 20924

Last change on this file since 20924 was 5859, checked in by ojw, 12 years ago

rename "mod_*" to "poi_*"

File size: 1.3 KB
Line 
1from poi_base import *
2import feedparser
3from xml.sax import make_parser, handler
4
5class geonamesParser(handler.ContentHandler):
6  def __init__(self, filename):
7    self.entries = []
8    parser = make_parser()
9    parser.setContentHandler(self)
10    parser.parse(filename)
11  def startElement(self, name, attrs):
12    if(name == "entry"):
13      self.entry = {}
14    self.text = ''
15  def characters(self, text):
16    self.text = self.text + text
17  def endElement(self, name):
18    if(name in ('title')):
19      self.entry[name] = self.text
20    if(name in ('lat','lng')):
21      self.entry[name] = float(self.text)
22    if(name == "entry"):
23      self.entries.append(self.entry)
24     
25class geonames(poiModule):
26    def __init__(self, modules):
27        poiModule.__init__(self, modules)
28        g = geonamesParser("data/wiki.xml")
29        self.add("Wikipedia entries",g)
30
31    def add(self, name, parser):
32        group = poiGroup(name)
33        count = 0
34        for item in parser.entries:
35            x = poi(item['lat'], item['lng'])
36            x.title = item['title']
37            group.items.append(x)
38            count = count + 1
39        print "Added %d items from %s" % (count, name)
40
41        self.groups.append(group)
42
43if __name__ == "__main__":
44    g = geonamesParser("data/wiki.xml")
Note: See TracBrowser for help on using the repository browser.