From dc6d5153f6fb6f72242ff47db4cdb4790a4b6ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20=27Natsuysaumi=27=20Kuranowski?= Date: Sat, 30 Sep 2017 01:37:24 +0200 Subject: [PATCH] Offline routing --- README.md | 14 +++++++++++++- pyroutelib3/__init__.py | 17 +++++++++++------ setup.py | 4 ++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3fd49d8..8803c1e 100644 --- a/README.md +++ b/README.md @@ -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("", "") +# 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 diff --git a/pyroutelib3/__init__.py b/pyroutelib3/__init__.py index 8be056e..3335742 100644 --- a/pyroutelib3/__init__.py +++ b/pyroutelib3/__init__.py @@ -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 @@ -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__ = "mkuranowski@gmail.com" 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.""" @@ -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) @@ -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""" diff --git a/setup.py b/setup.py index 49f5862..f617a19 100644 --- a/setup.py +++ b/setup.py @@ -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 = "mkuranowski@gmail.com", 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"]