Skip to content

Commit 2d8e47b

Browse files
committed
Added std basics.h and added XYZ option
1 parent bf0f8c9 commit 2d8e47b

File tree

2 files changed

+52
-46
lines changed

2 files changed

+52
-46
lines changed

example/mapcode.cpp

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ static void usage(const char* appName) {
8888
printf(" Encode a lat/lon to a Mapcode. If the territory code is specified, the\n");
8989
printf(" encoding will only succeeed if the lat/lon is located in the territory.\n");
9090
printf("\n");
91-
printf(" %s [-b | --boundaries] [<extraDigits>]\n", appName);
92-
printf(" %s [-g | --grid] <nrOfPoints> [<extraDigits>]\n", appName);
93-
printf(" %s [-r | --random] <nrOfPoints> [<extraDigits>] [<seed>]\n", appName);
91+
printf(" %s [-b[XYZ] | --boundaries[XYZ]] [<extraDigits>]\n", appName);
92+
printf(" %s [-g[XYZ] | --grid[XYZ]] <nrOfPoints> [<extraDigits>]\n", appName);
93+
printf(" %s [-r[XYZ] | --random[XYZ]] <nrOfPoints> [<extraDigits>] [<seed>]\n", appName);
9494
printf("\n");
9595
printf(" Create a test set of lat/lon pairs based on the Mapcode boundaries database\n");
9696
printf(" as a fixed 3D grid or random uniformly distributed set of lat/lons with their\n");
@@ -101,7 +101,7 @@ static void usage(const char* appName) {
101101
printf(" (You may wish to specify a specific seed to regenerate test cases).\n");
102102
printf("\n");
103103
printf(" The output format is:\n");
104-
printf(" <number-of-aliases> <lat-deg> <lon-deg> <x> <y> <z>\n");
104+
printf(" <number-of-aliases> <lat-deg> <lon-deg> [<x> <y> <z>]\n");
105105
printf(" <territory> <mapcode> (repeated 'number-of-aliases' times)\n");
106106
printf(" (empty lines and next record)\n");
107107
printf(" Ranges:\n");
@@ -287,7 +287,7 @@ static const char* asCoordinate(double coord, char* target)
287287
* If iShowError != 0, then encoding errors are output to stderr, otherwise they
288288
* are ignored.
289289
*/
290-
static void generateAndOutputMapcodes(double lat, double lon, int iShowError, int extraDigits) {
290+
static void generateAndOutputMapcodes(double lat, double lon, int iShowError, int extraDigits, int useXYZ) {
291291

292292
char* results[MAX_NR_OF_MAPCODE_RESULTS];
293293
int context = 0;
@@ -313,11 +313,16 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError, in
313313
}
314314
}
315315

316-
double x;
317-
double y;
318-
double z;
319-
convertLatLonToXYZ(lat, lon, &x, &y, &z);
320-
printf("%d %s %s %lf %lf %lf\n", nrResults, asCoordinate(lat, 0), asCoordinate(lon, 0), x, y, z);
316+
if (useXYZ) {
317+
double x;
318+
double y;
319+
double z;
320+
convertLatLonToXYZ(lat, lon, &x, &y, &z);
321+
printf("%d %s %s %lf %lf %lf\n", nrResults, asCoordinate(lat, 0), asCoordinate(lon, 0), x, y, z);
322+
}
323+
else {
324+
printf("%d %s %s\n", nrResults, asCoordinate(lat, 0), asCoordinate(lon, 0));
325+
}
321326
for (int j = 0; j < nrResults; ++j) {
322327
const char* foundMapcode = results[(j * 2)];
323328
const char* foundTerritory = results[(j * 2) + 1];
@@ -392,6 +397,9 @@ int main(const int argc, const char** argv)
392397
// Assume no extra digits (unless overridden later.
393398
int extraDigits = 0;
394399

400+
// If XYZ is added to -b, -r or -g, print x, y, z coordinates
401+
int useXYZ = 0;
402+
395403
// Provide usage message if no arguments specified.
396404
const char* appName = argv[0];
397405
if (argc < 2) {
@@ -486,7 +494,8 @@ int main(const int argc, const char** argv)
486494
}
487495
}
488496
}
489-
else if ((strcmp(cmd, "-b") == 0) || (strcmp(cmd, "--boundaries") == 0)) {
497+
else if ((strcmp(cmd, "-b") == 0) || (strcmp(cmd, "-bXYZ") == 0) ||
498+
(strcmp(cmd, "--boundaries") == 0) || (strcmp(cmd, "--boundariesXYZ") == 0)) {
490499

491500
// ------------------------------------------------------------------
492501
// Generate a test set based on the Mapcode boundaries.
@@ -499,6 +508,7 @@ int main(const int argc, const char** argv)
499508
if (argc == 3) {
500509
extraDigits = atoi(argv[2]);
501510
}
511+
useXYZ = (strstr(cmd, "XYZ") != 0);
502512

503513
resetStatistics(NR_BOUNDARY_RECS);
504514
for (int i = 0; i < totalNrOfPoints; ++i) {
@@ -522,42 +532,37 @@ int main(const int argc, const char** argv)
522532
// Try center.
523533
lat = (maxLat - minLat ) / 2.0;
524534
lon = (maxLon - minLon ) / 2.0;
525-
526-
// Try center.
527-
generateAndOutputMapcodes(lat, lon, 0, extraDigits);
535+
generateAndOutputMapcodes(lat, lon, 0, extraDigits, useXYZ);
528536

529537
// Try corners.
530-
generateAndOutputMapcodes(minLat, minLon, 0, extraDigits);
531-
generateAndOutputMapcodes(minLat, maxLon, 0, extraDigits);
532-
generateAndOutputMapcodes(maxLat, minLon, 0, extraDigits);
533-
generateAndOutputMapcodes(maxLat, maxLon, 0, extraDigits);
538+
generateAndOutputMapcodes(minLat, minLon, 0, extraDigits, useXYZ);
539+
generateAndOutputMapcodes(minLat, maxLon, 0, extraDigits, useXYZ);
540+
generateAndOutputMapcodes(maxLat, minLon, 0, extraDigits, useXYZ);
541+
generateAndOutputMapcodes(maxLat, maxLon, 0, extraDigits, useXYZ);
534542

535543
// Try JUST inside.
536-
double factor = 10.0;
537-
for (int j = 1; j < 2; ++j) {
538-
539-
double d = 1.0 / factor;
540-
generateAndOutputMapcodes(minLat + d, minLon + d, 0, extraDigits);
541-
generateAndOutputMapcodes(minLat + d, maxLon - d, 0, extraDigits);
542-
generateAndOutputMapcodes(maxLat - d, minLon + d, 0, extraDigits);
543-
generateAndOutputMapcodes(maxLat - d, maxLon - d, 0, extraDigits);
544-
545-
// Try JUST outside.
546-
generateAndOutputMapcodes(minLat - d, minLon - d, 0, extraDigits);
547-
generateAndOutputMapcodes(minLat - d, maxLon + d, 0, extraDigits);
548-
generateAndOutputMapcodes(maxLat + d, minLon - d, 0, extraDigits);
549-
generateAndOutputMapcodes(maxLat + d, maxLon + d, 0, extraDigits);
550-
factor = factor * 10.0;
551-
}
544+
const double d = 0.000001;
545+
generateAndOutputMapcodes(minLat + d, minLon + d, 0, extraDigits, useXYZ);
546+
generateAndOutputMapcodes(minLat + d, maxLon - d, 0, extraDigits, useXYZ);
547+
generateAndOutputMapcodes(maxLat - d, minLon + d, 0, extraDigits, useXYZ);
548+
generateAndOutputMapcodes(maxLat - d, maxLon - d, 0, extraDigits, useXYZ);
549+
550+
// Try JUST outside.
551+
generateAndOutputMapcodes(minLat - d, minLon - d, 0, extraDigits, useXYZ);
552+
generateAndOutputMapcodes(minLat - d, maxLon + d, 0, extraDigits, useXYZ);
553+
generateAndOutputMapcodes(maxLat + d, minLon - d, 0, extraDigits, useXYZ);
554+
generateAndOutputMapcodes(maxLat + d, maxLon + d, 0, extraDigits, useXYZ);
552555

553556
if ((i % SHOW_PROGRESS) == 0) {
554557
showProgress(i);
555558
}
556559
}
557560
outputStatistics();
558561
}
559-
else if ((strcmp(cmd, "-g") == 0) || (strcmp(cmd, "--grid") == 0) ||
560-
(strcmp(cmd, "-r") == 0) || (strcmp(cmd, "--random") == 0)) {
562+
else if ((strcmp(cmd, "-g") == 0) || (strcmp(cmd, "-gXYZ") == 0) ||
563+
(strcmp(cmd, "--grid") == 0) || (strcmp(cmd, "--gridXYZ") == 0) ||
564+
(strcmp(cmd, "-r") == 0) || (strcmp(cmd, "-rXYZ") == 0) ||
565+
(strcmp(cmd, "--random") == 0) || (strcmp(cmd, "--randomXYZ") == 0)) {
561566

562567
// ------------------------------------------------------------------
563568
// Generate grid test set: [-g | --grid] <nrOfPoints> [<extradigits>]
@@ -592,6 +597,7 @@ int main(const int argc, const char** argv)
592597
extraDigits = atoi(argv[3]);
593598
}
594599
}
600+
useXYZ = (strstr(cmd, "XYZ") != 0);
595601

596602
// Statistics.
597603
resetStatistics(nrOfPoints);
@@ -624,7 +630,7 @@ int main(const int argc, const char** argv)
624630
}
625631

626632
unitToLatLonDeg(unit1, unit2, &lat, &lon);
627-
generateAndOutputMapcodes(lat, lon, 1, extraDigits);
633+
generateAndOutputMapcodes(lat, lon, 1, extraDigits, useXYZ);
628634

629635
if ((i % SHOW_PROGRESS) == 0) {
630636
showProgress(i);

mapcodelib/basics.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2014 Stichting Mapcode Foundation (http://www.mapcode.com)
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
@@ -115,7 +115,7 @@ static const char *entity_iso = ""
115115

116116

117117

118-
static UWORD xdivider19[172] = {
118+
static const UWORD xdivider19[172] = {
119119
360, 360, 360, 360, 360, 360, 361, 361, 361, 361,
120120
362, 362, 362, 363, 363, 363, 364, 364, 365, 366,
121121
366, 367, 367, 368, 369, 370, 370, 371, 372, 373,
@@ -136,13 +136,13 @@ static UWORD xdivider19[172] = {
136136
23681,59485
137137
};
138138

139-
static long nc[MAXFITLONG+1] = { 1, 31, 961, 29791, 923521, 28629151, 887503681 };
139+
static const long nc[MAXFITLONG+1] = { 1, 31, 961, 29791, 923521, 28629151, 887503681 };
140140

141-
static long xside[MAXWIDE] = { 0, 5, 31, 168, 961, 168*31, 29791, 165869, 923521, 5141947};
141+
static const long xside[MAXWIDE] = { 0, 5, 31, 168, 961, 168*31, 29791, 165869, 923521, 5141947};
142142

143-
static long yside[MAXWIDE] = { 0, 6, 31, 176, 961, 176*31, 29791, 165869, 923521, 5141947};
143+
static const long yside[MAXWIDE] = { 0, 6, 31, 176, 961, 176*31, 29791, 165869, 923521, 5141947};
144144

145-
static signed char decode_chars[256] = {
145+
static const signed char decode_chars[256] = {
146146
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
147147
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
148148
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -161,14 +161,14 @@ static signed char decode_chars[256] = {
161161
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
162162
};
163163

164-
static char encode_chars[31] = {
164+
static const char encode_chars[31] = {
165165
'0','1','2','3','4','5','6','7','8','9',
166166
'B','C','D','F','G','H','J','K','L','M',
167167
'N','P','Q','R','S','T','V','W','X','Y','Z'
168168
};
169169

170170

171-
static UWORD data_start[MAX_CCODE+1] = {
171+
static const UWORD data_start[MAX_CCODE+1] = {
172172
0, 3, 6, 9, 13, 16, 18, 19, 30, 31,
173173
33, 35, 37, 42, 44, 47, 51, 54, 56, 58,
174174
60, 62, 64, 68, 71, 79, 81, 116, 121, 131,
@@ -232,7 +232,7 @@ static UWORD data_start[MAX_CCODE+1] = {
232232
typedef struct { long minx; long miny; long maxx; long maxy; unsigned long flags; } mminforec;
233233

234234
#ifndef MAKE_SOURCE_DIGITAL
235-
static mminforec mminfo[NR_RECS+1] = {
235+
static const mminforec mminfo[NR_RECS+1] = {
236236
{ 12433114, 41851944, 12548434, 41938434,0x001002b},
237237
{ 5850000, 35450000, 18560000, 55080000,0x50a0636},
238238
{ 12444000, 41899000, 12460000, 41908000,0x003063c},

0 commit comments

Comments
 (0)