Skip to content

Commit 6d4dd79

Browse files
committed
Added convenience method for other languages
1 parent 3475b1a commit 6d4dd79

File tree

4 files changed

+108
-6
lines changed

4 files changed

+108
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ The list of support languages may grow over time.
179179

180180
## Release Notes
181181

182+
### 2.5.4
183+
184+
* Added `encodeLatLonToSelectedMapcode` as a convenience for languages that use the
185+
C library, but have difficulties dealing with multi-dimensional arrays (like Swift).
186+
182187
### 2.5.3
183188

184189
* Cleaned up code after running Codacy code reviews.

mapcodelib/mapcoder.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ static const int STATE_MACHINE[27][6] = {
20372037
STATE_GO |
20382038
512, ERR_UNEXPECTED_HYPHEN},
20392039

2040-
//13 prefix.LLLLL ===
2040+
//13 prefix.LLLLL ===
20412041
{22 |
20422042
128, ERR_UNEXPECTED_DOT, ERR_INVALID_MAPCODE_FORMAT, ERR_INVALID_VOWEL, STATE_GO |
20432043
128, 11 |
@@ -2047,7 +2047,7 @@ static const int STATE_MACHINE[27][6] = {
20472047
{ERR_BAD_TERRITORY_FORMAT, ERR_UNEXPECTED_DOT, 15, 15, ERR_BAD_TERRITORY_FORMAT, ERR_UNEXPECTED_HYPHEN},
20482048
//15 TC-S === get 2nd state letter
20492049
{ERR_BAD_TERRITORY_FORMAT, ERR_UNEXPECTED_DOT, 16, 16, ERR_BAD_TERRITORY_FORMAT, ERR_UNEXPECTED_HYPHEN},
2050-
//16 TC-SS === white:waitprefix | det/vow:TC-SSS
2050+
//16 TC-SS === white:waitprefix | det/vow:TC-SSS
20512051
{18 |
20522052
64, ERR_UNEXPECTED_DOT, 17, 17, ERR_DOT_MISSING, ERR_UNEXPECTED_HYPHEN},
20532053
//17 TC-SSS === white:waitprefix
@@ -2159,7 +2159,7 @@ static enum MapcodeError parseMapcodeString(MapcodeElements *mapcodeElements, co
21592159
cx = getRomanVersionOf((UWORD) w);
21602160
}
21612161
c = decodeChar(cx);
2162-
if (c < 0) { // vowel or illegal?
2162+
if (c < 0) { // vowel or illegal?
21632163
if (c == -1) { // illegal?
21642164
return ERR_INVALID_CHARACTER;
21652165
}
@@ -3053,6 +3053,21 @@ encodeLatLonToSingleMapcode(char *mapcode, double latDeg, double lonDeg, enum Te
30533053
}
30543054

30553055

3056+
// PUBLIC - encode lat,lon for territory to a selected mapcode (from all results) with extraDigits accuracy
3057+
int
3058+
encodeLatLonToSelectedMapcode(char *mapcode, double latDeg, double lonDeg, enum Territory territory, int extraDigits, int indexOfSelected) {
3059+
Mapcodes mapcodes;
3060+
int nrOfResults = 0;
3061+
nrOfResults = encodeLatLonToMapcodes(&mapcodes, latDeg, lonDeg, territory, extraDigits);
3062+
ASSERT(nrOfResults == mapcodes.count);
3063+
if ((nrOfResults <= 0) || (indexOfSelected < 0) || (indexOfSelected > nrOfResults)) {
3064+
return 0;
3065+
}
3066+
strcpy(mapcode, mapcodes.mapcode[indexOfSelected]);
3067+
return nrOfResults;
3068+
}
3069+
3070+
30563071
// PUBLIC - encode lat,lon for (optional) territory to mapcodes with extraDigits accuracy
30573072
int
30583073
encodeLatLonToMapcodes(Mapcodes *mapcodes, double latDeg, double lonDeg, enum Territory territory, int extraDigits) {

mapcodelib/mapcoder.h

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extern "C" {
8080
* International mapcodes never include a territory ISO3166 code, nor a space.
8181
*/
8282
typedef struct {
83-
int count; // The number of mapcode results (length of array).
83+
int count; // The number of mapcode results (length of array).
8484
char mapcode[MAX_NR_OF_MAPCODE_RESULTS][MAX_MAPCODE_RESULT_ASCII_LEN]; // The mapcodes.
8585
} Mapcodes;
8686

@@ -179,8 +179,7 @@ int encodeLatLonToMapcodes(
179179
*
180180
* Arguments:
181181
* result - Returned Mapcode. The caller must not allocate or de-allocated this string.
182-
* The resulting string MUST be allocated (and de-allocated) by the caller (contrary to
183-
* encodeLatLonToMapcodes!).
182+
* The resulting string MUST be allocated (and de-allocated) by the caller.
184183
* The caller should allocate at least MAX_MAPCODE_RESULT_ASCII_LEN characters for the string.
185184
* lat - Latitude, in degrees. Range: -90..90.
186185
* lon - Longitude, in degrees. Range: -180..180.
@@ -201,6 +200,38 @@ int encodeLatLonToSingleMapcode(
201200
int extraDigits);
202201

203202

203+
/**
204+
* Encode a latitude, longitude pair (in degrees) to a single Mapcode, selected from all Mapcodes.
205+
* This method is offered for languages which have trouble supporting multi-dimensional arrays from C
206+
* (like Swift).
207+
*
208+
* Arguments:
209+
* result - Returned Mapcode. The caller must not allocate or de-allocated this string.
210+
* The resulting string MUST be allocated (and de-allocated) by the caller.
211+
* The caller should allocate at least MAX_MAPCODE_RESULT_ASCII_LEN characters for the string.
212+
* lat - Latitude, in degrees. Range: -90..90.
213+
* lon - Longitude, in degrees. Range: -180..180.
214+
* territory - Territory (e.g. as obtained from getTerritoryCode), used as encoding context.
215+
* Pass TERRITORY_NONE or TERRITORY_UNKNOWN to get Mapcodes for all territories.
216+
* extraDigits - Number of extra "digits" to add to the generated mapcode. The preferred default is 0.
217+
* Other valid values are 1 to 8, which will add extra letters to the mapcodes to
218+
* make them represent the coordinate more accurately.
219+
* indexOfSelected - Index of selected mapcode. Must be in valid range for number of results.
220+
* The index is base 0. To fetch all Mapcodes, pass 0 in a first call to retrieve the
221+
* first Mapcode and the total number of results and iterate over the rest.
222+
*
223+
* Returns:
224+
* Total number of results available for selection. <=0 if encoding failed, or >0 if it succeeded.
225+
*/
226+
int encodeLatLonToSelectedMapcode(
227+
char *mapcode,
228+
double latDeg,
229+
double lonDeg,
230+
enum Territory territory,
231+
int extraDigits,
232+
int indexOfSelected);
233+
234+
204235
/**
205236
* Decode a utf8 or ascii Mapcode to a latitude, longitude pair (in degrees).
206237
*

test/unittest.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,56 @@ static int testSingleEncodes(void) {
17111711
}
17121712

17131713

1714+
static int testSelectedEncodes(void) {
1715+
int nrTests = 0;
1716+
char mapcode[MAX_MAPCODE_RESULT_ASCII_LEN];
1717+
double lat = 52.158993;
1718+
double lon = 4.492346;
1719+
int i = 0;
1720+
int total = 0;
1721+
do {
1722+
total = encodeLatLonToSelectedMapcode(mapcode, lat, lon, TERRITORY_NONE, 0, i);
1723+
if (total != 4) {
1724+
foundError();
1725+
printf("*** ERROR *** testSelectedEncodes, expected %d alternatives, but got %d", 4, total);
1726+
}
1727+
switch (i) {
1728+
case 0:
1729+
if (strcmp(mapcode, "NLD QJM.0G") != 0) {
1730+
foundError();
1731+
printf("*** ERROR *** testSelectedEncodes, expected '%s', but got '%s', alternative %d\n", "NLD QJM.0G", mapcode, i);
1732+
}
1733+
break;
1734+
case 1:
1735+
if (strcmp(mapcode, "NLD CZQ.15C") != 0) {
1736+
foundError();
1737+
printf("*** ERROR *** testSelectedEncodes, expected '%s', but got '%s', alternative %d\n", "NLD CZQ.15C", mapcode, i);
1738+
}
1739+
break;
1740+
case 2:
1741+
if (strcmp(mapcode, "NLD N39J.MZN") != 0) {
1742+
foundError();
1743+
printf("*** ERROR *** testSelectedEncodes, expected '%s', but got '%s', alternative %d\n", "NLD N39J.MZN", mapcode, i);
1744+
}
1745+
break;
1746+
case 3:
1747+
if (strcmp(mapcode, "VHVN4.TZ9S") != 0) {
1748+
foundError();
1749+
printf("*** ERROR *** testSelectedEncodes, expected '%s', but got '%s', alternative %d\n", "VHVN4.TZ9S", mapcode, i);
1750+
}
1751+
break;
1752+
default:
1753+
foundError();
1754+
printf("*** ERROR *** testSelectedEncodes, expected %d alternatives, but got %d", 4, i);
1755+
break;
1756+
}
1757+
++i;
1758+
} while (i < total);
1759+
++nrTests;
1760+
return nrTests;
1761+
}
1762+
1763+
17141764
static int testGetFullTerritoryNameLocal(const char *expectedName, enum Territory territory, int alternative) {
17151765
int nrTests = 0;
17161766
int expectedCode = (*expectedName ? 1 : 0);
@@ -2227,6 +2277,7 @@ int main(const int argc, const char **argv) {
22272277

22282278
printf("-----------------------------------------------------------\nEncode/decode tests\n");
22292279
nrTests += testSingleEncodes();
2280+
nrTests += testSelectedEncodes();
22302281
nrTests += testEncodeDecode();
22312282

22322283
printf("-----------------------------------------------------------\nRe-encode tests\n");

0 commit comments

Comments
 (0)