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

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

-reodering imports

  • Property svn:keywords set to Rev
File size: 1.9 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4"""
5"""
6
7__version__ = "$Rev: 18456 $"[1:-2]
8__license__ = """This program is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program. If not, see <http://www.gnu.org/licenses/>."""
20_debug = 0
21
22
23from poi_base import *
24from xml.sax import make_parser, handler
25import feedparser
26
27class geonamesParser(handler.ContentHandler):
28        def __init__(self, filename):
29                self.entries = []
30                parser = make_parser()
31                parser.setContentHandler(self)
32                parser.parse(filename)
33        def startElement(self, name, attrs):
34                if(name == "entry"):
35                        self.entry = {}
36                self.text = ''
37        def characters(self, text):
38                self.text = self.text + text
39        def endElement(self, name):
40                if(name in ('title')):
41                        self.entry[name] = self.text
42                if(name in ('lat','lng')):
43                        self.entry[name] = float(self.text)
44                if(name == "entry"):
45                        self.entries.append(self.entry)
46
47class geonames(poiModule):
48                def __init__(self, modules):
49                                poiModule.__init__(self, modules)
50                                g = geonamesParser("data/wiki.xml")
51                                self.add("Wikipedia entries",g)
52
53                def add(self, name, parser):
54                                group = poiGroup(name)
55                                count = 0
56                                for item in parser.entries:
57                                                x = poi(item['lat'], item['lng'])
58                                                x.title = item['title']
59                                                group.items.append(x)
60                                                count = count + 1
61                                print "Added %d items from %s" % (count, name)
62
63                                self.groups.append(group)
64
65if __name__ == "__main__":
66                g = geonamesParser("data/wiki.xml")
Note: See TracBrowser for help on using the repository browser.