]> git.street.me.uk Git - andy/viking.git/blame - src/osrm.c
Add OSRM routing engine
[andy/viking.git] / src / osrm.c
CommitLineData
5ae894dc
GB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (c) 2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22/**
23 * SECTION:osrm
24 * @short_description: the class for OSRM
25 *
26 * The #OsrmRouting class handles OSRM
27 * service as routing engine.
28 *
29 * Technical details are available here:
30 * http://project-osrm.org/
31 */
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
37#include <string.h>
38
39#include <glib.h>
40#include <glib/gstdio.h>
41
42#include "osrm.h"
43
44/* See API references: https://github.com/DennisOSRM/Project-OSRM/wiki/Server-api */
45#define OSRM_DIRECTIONS_STRING "router.project-osrm.org/viaroute?loc=%s,%s&loc=%s,%s&output=gpx"
46
47static DownloadMapOptions osrms_routing_options = { FALSE, FALSE, "http://map.project-osrm.org/", 0, NULL };
48
49static void osrm_routing_finalize ( GObject *gob );
50
51static gchar *osrm_routing_get_url_for_coords ( VikRoutingEngine *self, struct LatLon start, struct LatLon end );
52static DownloadMapOptions *osrm_routing_get_download_options ( VikRoutingEngine *self );
53
54G_DEFINE_TYPE (OsrmRouting, osrm_routing, VIK_ROUTING_ENGINE_TYPE)
55
56static void osrm_routing_class_init ( OsrmRoutingClass *klass )
57{
58 GObjectClass *object_class;
59 VikRoutingEngineClass *parent_class;
60
61 object_class = G_OBJECT_CLASS (klass);
62
63 object_class->finalize = osrm_routing_finalize;
64
65 parent_class = VIK_ROUTING_ENGINE_CLASS (klass);
66
67 parent_class->get_url_for_coords = osrm_routing_get_url_for_coords;
68 parent_class->get_download_options = osrm_routing_get_download_options;
69}
70
71OsrmRouting *osrm_routing_new ()
72{
73 return OSRM_ROUTING ( g_object_new ( OSRM_ROUTING_TYPE,
74 "id", "osrm",
75 "label", "OSRM",
76 "format", "gpx",
77 NULL ) );
78}
79
80static void osrm_routing_init ( OsrmRouting *vlp )
81{
82}
83
84static void osrm_routing_finalize ( GObject *gob )
85{
86 G_OBJECT_GET_CLASS(gob)->finalize(gob);
87}
88
89static DownloadMapOptions *
90osrm_routing_get_download_options ( VikRoutingEngine *self )
91{
92 return &osrms_routing_options;
93}
94
95gchar *
96osrm_routing_get_url_for_coords ( VikRoutingEngine *self, struct LatLon start, struct LatLon end )
97{
98 gchar startlat[G_ASCII_DTOSTR_BUF_SIZE], startlon[G_ASCII_DTOSTR_BUF_SIZE];
99 gchar endlat[G_ASCII_DTOSTR_BUF_SIZE], endlon[G_ASCII_DTOSTR_BUF_SIZE];
100 gchar *url;
101 url = g_strdup_printf(OSRM_DIRECTIONS_STRING,
102 g_ascii_dtostr (startlat, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) start.lat),
103 g_ascii_dtostr (startlon, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) start.lon),
104 g_ascii_dtostr (endlat, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) end.lat),
105 g_ascii_dtostr (endlon, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) end.lon));
106
107 return url;
108}