source: subversion/applications/routing/pyroute/mod_geonames.py @ 5649

Last change on this file since 5649 was 5498, checked in by ojw, 12 years ago

reading geonames (gets XML from a file at the moment, instead of
downloading it)

File size: 1.3 KB
Line 
1from mod_base import dataSource, dataGroup, dataItem
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(dataSource):
26    def __init__(self):
27        dataSource.__init__(self)
28        # self.add(name, feed)
29        g = geonamesParser("data/wiki.xml")
30        self.add("Wikipedia entries",g)
31
32    def add(self, name, parser):
33        group = dataGroup(name)
34        count = 0
35        for item in parser.entries:
36            x = dataItem(item['lat'], item['lng'])
37            x.title = item['title']
38            group.items.append(x)
39            count = count + 1
40        print "Added %d items from %s" % (count, name)
41
42        self.groups.append(group)
43
44if __name__ == "__main__":
45    g = geonamesParser("data/wiki.xml")
Note: See TracBrowser for help on using the repository browser.