Skip to content

Commit

Permalink
Offline routing
Browse files Browse the repository at this point in the history
  • Loading branch information
MKuranowski committed Sep 29, 2017
1 parent 7380a19 commit dc6d515
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ if status == 'success':
**Statuses**: success, no_route, no_such_node, gave_up


##### Offline Routing

If you want to use pyroutelib3 offline or on custom .osm file, you just need to add a second argument to Router:
Path to the specific osm file,

```python
from pyroutelib3 import Router
router = Router("<transport mode>", "<path-to-.osm-file>")
# Continue on doing like in the example above
```


## Todo
- [x] Porting to python3
- [x] Making pyroutelib a package
- [ ] Custom transport types
- [ ] Handling the access key
- [ ] Turn restrictions
- [ ] Offline routers (load only local osm file)
- [x] Offline routers (load only local osm file)


## License
Expand Down
17 changes: 11 additions & 6 deletions pyroutelib3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# 2007-11-04 OJW Modified from pyroute.py
# 2007-11-05 OJW Multiple forms of transport
# 2017-09-24 MK Code cleanup
# 2017-09-30 MK LocalFile - Only router
#----------------------------------------------------------------------------
import os
import re
Expand All @@ -44,22 +45,26 @@
__copyright__ = "Copyright 2007, Oliver White; Modifications: Copyright 2017, Mikolaj Kuranowski"
__credits__ = ["Oliver White", "Mikolaj Kuranowski"]
__license__ = "GPL v3"
__version__ = "0.2"
__version__ = "0.3"
__maintainer__ = "Mikolaj Kuranowski"
__email__ = "[email protected]"


class Datastore(object):
"""Parse an OSM file looking for routing information"""
def __init__(self, transport):
def __init__(self, transport, localfile=""):
"""Initialise an OSM-file parser"""
self.routing = {}
self.rnodes = {}
self.transport = transport
self.localFile = localfile
self.tiles = {}
self.weights = weights.RoutingWeights()
self.api = osmapi.OsmApi(api="api.openstreetmap.org")

if self.localFile:
self.loadOsm(self.localFile)

def getArea(self, lat, lon):
"""Download data in the vicinity of a lat/long.
Return filename to existing or newly downloaded .osm file."""
Expand All @@ -68,9 +73,9 @@ def getArea(self, lat, lon):
(x,y) = tilenames.tileXY(lat, lon, z)

tileID = '%d,%d'%(x,y)
if(self.tiles.get(tileID,False)):
#print "Already got %s" % tileID
if self.tiles.get(tileID,False) or self.localFile:
return

self.tiles[tileID] = True

filename = tiledata.GetOsmTileData(z,x,y)
Expand Down Expand Up @@ -277,8 +282,8 @@ def report(self):
print("Loaded %d %s routes" % (len(list(self.routing.keys())), self.transport))

class Router(object):
def __init__(self, transport):
self.data = Datastore(transport)
def __init__(self, transport, localfile=""):
self.data = Datastore(transport, localfile)

def distance(self,n1,n2):
"""Calculate distance between two nodes"""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
name = "pyroutelib3",
packages = ["pyroutelib3"],
license = "GPL v3",
version = "0.2",
version = "0.3",
description = "Library for simple routing on OSM data",
long_description = readme,
author = "Oliver White",
maintainer = "Mikolaj Kuranowski",
maintainer_email = "[email protected]",
url = "https://github.com/MKuranowski/pyroutelib3",
download_url = "https://github.com/MKuranowski/pyroutelib3/archive/0.1.tar.gz",
download_url = "https://github.com/MKuranowski/pyroutelib3/archive/0.3.tar.gz",
keywords = "osm routing pyroutelib",
classifiers = [],
install_requires = ["osmapi"]
Expand Down

0 comments on commit dc6d515

Please sign in to comment.