-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmapcodemodule.c
More file actions
174 lines (149 loc) · 5.3 KB
/
mapcodemodule.c
File metadata and controls
174 lines (149 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (C) 2015 Stichting Mapcode Foundation (http://www.mapcode.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "Python.h"
#include "mapcoder.h"
#include "mapcode_legacy.h"
#include <math.h>
static char version_doc[] =
"version() -> string\n\
\n\
Returns the version of the Mapcode C library used by this module.\n";
static PyObject *version(PyObject *self, PyObject *args)
{
return Py_BuildValue("s", MAPCODE_C_VERSION);
}
static char isvalid_doc[] =
"isvalid(mapcode) -> bool\n\
\n\
Verify if the provided mapcode has the correct syntax.\n";
static PyObject *isvalid(PyObject *self, PyObject *args)
{
char *mapcode;
int includes_territory = 0;
if (!PyArg_ParseTuple(args, "s|i", &mapcode, &includes_territory))
return NULL;
if (compareWithMapcodeFormatUtf8(mapcode) == 0) {
return Py_True;
} else {
return Py_False;
}
}
static char decode_doc[] =
"decode(mapcode, (territoryname)) -> (float, float)\n\
\n\
Decodes the provided string to latitude and longitude. Optionally\n\
a territory context can be provided to disambiguate the mapcode.\n\
\n\
Returns (nan, nan) when decoding failed.\n";
static PyObject *decode(PyObject *self, PyObject *args)
{
char *mapcode, *territoryname = NULL;
double latitude, longitude;
int territorycode;
if (!PyArg_ParseTuple(args, "s|s", &mapcode, &territoryname))
return NULL;
if (territoryname) {
territorycode = getTerritoryCode(territoryname, 0);
if (territorycode < 0) {
latitude = NAN;
longitude = NAN;
return Py_BuildValue("ff", latitude, longitude);
}
} else
territorycode = 0;
if (decodeMapcodeToLatLonUtf8(&latitude, &longitude, mapcode, territorycode, NULL)) {
/* return nan,nan as error values */
latitude = NAN;
longitude = NAN;
}
return Py_BuildValue("ff", latitude, longitude);
}
static char encode_doc[] =
"encode(latitude, longitude, (territoryname, (extra_digits))) -> [(string, string)] \n\
\n\
Encodes the given latitude, longitude to one or more mapcodes.\n\
Returns a list of tuples that contain a mapcode and territory context.\n\
\n\
Optionally a territory context can be provided to generate a mapcode\n\
in particular territory context.\n\
Territory context AAA is a special case, which means Earth\n\
(for international mapcodes).\n";
static PyObject *encode(PyObject *self, PyObject *args)
{
double latitude, longitude;
char *territoryname = NULL;
int extra_digits = 0, territorycode = 0;
PyObject *result;
if (!PyArg_ParseTuple(args, "dd|zi", &latitude, &longitude, &territoryname, &extra_digits))
return NULL;
if (territoryname) {
territorycode = getTerritoryCode(territoryname, 0);
if (territorycode < 0) {
return PyList_New(0);
}
}
char *mapcode_results[2 * MAX_NR_OF_MAPCODE_RESULTS];
int n = encodeLatLonToMapcodes_Deprecated(mapcode_results, latitude, longitude, territorycode, extra_digits);
if (n > 0) {
result = PyList_New(n);
while (n--) {
// printf("debug: %d: %s - %s\n", n, mapcode_results[n * 2], mapcode_results[(n * 2) + 1]);
PyList_SetItem(result, n, Py_BuildValue("(ss)",
(mapcode_results[n * 2]), mapcode_results[(n * 2) + 1]));
}
return result;
}
return PyList_New(0);
}
static char mapcode_doc[] =
"Mapcode support library (see http://www.mapcode.com).\n\
\n\
This module exports the following functions:\n\
version Returns the version of the Mapcode C library.\n\
isvalid Verifies if the provided mapcode has the correct syntax.\n\
decode Decodes a mapcode to latitude and longitude.\n\
encode Encodes latitude and longitude to one or more mapcodes.\n";
/* The methods we expose in Python. */
static PyMethodDef mapcode_methods[] = {
{ "version", version, METH_VARARGS, version_doc },
{ "isvalid", isvalid, METH_VARARGS, isvalid_doc },
{ "decode", decode, METH_VARARGS, decode_doc },
{ "encode", encode, METH_VARARGS, encode_doc },
{ NULL, NULL, 0, NULL }
};
/* Initialisation that gets called when module is imported. */
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_mapcode(void)
{
static struct PyModuleDef mapcode_module = {
PyModuleDef_HEAD_INIT,
"mapcode", /* m_name */
mapcode_doc, /* m_doc */
-1, /* m_size */
mapcode_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
return(PyModule_Create(&mapcode_module));
}
#else
PyMODINIT_FUNC initmapcode(void)
{
Py_InitModule3("mapcode", mapcode_methods, mapcode_doc);
}
#endif