Skip to content

Commit

Permalink
Access tags
Browse files Browse the repository at this point in the history
  • Loading branch information
MKuranowski committed Oct 11, 2017
1 parent 98177cb commit cf5a83f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 52 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ end = router.data.findNode(lat, lon)
status, route = router.doRoute(start, end) # Find the route - a list of OSM nodes

if status == 'success':
routeLatLons = list(map(router.nodeLatLon, route)) # Get actual route coorinates
routeLatLons = list(map(router.nodeLatLon, route)) # Get actual route coordinates

```
**Transport Modes**: car, cycle, foot, horse, tram, train
Expand All @@ -41,8 +41,8 @@ router = Router("<transport mode>", "<path-to-.osm-file>")
## Todo
- [x] Porting to python3
- [x] Making pyroutelib a package
- [ ] Custom transport types
- [ ] Handling the access key
- [x] Custom transport types (todo documentation)
- [x] Handling the access key
- [ ] Turn restrictions
- [x] Offline routers (load only local osm file)

Expand Down
75 changes: 55 additions & 20 deletions pyroutelib3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import osmapi
import xml.etree.ElementTree as etree
from datetime import datetime
from . import (tiledata, tilenames, weights)
from . import (tiledata, tilenames)


__title__ = "pyroutelib3"
Expand All @@ -50,6 +50,37 @@
__email__ = "[email protected]"


TYPES = {
"car": {
"weights": {"motorway": 10, "trunk": 10, "primary": 2, "secondary": 1.5, "tertiary": 1,
"unclassified": 1, "minor": 1, "residential": 0.7, "track": 0.5, "service": 0.5},
"access": ["access", "vehicle", "motor_vehicle", "motorcar"]},
"bus": {
"weights": {"motorway": 10, "trunk": 10, "primary": 2, "secondary": 1.5, "tertiary": 1,
"unclassified": 1, "minor": 1, "residential": 0.8, "track": 0.3, "service": 0.9},
"access": ["access", "vehicle", "motor_vehicle", "psv", "bus"]},
"cycle": {
"weights": {"trunk": 0.05, "primary": 0.3, "secondary": 0.9, "tertiary": 1,
"unclassified": 1, "minor": 1, "cycleway": 2, "residential": 2.5, "track": 1,
"service": 1, "bridleway": 0.8, "footway": 0.8, "steps": 0.5, "path": 1},
"access": ["access", "vehicle", "bicycle"]},
"horse": {
"weights": {"primary": 0.05, "secondary": 0.15, "tertiary": 0.3, "unclassified": 1,
"minor": 1, "residential": 1, "track": 1.5, "service": 1, "bridleway": 5, "path": 1.5},
"access": ["access", "horse"]},
"foot": {
"weights": {"trunk": 0.3, "primary": 0.6, "secondary": 0.95, "tertiary": 1,
"unclassified": 1, "minor": 1, "residential": 1, "track": 1, "service": 1,
"bridleway": 1, "footway": 1.2, "path": 1.2, "steps": 1.15},
"access": ["access", "vehicle", "motor_vehicle", "motorcar"]},
"tram": {
"weights": {"tram": 1, "light_rail": 1},
"access": []},
"train": {
"weights": {"rail": 1, "light_rail": 1, "subway": 1, "narrow_guage": 1},
"access": []}
}

class Datastore(object):
"""Parse an OSM file looking for routing information"""
def __init__(self, transport, localfile=""):
Expand All @@ -59,8 +90,8 @@ def __init__(self, transport, localfile=""):
self.transport = transport
self.localFile = localfile
self.tiles = {}
self.weights = weights.RoutingWeights()
self.api = osmapi.OsmApi(api="api.openstreetmap.org")
self.types = TYPES

if self.localFile:
self.loadOsm(self.localFile)
Expand Down Expand Up @@ -93,6 +124,20 @@ def _ParseDate(self, DateString):
pass
return result

def _allowedVehicle(self, tags):
"Check way against access tags"

# Default to true
allowed = True

# Priority is ascending in the access array
for key in self.types[self.transport]["access"]:
if key in tags:
if tags[key] in ("no", "private"): allowed = False
else: allowed = True

return(allowed)

def getElementAttributes(self, element):
result = {}
for k, v in element.attrib.items():
Expand Down Expand Up @@ -188,32 +233,22 @@ def loadOsm(self, filename):
return(True)

def storeWay(self, wayID, tags, nodes):
highway = self.equivalent(tags.get('highway', ''))
railway = self.equivalent(tags.get('railway', ''))
tag = self.equivalent(tags.get('highway', '')) or self.equivalent(tags.get('railway', ''))
oneway = tags.get('oneway', '')

# Calculate what vehicles can use this route
# TODO: just use getWeight != 0
access = {}
access['cycle'] = highway in ('primary','secondary','tertiary','unclassified','minor','cycleway','residential', 'track','service')
access['car'] = highway in ('motorway','trunk','primary','secondary','tertiary','unclassified','minor','residential', 'service')
access['train'] = railway in ('rail','light_rail','subway')
access['tram'] = railway in ('tram')
access['foot'] = access['cycle'] or highway in('footway','steps')
access['horse'] = highway in ('track','unclassified','bridleway')
weight = self.types[self.transport]["weights"].get(tag, 0)

# Store routing information
last = [None,None,None]
# Check against access tags
if not self._allowedVehicle(tags): weight = 0

if(wayID == 41 and 0):
print(nodes)
sys.exit()
# Store routing information
last = [None, None, None]

for node in nodes:
(node_id,x,y) = node
(node_id, x, y) = node
if last[0]:
if(access[self.transport]):
weight = self.weights.get(self.transport, railway or highway)
if weight != 0:
if oneway not in ("-1"):
self.addLink(last[0], node_id, weight)
self.makeNodeRouteable(last)
Expand Down
29 changes: 0 additions & 29 deletions pyroutelib3/weights.py

This file was deleted.

0 comments on commit cf5a83f

Please sign in to comment.