summaryrefslogtreecommitdiff
path: root/chimere/route.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@peacefrogs.net>2012-08-15 01:38:28 +0200
committerÉtienne Loks <etienne.loks@peacefrogs.net>2012-08-22 02:03:18 +0200
commit1feeb42a1f12811106764ef8203bfe1a10362fb8 (patch)
treeb275800b96119265c55cc159f3a7fe41516b7ff7 /chimere/route.py
parentd63c56a5f8350c61117efec402b3ae67cc4c5a9b (diff)
downloadChimère-1feeb42a1f12811106764ef8203bfe1a10362fb8.tar.bz2
Chimère-1feeb42a1f12811106764ef8203bfe1a10362fb8.zip
First working version of routing.
* add an utilitary Routing class to manage different routing system * implement the routing with routino * add a view to manage routes * itinerary panel template * JS management of routing: * manage flag markers on the map * request the route when start and finish flag are set * display the route and the itinerary description * add of two flags images * itinerary panel CSS * french translation update
Diffstat (limited to 'chimere/route.py')
-rw-r--r--chimere/route.py87
1 files changed, 87 insertions, 0 deletions
diff --git a/chimere/route.py b/chimere/route.py
new file mode 100644
index 0000000..efc6763
--- /dev/null
+++ b/chimere/route.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Copyright (C) 2012 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet>
+#
+
+# 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 <http://www.gnu.org/licenses/>.
+
+# See the file COPYING for details.
+
+"""
+Routing management
+"""
+
+import os, re, shutil, tempfile
+from BeautifulSoup import BeautifulSoup
+from subprocess import Popen, PIPE
+from django.contrib.gis.gdal import DataSource
+
+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("<tr class='n'>"), re.compile("<tr class='s'>"),
+ re.compile("<tr class='t'>")]
+ def route(self, lon1, lat1, lon2, lat2, session_id='', transport='foot'):
+ '''
+ 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,
+ "--lat2=%0.15f" % lat2,
+ "--lon2=%0.15f" % lon2
+ ]
+ tmp_dir = tempfile.mkdtemp(prefix='chimere_') + os.sep
+ p = Popen(args, stdout=PIPE, cwd=tmp_dir)
+ p.communicate()
+ ds = DataSource(tmp_dir + 'shortest-track.gpx')
+ if not ds:
+ return [], 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 = []
+ for multiline in multilines:
+ res += [geom.geojson for geom in multiline]
+ desc = ['<table>']
+ # 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())
+ desc.append('</table>')
+ desc = BeautifulSoup('\n'.join(desc)).prettify()
+ shutil.rmtree(tmp_dir)
+ return res, desc
+router = None
+if settings.CHIMERE_ROUTING_ENGINE['ENGINE'] == 'routino':
+ router = RoutinoRouter()
+