1+ #include "Python.h"
2+ #include <wchar.h>
3+ #include "mapcoder.c"
4+
5+ PyObject * mapcode_module ;
6+
7+ /*
8+ mapcode.version method.
9+
10+ Returns the version of the C library used.
11+ */
12+
13+ static PyObject * version (PyObject * self , PyObject * args )
14+ {
15+ return Py_BuildValue ("s" , mapcode_cversion );
16+ }
17+
18+
19+ int isvalid (char * mapcode , int includes_territory )
20+ {
21+ if (compareWithMapcodeFormat (mapcode , includes_territory ? 1 : 0 ) == 0 ) {
22+ return 1 ;
23+ } else {
24+ return 0 ;
25+ }
26+ }
27+
28+
29+ char encode_result [MAX_NR_OF_MAPCODE_RESULTS * 2 ];
30+ static char * * encode (double latitude , double longitude , char * territory , int extra_digits )
31+ {
32+ int territorycode = 0 ;
33+ char * * s = (char * * ) encode_result ;
34+
35+ if (territory ) {
36+ territorycode = convertTerritoryIsoNameToCode (territory , 0 );
37+ /*
38+ printf("debug1: encode: territorystring: %s, code: %d\n", territory, territorycode);
39+ */
40+ if (territorycode < 0 ) {
41+ /* terminate array pointer so caller can detect it's empty */
42+ s [0 ] = 0 ;
43+ return s ;
44+ }
45+ }
46+
47+ /* printf("debug2: encode: territorystring: %s, code: %d\n", territory, territorycode);
48+ */
49+ int n = encodeLatLonToMapcodes ((char * * ) & encode_result , latitude , longitude , territorycode , extra_digits );
50+ if (n > 0 ) {
51+ /* for (int i = 0; i < n; ++i) {
52+ printf("debug: %s - %s\n", s[i*2], s[(i*2)+1]);
53+ }
54+ printf("debug: count = %d\n", i);
55+ */
56+ /* terminate array pointer at the end */
57+ s [n * 2 ] = 0 ;
58+
59+ return s ;
60+ } else {
61+ /* terminate array pointer at beginning */
62+ s [0 ] = 0 ;
63+ }
64+ return s ;
65+ }
66+
67+ /*
68+
69+ static char *encode_single(double latitude, double longitude, char *territory, int extra_digits)
70+ */
71+
72+ static char encode_single_result [MAX_NR_OF_MAPCODE_RESULTS ];
73+
74+ static PyObject * encode_single (PyObject * self , PyObject * args )
75+ {
76+ double latitude , longitude ;
77+ char * territory ;
78+ int extra_digits , territorycode = 0 ;
79+
80+ if (territory ) {
81+ territorycode = convertTerritoryIsoNameToCode (territory , 0 );
82+ if (territorycode < 0 )
83+ return NULL ;
84+ }
85+
86+ printf ("debug: encode_single: territorystring: %s, code: %d\n" , territory , territorycode );
87+ if (encodeLatLonToSingleMapcode (encode_single_result , latitude , longitude , territorycode , extra_digits ) > 0 ) {
88+ return Py_BuildValue ("s" , encode_single_result );
89+ } else
90+ return NULL ;
91+ }
92+
93+
94+ static int decode (double * latitude , double * longitude , const char * mapcode , char * territory )
95+ {
96+ int territorycode = 0 ;
97+
98+ if (territory ) {
99+ territorycode = convertTerritoryIsoNameToCode (territory , 0 );
100+ if (territorycode < 0 ) {
101+ * latitude = 0 ;
102+ * longitude = 0 ;
103+ return 1 ;
104+ }
105+ }
106+ return decodeMapcodeToLatLon (latitude , longitude , mapcode , territorycode );
107+ }
108+
109+
110+ /*
111+
112+ The methods we have
113+
114+ */
115+
116+ static PyMethodDef mapcode_methods [] = {
117+ { "version" , version , METH_VARARGS , "Returns the version of the Mapcode C library" },
118+ // { "encode", encode, METH_VARARGS, NULL},
119+ { (char * )"encode_single" , encode_single , METH_VARARGS , NULL },
120+ // { (char *)"decode", _wrap_decode, METH_VARARGS, NULL},
121+ // { (char *)"isvalid", _wrap_isvalid, METH_VARARGS, NULL},
122+ // { (char *)"alphabet", _wrap_alphabet, METH_VARARGS, NULL},
123+ { NULL , NULL , 0 , NULL }
124+ };
125+
126+
127+ /*
128+
129+ Initialisation that gets called when module is imported.
130+
131+ */
132+
133+ PyMODINIT_FUNC initmapcode (void )
134+ {
135+ mapcode_module = Py_InitModule ("mapcode" , mapcode_methods );
136+ }
0 commit comments