1+ /*
2+ * Copyright (C) 2015 Stichting Mapcode Foundation (http://www.mapcode.com)
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
117#include "Python.h"
218#include "mapcoder.c"
319
4- PyObject * mapcode_module ;
5-
6-
7-
820
921static char version_doc [] =
1022 "version()\n\
@@ -17,8 +29,6 @@ static PyObject *version(PyObject *self, PyObject *args)
1729}
1830
1931
20-
21-
2232static char isvalid_doc [] =
2333 "isvalid()\n\
2434\n\
@@ -27,9 +37,9 @@ Verify if the provided string has the right mapcode syntax.\n";
2737static PyObject * isvalid (PyObject * self , PyObject * args )
2838{
2939 char * mapcode ;
30- int includes_territory ;
40+ int includes_territory = 0 ;
3141
32- if (!PyArg_ParseTuple (args , "si " , & mapcode , & includes_territory ))
42+ if (!PyArg_ParseTuple (args , "s|i " , & mapcode , & includes_territory ))
3343 return NULL ;
3444
3545 if (compareWithMapcodeFormat (mapcode , includes_territory ? 1 : 0 ) == 0 ) {
@@ -39,6 +49,7 @@ static PyObject *isvalid(PyObject *self, PyObject *args)
3949 }
4050}
4151
52+
4253static char decode_doc [] =
4354 "decode(mapcode, (territoryname))\n\
4455\n\
@@ -57,8 +68,6 @@ static PyObject *decode(PyObject *self, PyObject *args)
5768 if (territoryname ) {
5869 territorycode = convertTerritoryIsoNameToCode (territoryname , 0 );
5970 if (territorycode < 0 ) {
60- latitude = 0 ;
61- longitude = 0 ;
6271 return Py_False ;
6372 }
6473 } else
@@ -70,54 +79,62 @@ static PyObject *decode(PyObject *self, PyObject *args)
7079}
7180
7281
82+ static char encode_doc [] =
83+ "encode(latitude, longitude, (territoryname, extra_digits))\n\
84+ \n\
85+ Encodes the given latitude, longitude to one or more mapcodes.\n\
86+ Returns a list with one or more pairs of mapcode and territoryname.\n\
87+ \n\
88+ Optionally a territoryname can be provided to generate a mapcode in partiucular territory.\n" ;
7389
74- char encode_result [MAX_NR_OF_MAPCODE_RESULTS * 2 ];
75- static char * * encode (double latitude , double longitude , char * territory , int extra_digits )
90+ static PyObject * encode (PyObject * self , PyObject * args )
7691{
77- int territorycode = 0 ;
78- char * * s = (char * * ) encode_result ;
92+ double latitude , longitude ;
93+ char * territoryname = NULL ;
94+ int extra_digits = 0 , territorycode = 0 ;
95+ PyObject * result ;
96+
97+ if (!PyArg_ParseTuple (args , "dd|si" , & latitude , & longitude , & territoryname , & extra_digits ))
98+ return NULL ;
99+
100+ // printf("encode: args: %f, %f, %x, %i\n", latitude, longitude, territoryname, extra_digits);
101+
102+ if (territoryname ) {
103+ territorycode = convertTerritoryIsoNameToCode (territoryname , 0 );
104+ printf ("debug1: encode: territorystring: %s, code: %d\n" , territoryname , territorycode );
79105
80- if (territory ) {
81- territorycode = convertTerritoryIsoNameToCode (territory , 0 );
82- /*
83- printf("debug1: encode: territorystring: %s, code: %d\n", territory, territorycode);
84- */
85106 if (territorycode < 0 ) {
86- /* terminate array pointer so caller can detect it's empty */
87- s [0 ] = 0 ;
88- return s ;
107+ return Py_False ;
89108 }
90109 }
91110
92- /* printf("debug2: encode: territorystring: %s, code: %d\n", territory, territorycode);
93- */
94- int n = encodeLatLonToMapcodes ((char * * ) & encode_result , latitude , longitude , territorycode , extra_digits );
111+ // printf("encode: territorystring: %s, code: %d\n", territoryname, territorycode);
112+
113+ char * mapcode_results [MAX_NR_OF_MAPCODE_RESULTS ];
114+ int n = encodeLatLonToMapcodes (mapcode_results , latitude , longitude , territorycode , extra_digits );
115+ // printf("encode: count %d\n", n);
95116 if (n > 0 ) {
96- /* for (int i = 0; i < n; ++i) {
97- printf("debug: %s - %s\n", s[i*2], s[(i*2)+1]);
117+ result = PyList_New (n );
118+ while (n -- ) {
119+ // printf("debug: %d: %s - %s\n", n, mapcode_results[n * 2], mapcode_results[(n * 2) + 1]);
120+ PyList_SetItem (result , n , Py_BuildValue ("[ss]" , (mapcode_results [n * 2 ]), mapcode_results [(n * 2 ) + 1 ]));
98121 }
99- printf("debug: count = %d\n", i);
100- */
101- /* terminate array pointer at the end */
102- s [n * 2 ] = 0 ;
103-
104- return s ;
105- } else {
106- /* terminate array pointer at beginning */
107- s [0 ] = 0 ;
122+ return result ;
108123 }
109- return s ;
124+ return Py_False ;
110125}
111126
112- /*
113127
114- static char *encode_single(double latitude, double longitude, char *territory, int extra_digits)
115- */
128+ static char encode_single_doc [] =
129+ "encode_single(latitude, longitude, (territoryname, extra_digits))\n\
130+ \n\
131+ Encodes the given latitude, longitude to a mapcode. Optionally a territoryname\n\
132+ can be provided to generate a mapcode in partiucular territory.\n" ;
116133
117134static PyObject * encode_single (PyObject * self , PyObject * args )
118135{
119136 double latitude , longitude ;
120- char * territoryname = NULL , * encode_single_result ;
137+ char * territoryname = NULL ;
121138 int extra_digits = 0 , territorycode = 0 ;
122139 PyObject * result ;
123140
@@ -134,10 +151,9 @@ static PyObject *encode_single(PyObject *self, PyObject *args)
134151
135152 // printf("debug: encode_single: territorystring: %s, code: %d\n", territoryname, territorycode);
136153
137- encode_single_result = malloc ( MAX_NR_OF_MAPCODE_RESULTS ) ;
154+ char encode_single_result [ MAX_NR_OF_MAPCODE_RESULTS ] ;
138155 if (encodeLatLonToSingleMapcode (encode_single_result , latitude , longitude , territorycode , extra_digits ) > 0 ) {
139156 result = Py_BuildValue ("s" , encode_single_result );
140- free (encode_single_result );
141157 return result ;
142158 } else
143159 return Py_False ;
@@ -150,8 +166,8 @@ static PyMethodDef mapcode_methods[] = {
150166 { "version" , version , METH_VARARGS , version_doc },
151167 { "isvalid" , isvalid , METH_VARARGS , isvalid_doc },
152168 { "decode" , decode , METH_VARARGS , decode_doc },
153- // { "encode", encode, METH_VARARGS, NULL },
154- { "encode_single" , encode_single , METH_VARARGS , "Do a mapcode geocode" },
169+ { "encode" , encode , METH_VARARGS , encode_doc },
170+ { "encode_single" , encode_single , METH_VARARGS , encode_single_doc },
155171 { NULL , NULL , 0 , NULL }
156172};
157173
@@ -160,5 +176,5 @@ static PyMethodDef mapcode_methods[] = {
160176
161177PyMODINIT_FUNC initmapcode (void )
162178{
163- mapcode_module = Py_InitModule ("mapcode" , mapcode_methods );
164- }
179+ Py_InitModule ("mapcode" , mapcode_methods );
180+ }
0 commit comments