Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 356e650

Browse files
committed
Fixed mapcode utility precision; added TODOs in code
1 parent 16817c7 commit 356e650

File tree

7 files changed

+57
-48
lines changed

7 files changed

+57
-48
lines changed

mapcodelib/basics.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
// *** GENERATED FILE, DO NOT CHANGE OR PRETTIFY ***
1818

19+
// TODO: Missing multiple header inclusion prevention: #ifndef __FILE_H__ #define __FILE_H__ etc.
20+
1921
#define mapcode_dataversion "2.2" // coords 2.0.2
2022

2123
static const char *ALIASES =

mapcodelib/mapcode_countrynames.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616
// *** GENERATED FILE, DO NOT CHANGE OR PRETTIFY ***
17+
18+
// TODO: Missing multiple header inclusion prevention: #ifndef __FILE_H__ #define __FILE_H__ etc.
19+
1720
const char *isofullname[] = {
1821
"Vatican City State (Holy See)",
1922
"Monaco (Principality of _)",

mapcodelib/mapcode_countrynames_short.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616
// *** GENERATED FILE, DO NOT CHANGE OR PRETTIFY ***
17+
18+
// TODO: Missing multiple header inclusion prevention: #ifndef __FILE_H__ #define __FILE_H__ etc.
19+
1720
const char *isofullname[] = {
1821
"Vatican City State (Holy See)",
1922
"Monaco",

mapcodelib/mapcode_fast_encode.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616

1717
// *** GENERATED FILE, DO NOT CHANGE OR PRETTIFY ***
18+
19+
// TODO: Missing multiple header inclusion prevention: #ifndef __FILE_H__ #define __FILE_H__ etc.
20+
1821
static int redivar[2924] = {
1922
27984500, 1383,
2023
2537718, 676,

mapcodelib/mapcode_fastalpha.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17+
// TODO: Missing multiple header inclusion prevention: #ifndef __FILE_H__ #define __FILE_H__ etc.
18+
1719
// *** GENERATED FILE, DO NOT CHANGE OR PRETTIFY ***
1820

1921
static const char parentletter[MAX_CCODE + 1] =

mapcodelib/mapcoder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17+
#ifndef __MAPCODER_H__
18+
#define __MAPCODER_H__
19+
1720
#ifdef __cplusplus
1821
extern "C" {
1922
#endif
@@ -43,7 +46,6 @@ typedef struct {
4346
char mapcode[MAX_NR_OF_MAPCODE_RESULTS][MAX_MAPCODE_RESULT_LEN]; // The mapcodes.
4447
} Mapcodes;
4548

46-
4749
/**
4850
* Encode a latitude, longitude pair (in degrees) to a set of Mapcodes.
4951
*
@@ -363,3 +365,4 @@ const UWORD *encodeToAlphabet(const char *string, int alphabet);
363365
#ifdef __cplusplus
364366
}
365367
#endif
368+
#endif

utility/mapcode.cpp

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <math.h>
4343
#include <time.h>
4444
#include "../mapcodelib/mapcoder.c"
45+
#include "../mapcodelib/mapcoder.h"
4546

4647
// Specific define to be able to limit output to microdegrees, for test files.
4748
#undef LIMIT_TO_MICRODEGREES
@@ -196,17 +197,17 @@ static void convertLatLonToXYZ(double latDeg, double lonDeg, double *x, double *
196197
/**
197198
* This methods provides a self check for encoding lat/lon to Mapcode.
198199
*/
199-
static void selfCheckLatLonToMapcode(const double lat, double lon, const char *territory, const char *mapcode,
200-
int extraDigits) {
201-
int context = convertTerritoryIsoNameToCode(territory, 0);
202-
char *results[2 * MAX_NR_OF_MAPCODE_RESULTS];
200+
static void selfCheckLatLonToMapcode(const double lat, double lon, const char *mapcode, int extraDigits) {
201+
// TODO: Fix self check; read context.
202+
// int context = convertTerritoryIsoNameToCode(territory, 0);
203+
int context = 0;
204+
Mapcodes mapcodes;
203205
const double limitLat = (lat < -90.0) ? -90.0 : ((lat > 90.0) ? 90.0 : lat);
204206
const double limitLon = (lon < -180.0) ? -180.0 : ((lon > 180.0) ? 180.0 : lon);
205-
const int nrResults = encodeLatLonToMapcodes_Deprecated(results, limitLat, limitLon, context, extraDigits);
207+
const int nrResults = encodeLatLonToMapcodes(&mapcodes, limitLat, limitLon, context, extraDigits);
206208
if (nrResults <= 0) {
207209
fprintf(stderr, "error: encoding lat/lon to mapcode failure; "
208-
"cannot encode lat=%.12g, lon=%.12g (default territory=%s)\n",
209-
lat, lon, territory);
210+
"cannot encode lat=%.20g, lon=%.20g\n", lat, lon);
210211
if (selfCheckEnabled) {
211212
exit(INTERNAL_ERROR);
212213
}
@@ -218,22 +219,14 @@ static void selfCheckLatLonToMapcode(const double lat, double lon, const char *t
218219
/* Check if the territory and code were found in results. Note that the territory
219220
* may be a minimal code, like IN (which may indicate US-IN or RU-IN).
220221
*/
221-
const char *foundMapcode = results[(i * 2)];
222-
const char *foundTerritory = results[(i * 2) + 1];
223-
char *foundTerritoryMin = strstr(foundTerritory, "-");
224-
if (foundTerritoryMin && (strlen(foundTerritoryMin) > 0)) {
225-
++foundTerritoryMin;
226-
}
227-
228-
found = (((strcmp(territory, foundTerritory) == 0) ||
229-
(strcmp(territory, foundTerritoryMin) == 0)) &&
230-
(strcmp(mapcode, foundMapcode) == 0));
222+
const char *foundMapcode = mapcodes.mapcode[i];
223+
found = (strcmp(mapcode, foundMapcode) == 0);
231224
}
232225
if (!found) {
233226
fprintf(stderr, "error: encoding lat/lon to mapcode failure; "
234-
"mapcode '%s %s' decodes to lat=%.12g(%.12g), lon=%.12g(%.12g), "
235-
"which does not encode back to '%s %s'\n",
236-
territory, mapcode, lat, limitLat, lon, limitLon, territory, mapcode);
227+
"mapcode '%s' decodes to lat=%.20g(%.20g), lon=%.20g(%.20g), "
228+
"which does not encode back to '%s'\n",
229+
mapcode, lat, limitLat, lon, limitLon, mapcode);
237230
if (selfCheckEnabled) {
238231
exit(INTERNAL_ERROR);
239232
}
@@ -245,15 +238,17 @@ static void selfCheckLatLonToMapcode(const double lat, double lon, const char *t
245238
/**
246239
* This method provides a self-check for decoding a Mapcode to lat/lon.
247240
*/
248-
static void selfCheckMapcodeToLatLon(const char *territory, const char *mapcode,
241+
static void selfCheckMapcodeToLatLon(const char *mapcode,
249242
const double lat, const double lon) {
250243
double foundLat;
251244
double foundLon;
252-
int foundContext = convertTerritoryIsoNameToCode(territory, 0);
245+
// TODO: Fix self-check.
246+
// int foundContext = convertTerritoryIsoNameToCode(territory, 0);
247+
int foundContext = 0;
253248
int err = decodeMapcodeToLatLon(&foundLat, &foundLon, mapcode, foundContext);
254249
if (err != 0) {
255250
fprintf(stderr, "error: decoding mapcode to lat/lon failure; "
256-
"cannot decode '%s %s')\n", territory, mapcode);
251+
"cannot decode '%s')\n", mapcode);
257252
if (selfCheckEnabled) {
258253
exit(INTERNAL_ERROR);
259254
}
@@ -266,9 +261,9 @@ static void selfCheckMapcodeToLatLon(const char *territory, const char *mapcode,
266261
}
267262
if ((deltaLat > DELTA) || (deltaLon > DELTA)) {
268263
fprintf(stderr, "error: decoding mapcode to lat/lon failure; "
269-
"lat=%.12g, lon=%.12g produces mapcode %s %s, "
270-
"which decodes to lat=%.12g (delta=%.12g), lon=%.12g (delta=%.12g)\n",
271-
lat, lon, territory, mapcode, foundLat, deltaLat, foundLon, deltaLon);
264+
"lat=%.20g, lon=%.20g produces mapcode %s, "
265+
"which decodes to lat=%.20g (delta=%.20g), lon=%.20g (delta=%.20g)\n",
266+
lat, lon, mapcode, foundLat, deltaLat, foundLon, deltaLon);
272267
if (selfCheckEnabled) {
273268
exit(INTERNAL_ERROR);
274269
}
@@ -278,7 +273,6 @@ static void selfCheckMapcodeToLatLon(const char *territory, const char *mapcode,
278273

279274
static void generateAndOutputMapcodes(double lat, double lon, int iShowError, int extraDigits, int useXYZ) {
280275

281-
char *results[2 * MAX_NR_OF_MAPCODE_RESULTS];
282276
int context = 0;
283277

284278
while (lon > 180.0) {
@@ -304,10 +298,11 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError, in
304298
}
305299
#endif
306300

307-
const int nrResults = encodeLatLonToMapcodes_Deprecated(results, lat, lon, context, extraDigits);
301+
Mapcodes mapcodes;
302+
const int nrResults = encodeLatLonToMapcodes(&mapcodes, lat, lon, context, extraDigits);
308303
if (nrResults <= 0) {
309304
if (iShowError) {
310-
fprintf(stderr, "error: cannot encode lat=%.12g, lon=%.12g)\n", lat, lon);
305+
fprintf(stderr, "error: cannot encode lat=%.20g, lon=%.20g)\n", lat, lon);
311306
exit(NORMAL_ERROR);
312307
}
313308
}
@@ -317,22 +312,21 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError, in
317312
double y;
318313
double z;
319314
convertLatLonToXYZ(lat, lon, &x, &y, &z);
320-
printf("%d %.14g %.14g %.14g %.14g %.14g\n", nrResults, lat, lon, x, y, z);
315+
printf("%d %.20g %.20g %.20g %.20g %.20g\n", nrResults, lat, lon, x, y, z);
321316
}
322317
else {
323-
printf("%d %.14g %.14g\n", nrResults, lat, lon);
318+
printf("%d %.20g %.20g\n", nrResults, lat, lon);
324319
}
325320
for (int j = 0; j < nrResults; ++j) {
326-
const char *foundMapcode = results[(j * 2)];
327-
const char *foundTerritory = results[(j * 2) + 1];
321+
const char *foundMapcode = mapcodes.mapcode[j];
328322

329323
// Output result line.
330-
printf("%s %s\n", foundTerritory, foundMapcode);
324+
printf("%s\n", foundMapcode);
331325

332326
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
333327
if (selfCheckEnabled) {
334-
selfCheckLatLonToMapcode(lat, lon, foundTerritory, foundMapcode, extraDigits);
335-
selfCheckMapcodeToLatLon(foundTerritory, foundMapcode, lat, lon);
328+
selfCheckLatLonToMapcode(lat, lon, foundMapcode, extraDigits);
329+
selfCheckMapcodeToLatLon(foundMapcode, lat, lon);
336330
}
337331
}
338332

@@ -366,9 +360,9 @@ static void outputStatistics() {
366360
fprintf(stderr, "\nStatistics:\n");
367361
fprintf(stderr, "Total number of 3D points generated = %d\n", totalNrOfPoints);
368362
fprintf(stderr, "Total number of mapcodes generated = %d\n", totalNrOfResults);
369-
fprintf(stderr, "Average number of mapcodes per 3D point = %.12g\n",
363+
fprintf(stderr, "Average number of mapcodes per 3D point = %.20g\n",
370364
((float) totalNrOfResults) / ((float) totalNrOfPoints));
371-
fprintf(stderr, "Largest number of results for 1 mapcode = %d at (%.12g, %.12g)\n",
365+
fprintf(stderr, "Largest number of results for 1 mapcode = %d at (%.20g, %.20g)\n",
372366
largestNrOfResults, latLargestNrOfResults, lonLargestNrOfResults);
373367
}
374368

@@ -437,7 +431,7 @@ int main(const int argc, const char **argv) {
437431
}
438432

439433
// Output the decoded lat/lon.
440-
printf("%.12g %.12g\n", lat, lon);
434+
printf("%.20g %.20g\n", lat, lon);
441435

442436
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
443437
if (selfCheckEnabled) {
@@ -446,7 +440,7 @@ int main(const int argc, const char **argv) {
446440
if (suffix != 0) {
447441
extraDigits = (int) (strlen(suffix) - 1);
448442
}
449-
selfCheckLatLonToMapcode(lat, lon, defaultTerritory, mapcode, extraDigits);
443+
selfCheckLatLonToMapcode(lat, lon, mapcode, extraDigits);
450444
}
451445
}
452446
}
@@ -514,23 +508,22 @@ int main(const int argc, const char **argv) {
514508
}
515509

516510
// Encode the lat/lon to a set of Mapcodes.
517-
char *results[2 * MAX_NR_OF_MAPCODE_RESULTS];
518-
const int nrResults = encodeLatLonToMapcodes_Deprecated(results, lat, lon, context, extraDigits);
511+
Mapcodes mapcodes;
512+
const int nrResults = encodeLatLonToMapcodes(&mapcodes, lat, lon, context, extraDigits);
519513
if (nrResults <= 0) {
520-
fprintf(stderr, "error: cannot encode lat=%.12g, lon=%.12g (default territory=%s)\n",
514+
fprintf(stderr, "error: cannot encode lat=%.20g, lon=%.20g (default territory=%s)\n",
521515
lat, lon, defaultTerritory);
522516
return NORMAL_ERROR;
523517
}
524518

525519
// Output the Mapcode.
526520
for (int i = 0; i < nrResults; ++i) {
527-
const char *foundMapcode = results[(i * 2)];
528-
const char *foundTerritory = results[(i * 2) + 1];
529-
printf("%s %s\n", foundTerritory, foundMapcode);
521+
const char *foundMapcode = mapcodes.mapcode[i];
522+
printf("%s\n", foundMapcode);
530523

531524
// Self-checking code to see if decoder produces the lat/lon for all of these Mapcodes.
532525
if (selfCheckEnabled) {
533-
selfCheckMapcodeToLatLon(foundTerritory, foundMapcode, lat, lon);
526+
selfCheckMapcodeToLatLon(foundMapcode, lat, lon);
534527
}
535528
}
536529
}

0 commit comments

Comments
 (0)