Routino SVN Repository Browser

Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino

ViewVC logotype

Contents of /trunk/python/src/router.i

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1962 - (show annotations) (download)
Fri Oct 26 18:39:08 2018 UTC (6 years, 4 months ago) by amb
File size: 3568 byte(s)
Add a Python3 module that can access the Routino database and
calculate routes.  Not compiled by default at the moment.

1 /***************************************
2 Python router interface definition.
3
4 Part of the Routino routing software.
5 ******************/ /******************
6 This file Copyright 2018 Andrew M. Bishop
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***************************************/
21
22
23 /* Name the module 'router' in the 'routino' package */
24
25 %module(package="routino.router") router
26
27
28 /* Include the 'routino.h' header file from the library in the auto-generated code */
29
30 %{
31 #include "routino.h"
32 %}
33
34
35 /* Return NULL-terminated arrays of strings as a list of strings */
36
37 %typemap(ret) char** {
38 $result = PyList_New(0);
39
40 char **p=$1;
41
42 while(*p)
43 {
44 PyList_Append($result, PyString_FromString(*p));
45 p++;
46 }
47 }
48
49
50 /* Handle lists of Routino Waypoints as an array */
51
52 %typemap(in) Routino_Waypoint ** {
53 /* Check if is a list */
54 if (PyList_Check($input))
55 {
56 int size = PyList_Size($input);
57 int i = 0;
58 $1 = (Routino_Waypoint **) malloc(size*sizeof(Routino_Waypoint *));
59 for (i = 0; i < size; i++)
60 if (!SWIG_IsOK(SWIG_ConvertPtr(PyList_GetItem($input, i), (void **) &$1[i], $descriptor(Routino_Waypoint*), 0)))
61 SWIG_exception_fail(SWIG_TypeError, "in method '$symname', expecting type Routino_Waypoint");
62 } else {
63 PyErr_SetString(PyExc_TypeError, "not a list");
64 SWIG_fail;
65 }
66 }
67
68 %typemap(freearg) Routino_Waypoint ** {
69 free((Routino_Waypoint *) $1);
70 }
71
72
73 /* Rename variables and functions by stripping 'Routino_' or 'ROUTINO_' prefixes */
74
75 %rename("%(regex:/R[Oo][Uu][Tt][Ii][Nn][Oo]_(.*)/\\1/)s") "";
76
77 /* Rename the Routino_CalculateRoute() function so we can replace with a Python wrapper */
78
79 %rename("_CalculateRoute") "Routino_CalculateRoute";
80
81 /* Rename the Routino_LoadDatabase() function so we can replace with a Python wrapper */
82
83 %rename("_LoadDatabase") "Routino_LoadDatabase";
84
85
86 /* Add some custom Python code to the module */
87
88 %pythoncode %{
89
90 # Set up a replacement function for a macro in the original
91
92 def CheckAPIVersion():
93 return _router.Check_API_Version(_router.API_VERSION)
94
95 # Set up a replacement function so that we do not need to pass the size of the list
96
97 def CalculateRoute(database, profile, translation, waypoints, options, progress=None):
98 return _router._CalculateRoute(database, profile, translation, waypoints, len(waypoints), options, progress)
99
100 # Set up a replacement function to make the second argument optional
101
102 def LoadDatabase(dirname, prefix=None):
103 return _router._LoadDatabase(dirname, prefix)
104
105 # Create a function for concatenating directory names, prefixes and filenames
106
107 def FileName(dirname, prefix, name):
108
109 filename=""
110
111 if dirname is not None:
112 filename=dirname + "/"
113
114 if prefix is not None:
115 filename += prefix + "-"
116
117 filename += name
118
119 return filename
120 %}
121
122
123 /* Use the 'routino.h' header file from the library to generate the wrapper (everything is read-only) */
124
125 %immutable;
126
127 %include "../src/routino.h"