42
42
#include < math.h>
43
43
#include < time.h>
44
44
#include " ../mapcodelib/mapcoder.c"
45
+ #include " ../mapcodelib/mapcoder.h"
45
46
46
47
// Specific define to be able to limit output to microdegrees, for test files.
47
48
#undef LIMIT_TO_MICRODEGREES
@@ -196,17 +197,17 @@ static void convertLatLonToXYZ(double latDeg, double lonDeg, double *x, double *
196
197
/* *
197
198
* This methods provides a self check for encoding lat/lon to Mapcode.
198
199
*/
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;
203
205
const double limitLat = (lat < -90.0 ) ? -90.0 : ((lat > 90.0 ) ? 90.0 : lat);
204
206
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);
206
208
if (nrResults <= 0 ) {
207
209
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);
210
211
if (selfCheckEnabled) {
211
212
exit (INTERNAL_ERROR);
212
213
}
@@ -218,22 +219,14 @@ static void selfCheckLatLonToMapcode(const double lat, double lon, const char *t
218
219
/* Check if the territory and code were found in results. Note that the territory
219
220
* may be a minimal code, like IN (which may indicate US-IN or RU-IN).
220
221
*/
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 );
231
224
}
232
225
if (!found) {
233
226
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);
237
230
if (selfCheckEnabled) {
238
231
exit (INTERNAL_ERROR);
239
232
}
@@ -245,15 +238,17 @@ static void selfCheckLatLonToMapcode(const double lat, double lon, const char *t
245
238
/* *
246
239
* This method provides a self-check for decoding a Mapcode to lat/lon.
247
240
*/
248
- static void selfCheckMapcodeToLatLon (const char *territory, const char * mapcode,
241
+ static void selfCheckMapcodeToLatLon (const char *mapcode,
249
242
const double lat, const double lon) {
250
243
double foundLat;
251
244
double foundLon;
252
- int foundContext = convertTerritoryIsoNameToCode (territory, 0 );
245
+ // TODO: Fix self-check.
246
+ // int foundContext = convertTerritoryIsoNameToCode(territory, 0);
247
+ int foundContext = 0 ;
253
248
int err = decodeMapcodeToLatLon (&foundLat, &foundLon, mapcode, foundContext);
254
249
if (err != 0 ) {
255
250
fprintf (stderr, " error: decoding mapcode to lat/lon failure; "
256
- " cannot decode '%s %s ')\n " , territory , mapcode);
251
+ " cannot decode '%s')\n " , mapcode);
257
252
if (selfCheckEnabled) {
258
253
exit (INTERNAL_ERROR);
259
254
}
@@ -266,9 +261,9 @@ static void selfCheckMapcodeToLatLon(const char *territory, const char *mapcode,
266
261
}
267
262
if ((deltaLat > DELTA) || (deltaLon > DELTA)) {
268
263
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);
272
267
if (selfCheckEnabled) {
273
268
exit (INTERNAL_ERROR);
274
269
}
@@ -278,7 +273,6 @@ static void selfCheckMapcodeToLatLon(const char *territory, const char *mapcode,
278
273
279
274
static void generateAndOutputMapcodes (double lat, double lon, int iShowError, int extraDigits, int useXYZ) {
280
275
281
- char *results[2 * MAX_NR_OF_MAPCODE_RESULTS];
282
276
int context = 0 ;
283
277
284
278
while (lon > 180.0 ) {
@@ -304,10 +298,11 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError, in
304
298
}
305
299
#endif
306
300
307
- const int nrResults = encodeLatLonToMapcodes_Deprecated (results, lat, lon, context, extraDigits);
301
+ Mapcodes mapcodes;
302
+ const int nrResults = encodeLatLonToMapcodes (&mapcodes, lat, lon, context, extraDigits);
308
303
if (nrResults <= 0 ) {
309
304
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);
311
306
exit (NORMAL_ERROR);
312
307
}
313
308
}
@@ -317,22 +312,21 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError, in
317
312
double y;
318
313
double z;
319
314
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);
321
316
}
322
317
else {
323
- printf (" %d %.14g %.14g \n " , nrResults, lat, lon);
318
+ printf (" %d %.20g %.20g \n " , nrResults, lat, lon);
324
319
}
325
320
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];
328
322
329
323
// Output result line.
330
- printf (" %s %s \n " , foundTerritory , foundMapcode);
324
+ printf (" %s\n " , foundMapcode);
331
325
332
326
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
333
327
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);
336
330
}
337
331
}
338
332
@@ -366,9 +360,9 @@ static void outputStatistics() {
366
360
fprintf (stderr, " \n Statistics:\n " );
367
361
fprintf (stderr, " Total number of 3D points generated = %d\n " , totalNrOfPoints);
368
362
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 " ,
370
364
((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 " ,
372
366
largestNrOfResults, latLargestNrOfResults, lonLargestNrOfResults);
373
367
}
374
368
@@ -437,7 +431,7 @@ int main(const int argc, const char **argv) {
437
431
}
438
432
439
433
// Output the decoded lat/lon.
440
- printf (" %.12g %.12g \n " , lat, lon);
434
+ printf (" %.20g %.20g \n " , lat, lon);
441
435
442
436
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
443
437
if (selfCheckEnabled) {
@@ -446,7 +440,7 @@ int main(const int argc, const char **argv) {
446
440
if (suffix != 0 ) {
447
441
extraDigits = (int ) (strlen (suffix) - 1 );
448
442
}
449
- selfCheckLatLonToMapcode (lat, lon, defaultTerritory, mapcode, extraDigits);
443
+ selfCheckLatLonToMapcode (lat, lon, mapcode, extraDigits);
450
444
}
451
445
}
452
446
}
@@ -514,23 +508,22 @@ int main(const int argc, const char **argv) {
514
508
}
515
509
516
510
// 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);
519
513
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 " ,
521
515
lat, lon, defaultTerritory);
522
516
return NORMAL_ERROR;
523
517
}
524
518
525
519
// Output the Mapcode.
526
520
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);
530
523
531
524
// Self-checking code to see if decoder produces the lat/lon for all of these Mapcodes.
532
525
if (selfCheckEnabled) {
533
- selfCheckMapcodeToLatLon (foundTerritory, foundMapcode, lat, lon);
526
+ selfCheckMapcodeToLatLon (foundMapcode, lat, lon);
534
527
}
535
528
}
536
529
}
0 commit comments