#!/usr/bin/env python # -*- coding: utf-8 -*- # Copyright (C) 2012 Étienne Loks # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # See the file COPYING for details. """ Routing management """ import os import re import shutil import tempfile import json from bs4 import BeautifulSoup from subprocess import Popen, PIPE from django.contrib.gis.gdal import DataSource from django.contrib.gis.gdal.error import GDALException from django.conf import settings class Router: def route(self, lon1, lat1, lon2, lat2, transport='foot'): ''' Get a list of geojson polylines ''' return [] class RoutinoRouter(Router): re_desc = [re.compile(""), re.compile(""), re.compile("")] def route(self, lon1, lat1, lon2, lat2, steps=[], transport='foot', speed=None): ''' Get a list of geojson polylines and route description ''' language = settings.LANGUAGE_CODE.split('-')[0] args = [settings.CHIMERE_ROUTING_ENGINE['PATH'], "--dir=%s" % settings.CHIMERE_ROUTING_ENGINE['DB_PATH'], "--transport=%s" % transport, "--language=%s" % language, "--shortest", "--output-html", "--output-gpx-track", "--lat1=%0.15f" % lat1, "--lon1=%0.15f" % lon1, ] if speed: args += [ "--speed-%s=%s" % (highway, str(speed)) for highway in ( 'motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'unclassified', 'residential', 'service', 'track', 'cycleway', 'path', 'steps')] lonlat_index = 1 for lon, lat in steps: lonlat_index += 1 args += ["--lat%d=%0.15f" % (lonlat_index, lat), "--lon%d=%0.15f" % (lonlat_index, lon)] lonlat_index += 1 args += ["--lat%d=%0.15f" % (lonlat_index, lat2), "--lon%d=%0.15f" % (lonlat_index, lon2)] tmp_dir = tempfile.mkdtemp(prefix='chimere_') + os.sep p = Popen(args, stdout=PIPE, cwd=tmp_dir) p.communicate() try: ds = DataSource(tmp_dir + 'shortest-track.gpx') except GDALException: return [], None, None if not ds: return [], None, None layer = ds[0] trk_layer = None for layer in ds: if layer.name == 'tracks': trk_layer = layer break multilines = trk_layer.get_geoms() res = [] jsonencoder = json.JSONEncoder() jsondecoder = json.JSONDecoder() for multiline in multilines: res += [ jsonencoder.encode( {"type": "Feature", "geometry": jsondecoder.decode(geom.geojson)}) for geom in multiline] desc = [] # only keeping interessant lines of the desc for line in open(tmp_dir + 'shortest.html').readlines(): if [True for r in self.re_desc if r.match(line)]: desc.append(BeautifulSoup(line).prettify()) total = self.webify(desc[-1]) desc = desc[1:-2] # very fragile piece of code but only break the numerotation number_tpl = '%d.' new_desc = [] for idx, d in enumerate(desc): for tag in ("", "", "", ""): d = re.sub(tag, "", d) if idx % 2: new_desc.append( re.sub('', number_tpl % (int(idx / 2) + 1), d)) else: new_desc.append(d) desc = new_desc desc = self.webify(BeautifulSoup('\n'.join(desc)).prettify()) desc = re.sub(" \[", "", desc) desc = re.sub(" \]", "", desc) shutil.rmtree(tmp_dir) return res, desc, total @staticmethod def webify(lbl): lbl = re.sub("", "", lbl) lbl = re.sub("", "", lbl) lbl = re.sub("