source: subversion/applications/routing/pyroute/loadOsm.py @ 5730

Last change on this file since 5730 was 5671, checked in by ojw, 12 years ago

download on request

File size: 7.5 KB
Line 
1#!/usr/bin/python
2#----------------------------------------------------------------
3# load OSM data file into memory
4#
5#------------------------------------------------------
6# Usage:
7#   data = LoadOsm(filename)
8# or:
9#   loadOsm.py filename.osm
10#------------------------------------------------------
11# Copyright 2007, Oliver White
12#
13# This program is free software: you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation, either version 3 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program.  If not, see <http://www.gnu.org/licenses/>.
25#------------------------------------------------------
26# Changelog:
27#  2007-11-04  OJW  Modified from pyroute.py
28#  2007-11-05  OJW  Multiple forms of transport
29#------------------------------------------------------
30import sys
31import os
32from xml.sax import make_parser, handler
33import urllib
34from time import clock
35
36execfile("weights.py")
37
38class LoadOsm(handler.ContentHandler):
39  """Parse an OSM file looking for routing information, and do routing with it"""
40  def __init__(self, filename, storeMap = 0):
41    """Initialise an OSM-file parser"""
42    self.routing = {}
43    self.routeTypes = ('cycle','car','train','foot','horse')
44    self.routeableNodes = {}
45    for routeType in self.routeTypes:
46      self.routing[routeType] = {}
47      self.routeableNodes[routeType] = {}
48    self.nodes = {}
49    self.ways = []
50    self.storeMap = storeMap
51   
52    if(filename == None):
53      return
54    self.loadfile(filename)
55   
56  def loadfile(self, filename):
57    parser = make_parser()
58    parser.setContentHandler(self)
59    parser.parse(filename)
60 
61  def download(self,lat,lon,size):
62    url = "http://osm.test.le.ac.uk/api/0.5/way[%1.4f,%1.4f,%1.4f,%1.4f][*=*]" % (lon-size,lat-size,lon+size,lat+size)
63    url = "http://api.openstreetmap.org/api/0.5/map?bbox=%1.4f,%1.4f,%1.4f,%1.4f" % (lon-size,lat-size,lon+size,lat+size)
64    print "downloading %s" % url
65    start = clock()
66    filename = "data/routing.osm"
67    urllib.urlretrieve(url, filename)
68    end = clock()
69    delay = end - start
70    print "finished downloading, took %1.1f s" % (delay)
71    self.loadfile(filename)
72  def report(self):
73    """Display some info about the loaded data"""
74    report = "Loaded %d nodes,\n" % len(self.nodes.keys())
75    report = report + "%d ways, and...\n" % len(self.ways)
76    for routeType in self.routeTypes:
77      report = report + " %d %s routes\n" % ( \
78        len(self.routing[routeType].keys()),
79        routeType)
80    return(report)
81
82  def startElement(self, name, attrs):
83    """Handle XML elements"""
84    if name in('node','way','relation'):
85      self.tags = {}
86      self.waynodes = []
87      if name == 'node':
88        """Nodes need to be stored"""
89        id = int(attrs.get('id'))
90        lat = float(attrs.get('lat'))
91        lon = float(attrs.get('lon'))
92        self.nodes[id] = (lat,lon)
93    elif name == 'nd':
94      """Nodes within a way -- add them to a list"""
95      self.waynodes.append(int(attrs.get('ref')))
96    elif name == 'tag':
97      """Tags - store them in a hash"""
98      k,v = (attrs.get('k'), attrs.get('v'))
99      if not k in ('created_by'):
100        self.tags[k] = v
101 
102  def endElement(self, name):
103    """Handle ways in the OSM data"""
104    if name == 'way':
105      highway = self.equivalent(self.tags.get('highway', ''))
106      railway = self.equivalent(self.tags.get('railway', ''))
107      oneway = self.tags.get('oneway', '')
108      reversible = not oneway in('yes','true','1')
109   
110      # Calculate what vehicles can use this route
111      access = {}
112      access['cycle'] = highway in ('primary','secondary','tertiary','unclassified','minor','cycleway','residential', 'track','service')
113      access['car'] = highway in ('motorway','trunk','primary','secondary','tertiary','unclassified','minor','residential', 'service')
114      access['train'] = railway in('rail','light_rail','subway')
115      access['foot'] = access['cycle'] or highway in('footway','steps')
116      access['horse'] = highway in ('track','unclassified','bridleway')
117     
118      # Store routing information
119      last = -1
120      for i in self.waynodes:
121        if last != -1:
122          #print "%d -> %d & v.v." % (last, i)
123          for routeType in self.routeTypes:
124            if(access[routeType]):
125              weight = getWeight(routeType, highway)
126              self.addLink(last, i, routeType, weight)
127              if reversible or routeType == 'foot':
128                self.addLink(i, last, routeType, weight)
129        last = i
130     
131      # Store map information
132      if(self.storeMap):
133        wayType = self.WayType(self.tags)
134        if(wayType):
135          self.ways.append({ \
136            't':wayType,
137            'n':self.waynodes})
138 
139  def addLink(self,fr,to, routeType, weight=1):
140    """Add a routeable edge to the scenario"""
141    self.routeablefrom(fr,routeType)
142    try:
143      if to in self.routing[routeType][fr].keys():
144        return
145      self.routing[routeType][fr][to] = weight
146    except KeyError:
147      self.routing[routeType][fr] = {to: weight}
148
149  def WayType(self, tags):
150    # Look for a variety of tags (priority order - first one found is used)
151    for key in ('highway','railway','waterway','natural'):
152      value = tags.get('highway', '')
153      if value:
154        return(self.equivalent(value))
155    return('')
156  def equivalent(self,tag):
157    """Simplifies a bunch of tags to nearly-equivalent ones"""
158    equivalent = { \
159      "primary_link":"primary",
160      "trunk":"primary",
161      "trunk_link":"primary",
162      "secondary_link":"secondary",
163      "tertiary":"secondary",
164      "tertiary_link":"secondary",
165      "residential":"unclassified",
166      "minor":"unclassified",
167      "steps":"footway",
168      "driveway":"service",
169      "pedestrian":"footway",
170      "bridleway":"cycleway",
171      "track":"cycleway",
172      "arcade":"footway",
173      "canal":"river",
174      "riverbank":"river",
175      "lake":"river",
176      "light_rail":"railway"
177      }
178    try:
179      return(equivalent[tag])
180    except KeyError:
181      return(tag)
182   
183  def routeablefrom(self,fr,routeType):
184    self.routeableNodes[routeType][fr] = 1
185 
186  def save(self, filename):
187    """Saves routing data (to cache)"""
188    toStore = { \
189      'routing': self.routing,
190      'routeNodes':self.routeableNodes,
191      'nodes':self.nodes,
192      'ways':self.ways}
193    file = open(filename, "w")
194    file.write(str(toStore))
195    file.close()
196
197  def load(self, filename):
198    """Loads routing data (from cache)"""
199    file = open(filename, "r")
200    struct = eval(file.read())
201    self.routing = struct['routing']
202    self.routeableNodes = struct['routeNodes']
203    self.nodes = struct['nodes']
204    self.ways = struct['ways']
205    file.close()
206    print "done"
207   
208  def findNode(self,lat,lon,routeType):
209    """Find the nearest node to a point.
210    Filters for nodes which have a route leading from them"""
211    maxDist = 1000
212    nodeFound = None
213    for id in self.routeableNodes[routeType].keys():
214      n = self.nodes[id]
215      dlat = n[0] - lat
216      dlon = n[1] - lon
217      dist = dlat * dlat + dlon * dlon
218      if(dist < maxDist):
219        maxDist = dist
220        nodeFound = id
221    return(nodeFound)
222   
223# Parse the supplied OSM file
224if __name__ == "__main__":
225  print "Loading data..."
226  data = LoadOsm(sys.argv[1], True)
227  data.report()
Note: See TracBrowser for help on using the repository browser.