source: subversion/applications/routing/pyroute-dev/poi_geonames.py @ 18446

Last change on this file since 18446 was 18446, checked in by buerste, 10 years ago

-adding/changing path to python binary
+encoding definition

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