source: subversion/applications/routing/pyroute-dev/poi_osm.py @ 18444

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

-switching to windows eol-markers
-converting all files to UTF8 with bom

File size: 3.8 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------
3# OSM POI handler for pyroute
4#
5#------------------------------------------------------
6# Copyright 2007, Oliver White
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#------------------------------------------------------
21from xml.sax import make_parser, handler
22from poi_base import *
23import os
24import urllib
25
26class osmPoiModule(poiModule, handler.ContentHandler):
27  def __init__(self, modules):
28    poiModule.__init__(self, modules)
29    self.draw = False
30    self.loadPOIs("all", "amenity|shop=*")
31   
32  def loadPOIs(self, name, search):
33    filename = os.path.join(os.path.dirname(__file__),
34                            "data", "poi_%s.osm" % name)
35   
36    url = "http://www.informationfreeway.org/api/0.5/node[%s][%s]" %(search,
37                                                                     self.bbox())
38   
39    if(not os.path.exists(filename)):
40      print "Downloading POIs from OSM"
41      urllib.urlretrieve(url, filename)
42    self.load(filename, os.path.join(os.path.dirname(__file__),
43                            "Setup", "poi.txt"))
44
45  def bbox(self):
46    # TODO: based on location!
47    return "bbox=-6,48,2.5,61"
48 
49  def load(self, filename, listfile):
50    self.filters = []
51    print "Loading POIs from %s" % listfile
52    f = open(listfile,"r")
53    try:
54      for line in f:
55        if(len(line) > 1):
56          text = line.rstrip()
57          name, filter = text.split('|')
58          group = poiGroup(name)
59          self.groups.append(group)
60          self.filters.append({'name':name,'filter':filter,'group':group})
61    finally:
62      f.close()
63   
64    if(not os.path.exists(filename)):
65      print "Can't load %s"%filename
66      return
67    self.inNode = False
68    parser = make_parser()
69    parser.setContentHandler(self)
70    parser.parse(filename)
71   
72 
73  def startElement(self, name, attrs):
74    if name == "node":
75      self.currentNode = { \
76        'lat': float(attrs.get('lat')),
77        'lon': float(attrs.get('lon'))}
78      self.inNode = True
79    if name == "tag" and self.inNode:
80      self.currentNode[attrs.get('k')] = attrs.get('v')
81 
82  def endElement(self, name):
83    if(name == "node"):
84      self.storeNode(self.currentNode)
85      self.inNode = False
86
87  def passesFilter(self,n,f):
88    parts = f.split(';')
89    matched = True
90    for part in parts:
91      k,v = part.split('=',1)
92      if(n.get(k,'') != v):
93        matched = False
94    return(matched)
95   
96  def storeNode(self, n):
97    for f in self.filters:
98      if(self.passesFilter(n,f['filter'])):
99                                x = poi(n['lat'], n['lon'])
100                                x.title = n.get('amenity','') + ': ' + n.get('name', '?')
101                                #print "%s matches %s" % (x.title, f['name'])
102                                f['group'].items.append(x)
103
104  def save(self):
105    # Default filename if none was loaded
106    if(self.filename == None):
107      self.filename = os.path.join(os.path.dirname(__file__),
108                            "data", "poi.osm")
109
110    self.saveAs(self.filename)
111 
112  def saveAs(self,filename):
113    if(filename == None):
114      return
115    pass
116 
117
118if __name__ == "__main__":
119  nodes = osmPoiModule(None)
120  nodes.sort({'valid':True,'lat':51.3,'lon':-0.2})
121  #nodes.report()
Note: See TracBrowser for help on using the repository browser.