Skip to content

Commit 55c8adc

Browse files
committed
Removed unnecessary code.
1 parent 9060711 commit 55c8adc

File tree

2 files changed

+69
-55
lines changed

2 files changed

+69
-55
lines changed

mapcodemodule.c

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
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

921
static char version_doc[] =
1022
"version()\n\
@@ -17,8 +29,6 @@ static PyObject *version(PyObject *self, PyObject *args)
1729
}
1830

1931

20-
21-
2232
static char isvalid_doc[] =
2333
"isvalid()\n\
2434
\n\
@@ -27,9 +37,9 @@ Verify if the provided string has the right mapcode syntax.\n";
2737
static 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+
4253
static 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

117134
static 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

161177
PyMODINIT_FUNC initmapcode(void)
162178
{
163-
mapcode_module = Py_InitModule("mapcode", mapcode_methods);
164-
}
179+
Py_InitModule("mapcode", mapcode_methods);
180+
}

setup.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
#!/usr/bin/env python
22

3-
from distutils.core import setup, Extension
43

4+
from distutils.core import setup, Extension
55

6-
mapcode_module = Extension('mapcode/_mapcode',
7-
sources=['mapcode/mapcode_wrap.c', 'mapcode/mapcode_swig.c'],
8-
include_dirs=['../mapcode-cpp/mapcodelib']
9-
)
106

117
setup(
128
name='mapcode',
13-
version='0.1',
9+
ext_modules=[Extension('mapcode',
10+
sources=['mapcodemodule.c'],
11+
include_dirs=['../mapcode-cpp/mapcodelib']
12+
)],
13+
version='0.2',
1414
description='A Python module to do mapcode encoding and decoding. See http://www.mapcode.com for more information.',
1515
author='Erik Bos',
1616
author_email='erik@xs4all.nl',
1717
url='https://github.com/mapcode-foundation/mapcode-python',
18-
packages=['mapcode'],
19-
ext_modules=[mapcode_module],
2018
license='Apache License 2.0',
2119
classifiers=[
2220
'Development Status :: 3 - Alpha',

0 commit comments

Comments
 (0)