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