source: subversion/applications/routing/pyroute/poi_osm.py @ 8025

Last change on this file since 8025 was 6001, checked in by ojw, 12 years ago

move OSM data into module

File size: 3.4 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 = "data/poi_%s.osm" % name
34    url = "http://www.informationfreeway.org/api/0.5/node[%s][%s]" % (search, self.bbox())
35   
36    if(not os.path.exists(filename)):
37      print "Downloading POIs from OSM"
38      urllib.urlretrieve(url, filename)
39    self.load(filename, "Setup/poi.txt")
40
41  def bbox(self):
42    # TODO: based on location!
43    return "bbox=-6,48,2.5,61"
44 
45  def load(self, filename, listfile):
46    self.filters = []
47    print "Loading POIs from %s" % listfile
48    f = open(listfile,"r")
49    try:
50      for line in f:
51        if(len(line) > 1):
52          text = line.rstrip()
53          name, filter = text.split('|')
54          group = poiGroup(name)
55          self.groups.append(group)
56          self.filters.append({'name':name,'filter':filter,'group':group})
57    finally:
58      f.close()
59   
60    if(not os.path.exists(filename)):
61      print "Can't load %s"%filename
62      return
63    self.inNode = False
64    parser = make_parser()
65    parser.setContentHandler(self)
66    parser.parse(filename)
67   
68 
69  def startElement(self, name, attrs):
70    if name == "node":
71      self.currentNode = { \
72        'lat': float(attrs.get('lat')),
73        'lon': float(attrs.get('lon'))}
74      self.inNode = True
75    if name == "tag" and self.inNode:
76      self.currentNode[attrs.get('k')] = attrs.get('v')
77 
78  def endElement(self, name):
79    if(name == "node"):
80      self.storeNode(self.currentNode)
81      self.inNode = False
82
83  def passesFilter(self,n,f):
84    parts = f.split(';')
85    matched = True
86    for part in parts:
87      k,v = part.split('=',1)
88      if(n.get(k,'') != v):
89        matched = False
90    return(matched)
91   
92  def storeNode(self, n):
93    for f in self.filters:
94      if(self.passesFilter(n,f['filter'])):
95                                x = poi(n['lat'], n['lon'])
96                                x.title = n.get('amenity','') + ': ' + n.get('name', '?')
97                                #print "%s matches %s" % (x.title, f['name'])
98                                f['group'].items.append(x)
99
100  def save(self):
101    # Default filename if none was loaded
102    if(self.filename == None):
103      self.filename = "data/poi.osm"
104    self.saveAs(self.filename)
105 
106  def saveAs(self,filename):
107    if(filename == None):
108      return
109    pass
110 
111
112if __name__ == "__main__":
113  nodes = osmPoiModule(None)
114  nodes.sort({'valid':True,'lat':51.3,'lon':-0.2})
115  #nodes.report()
Note: See TracBrowser for help on using the repository browser.