Skip to content

Commit 94f0021

Browse files
committed
Moved to 1.40 and fixed warnings
1 parent 96251b4 commit 94f0021

File tree

6 files changed

+93
-118
lines changed

6 files changed

+93
-118
lines changed

README

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Documentation, including example snippets of C source code, can be found in
3434

3535
Also see www.mapcode.com for background and reference materials.
3636

37-
Note: this version may be restricted to a particular area of the Earthas!
37+
Note: this version may be restricted to a particular area of the Earth!
3838
In that case, basics.h will state a version number of the for:
3939
#define mapcode_cversion "1.2xxx"
4040
where "xxx" states the geographical limitation.
@@ -49,7 +49,7 @@ of how to use the library.
4949
To build the original Mapcode tool, execute:
5050

5151
cd example
52-
gcc mapcode.cpp -o mapcode
52+
gcc mapcode.c -o mapcode
5353

5454
For help, simply execute the binary file 'mapcode' without no arguments.
5555
This tool provides a rather extensive command-line interface to encode and
@@ -60,43 +60,35 @@ decode Mapcodes.
6060
MAPCODE LIBRARY - RELEASE NOTES
6161
-------------------------------------------------------------------------------
6262

63-
1.33
63+
1.40
64+
Added extraDigits parameter to allow high-precision mapcodes to be generated.
6465

65-
Fix to not remove valid results just across the edge of a territory. Improved
66-
interface readability and renamed methods to more readable forms. Removed
67-
several warnings produced by GCC.
66+
1.33
67+
Fix to not remove valid results just across the edge of a territory. Improved interface readability
68+
and renamed methods to more readable forms.
6869

6970
1.32
70-
71-
Added encodeLatLonToSingleMapcode(); fixed 1.29 so no country-wide alternative is
72-
produced in edge cases; prevent FIJI failing to decode at exactly 180 degrees.
71+
Added encodeLatLonToSingleMapcode(); fixed 1.29 so no country-wide alternative is produced in edge cases; prevent FIJI failing to
72+
decode at exactly 180 degrees.
7373

7474
1.31
75-
7675
Added compareWithMapcodeFormat().
7776

7877
1.30
79-
8078
IUpdated the documentation and extended it with examples and suggestions.
8179

8280
1.29
83-
8481
Also generate country-wide alternative mapcodes for states.
8582

8683
1.28
87-
88-
Bug fix for the needless generation of 7-letter alternatives to short mapcodes
89-
in large states in India.
84+
Bug fix for the needless generation of 7-letter alternatives to short mapcodes in large states in India.
9085

9186
1.27
92-
9387
Improved (faster) implementation of the function isInArea.
9488

9589
1.26
96-
9790
Added alias OD ("Odisha") for indian state OR ("Orissa").
9891

9992
1.25
100-
10193
Initial release to the public domain.
10294

example/mapcode.cpp

Lines changed: 51 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
1-
/*
2-
* Copyright (C) 2014 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.
1+
/**
2+
* Copyright (C) 2014 Stichting Mapcode Foundation
3+
* For terms of use refer to http://www.mapcode.com/downloads.html
154
*/
165

176
/**
@@ -42,7 +31,6 @@
4231
#define my_isnan(x) (false)
4332
#define my_round(x) ((long) (floor((x) + 0.5)))
4433

45-
4634
static const char* VERSION = "1";
4735
static const int SELF_CHECK = 1;
4836
static const int SELF_CHECK_EXIT = 0;
@@ -91,7 +79,7 @@ static void usage(const char* appName) {
9179
printf(" encoding will only succeeed if the lat/lon is located in the territory.\n");
9280
printf("\n");
9381
printf(" %s [-b | --boundaries]\n", appName);
94-
printf(" %s [-g | --grid] <nrOfPoints>\n", appName);
82+
printf(" %s [-g | --grid] <nrOfPoints> [<extraDigits>]\n", appName);
9583
printf(" %s [-r | --random] <nrOfPoints> [<seed>]\n", appName);
9684
printf("\n");
9785
printf(" Create a test set of lat/lon pairs based on the Mapcode boundaries database\n");
@@ -183,12 +171,12 @@ static void convertLatLonToXYZ(double latDeg, double lonDeg, double* x, double*
183171
/**
184172
* This methods provides a self check for encoding lat/lon to Mapcode.
185173
*/
186-
static void selfCheckLatLonToMapcode(const double lat, double lon, const char* territory, const char* mapcode) {
174+
static void selfCheckLatLonToMapcode(const double lat, double lon, const char* territory, const char* mapcode, int extraDigits) {
187175
int context = convertTerritoryIsoNameToCode(territory, 0);
188176
char* results[MAX_NR_OF_MAPCODE_RESULTS];
189177
const double limitLat = (lat < -90.0) ? -90.0 : ((lat > 90.0) ? 90.0 : lat);
190178
const double limitLon = (lon < -180.0) ? -180.0 : ((lon > 180.0) ? 180.0 : lon);
191-
const int nrResults = encodeLatLonToMapcodes(results, limitLat, limitLon, context);
179+
const int nrResults = encodeLatLonToMapcodes(results, limitLat, limitLon, context, extraDigits);
192180
if (nrResults <= 0) {
193181
fprintf(stderr, "internal error: encoding lat/lon to Mapcode failure; "
194182
"cannot encode lat=%f, lon=%f (default territory=%s)\n",
@@ -256,7 +244,7 @@ static void selfCheckMapcodeToLatLon(const char* territory, const char* mapcode,
256244

257245
/**
258246
* The method asCoordinate() generates and returns a printable coordinate
259-
* precisely as it would be interpreted internally by Mapcode encoding
247+
* precisely as it would be interpreted internally by mapcode encoding
260248
* (i.e. correctly rounded to the nearest one-millionth of a degree).
261249
* As target, pass a buffer for at least 12 characters (including zero termination).
262250
* If target = 0, an internal scratch buffer is used (the THIRD call will
@@ -272,8 +260,8 @@ static const char* asCoordinate(double coord, char* target)
272260
c = -c;
273261
}
274262
if (target == 0) {
275-
target = &asCoordinateBuffer[ascoptr];
276263
ascoptr= ((ascoptr != 0) ? 0 : 12);
264+
target = &asCoordinateBuffer[ascoptr];
277265
}
278266
sprintf(target,"%s%d.%06d", (negative ? "-" : ""), (int) (c / 1000000), (int) (c % 1000000));
279267
return target;
@@ -285,25 +273,25 @@ static const char* asCoordinate(double coord, char* target)
285273
* If iShowError != 0, then encoding errors are output to stderr, otherwise they
286274
* are ignored.
287275
*/
288-
static void generateAndOutputMapcodes(double lat, double lon, int iShowError) {
276+
static void generateAndOutputMapcodes(double lat, double lon, int iShowError, int extraDigits) {
289277

290278
char* results[MAX_NR_OF_MAPCODE_RESULTS];
291279
int context = 0;
292280

293-
while (lon > 180) {
294-
lon -= 360;
295-
}
296-
while (lon < -180) {
297-
lon += 360;
298-
}
299-
while (lat > 90) {
300-
lat -= 180;
301-
}
302-
while (lat < -90) {
303-
lat += 180;
304-
}
305-
306-
const int nrResults = encodeLatLonToMapcodes(results, lat, lon, context);
281+
while (lon > 180) {
282+
lon -= 360;
283+
}
284+
while (lon < -180) {
285+
lon += 360;
286+
}
287+
while (lat > 90) {
288+
lat -= 180;
289+
}
290+
while (lat < -90) {
291+
lat += 180;
292+
}
293+
294+
const int nrResults = encodeLatLonToMapcodes(results, lat, lon, context, extraDigits);
307295
if (nrResults <= 0) {
308296
if (iShowError) {
309297
fprintf(stderr, "error: cannot encode lat=%s, lon=%s)\n", asCoordinate(lat, 0), asCoordinate(lon, 0));
@@ -325,7 +313,7 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError) {
325313

326314
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
327315
if (SELF_CHECK) {
328-
selfCheckLatLonToMapcode(lat, lon, foundTerritory, foundMapcode);
316+
selfCheckLatLonToMapcode(lat, lon, foundTerritory, foundMapcode, extraDigits);
329317
selfCheckMapcodeToLatLon(foundTerritory, foundMapcode, lat, lon);
330318
}
331319
}
@@ -387,6 +375,9 @@ static void showProgress(int i) {
387375
*/
388376
int main(const int argc, const char** argv)
389377
{
378+
// Assume no extra digits (unless overridden later
379+
int extraDigits=0;
380+
390381
// Provide usage message if no arguments specified.
391382
const char* appName = argv[0];
392383
if (argc < 2) {
@@ -430,7 +421,7 @@ int main(const int argc, const char** argv)
430421

431422
// Self-checking code to see if encoder produces this Mapcode for the lat/lon.
432423
if (SELF_CHECK) {
433-
selfCheckLatLonToMapcode(lat, lon, defaultTerritory, mapcode);
424+
selfCheckLatLonToMapcode(lat, lon, defaultTerritory, mapcode, extraDigits);
434425
}
435426
}
436427
}
@@ -462,7 +453,7 @@ int main(const int argc, const char** argv)
462453

463454
// Encode the lat/lon to a set of Mapcodes.
464455
char* results[MAX_NR_OF_MAPCODE_RESULTS];
465-
const int nrResults = encodeLatLonToMapcodes(results, lat, lon, context);
456+
const int nrResults = encodeLatLonToMapcodes(results, lat, lon, context, extraDigits);
466457
if (nrResults <= 0) {
467458
fprintf(stderr, "error: cannot encode lat=%f, lon=%f (default territory=%s)\n",
468459
lat, lon, defaultTerritory);
@@ -516,37 +507,37 @@ int main(const int argc, const char** argv)
516507
lon = (maxLon - minLon ) / 2.0;
517508

518509
// Try center.
519-
generateAndOutputMapcodes(lat, lon, 0);
510+
generateAndOutputMapcodes(lat, lon, 0, extraDigits);
520511

521512
// Try corners.
522-
generateAndOutputMapcodes(minLat, minLon, 0);
523-
generateAndOutputMapcodes(minLat, maxLon, 0);
524-
generateAndOutputMapcodes(maxLat, minLon, 0);
525-
generateAndOutputMapcodes(maxLat, maxLon, 0);
513+
generateAndOutputMapcodes(minLat, minLon, 0, extraDigits);
514+
generateAndOutputMapcodes(minLat, maxLon, 0, extraDigits);
515+
generateAndOutputMapcodes(maxLat, minLon, 0, extraDigits);
516+
generateAndOutputMapcodes(maxLat, maxLon, 0, extraDigits);
526517

527518
// Try JUST inside.
528519
double factor = 1.0;
529520
for (int j = 1; j < 6; ++j) {
530521

531522
double d = 1.0 / factor;
532-
generateAndOutputMapcodes(minLat + d, minLon + d, 0);
533-
generateAndOutputMapcodes(minLat + d, maxLon - d, 0);
534-
generateAndOutputMapcodes(maxLat - d, minLon + d, 0);
535-
generateAndOutputMapcodes(maxLat - d, maxLon - d, 0);
523+
generateAndOutputMapcodes(minLat + d, minLon + d, 0, extraDigits);
524+
generateAndOutputMapcodes(minLat + d, maxLon - d, 0, extraDigits);
525+
generateAndOutputMapcodes(maxLat - d, minLon + d, 0, extraDigits);
526+
generateAndOutputMapcodes(maxLat - d, maxLon - d, 0, extraDigits);
536527

537528
// Try JUST outside.
538-
generateAndOutputMapcodes(minLat - d, minLon - d, 0);
539-
generateAndOutputMapcodes(minLat - d, maxLon + d, 0);
540-
generateAndOutputMapcodes(maxLat + d, minLon - d, 0);
541-
generateAndOutputMapcodes(maxLat + d, maxLon + d, 0);
529+
generateAndOutputMapcodes(minLat - d, minLon - d, 0, extraDigits);
530+
generateAndOutputMapcodes(minLat - d, maxLon + d, 0, extraDigits);
531+
generateAndOutputMapcodes(maxLat + d, minLon - d, 0, extraDigits);
532+
generateAndOutputMapcodes(maxLat + d, maxLon + d, 0, extraDigits);
542533
factor = factor * 10.0;
543534
}
544535

545536
// Try 22m outside.
546-
generateAndOutputMapcodes(minLat - 22, (maxLon - minLon) / 2, 0);
547-
generateAndOutputMapcodes(minLat - 22, (maxLon - minLon) / 2, 0);
548-
generateAndOutputMapcodes(maxLat + 22, (maxLon - minLon) / 2, 0);
549-
generateAndOutputMapcodes(maxLat + 22, (maxLon - minLon) / 2, 0);
537+
generateAndOutputMapcodes(minLat - 22, (maxLon - minLon) / 2, 0, extraDigits);
538+
generateAndOutputMapcodes(minLat - 22, (maxLon - minLon) / 2, 0, extraDigits);
539+
generateAndOutputMapcodes(maxLat + 22, (maxLon - minLon) / 2, 0, extraDigits);
540+
generateAndOutputMapcodes(maxLat + 22, (maxLon - minLon) / 2, 0, extraDigits);
550541

551542
if ((i % SHOW_PROGRESS) == 0) {
552543
showProgress(i);
@@ -558,7 +549,7 @@ int main(const int argc, const char** argv)
558549
(strcmp(cmd, "-r") == 0) || (strcmp(cmd, "--random") == 0)) {
559550

560551
// ------------------------------------------------------------------
561-
// Generate grid test set: [-g | --grid] <nrOfPoints>
552+
// Generate grid test set: [-g | --grid] <nrOfPoints> [<extradigits>]
562553
// Generate uniform test set: [-r | --random] <nrOfPoints> [<seed>]
563554
// ------------------------------------------------------------------
564555
if ((argc < 3) || (argc > 4)) {
@@ -583,10 +574,8 @@ int main(const int argc, const char** argv)
583574
}
584575
}
585576
else {
586-
if (argc > 3) {
587-
fprintf(stderr, "error: cannot specify seed for -g/--grid\n\n");
588-
usage(appName);
589-
return NORMAL_ERROR;
577+
if (argc == 4) {
578+
extraDigits = atoi(argv[3]);
590579
}
591580
}
592581

@@ -596,7 +585,7 @@ int main(const int argc, const char** argv)
596585

597586
int gridX = 0;
598587
int gridY = 0;
599-
int line = my_round(sqrt((float) totalNrOfPoints));
588+
int line = my_round(sqrt(totalNrOfPoints));
600589
for (int i = 0; i < totalNrOfPoints; ++i) {
601590
double lat;
602591
double lon;
@@ -621,7 +610,7 @@ int main(const int argc, const char** argv)
621610
}
622611

623612
unitToLatLonDeg(unit1, unit2, &lat, &lon);
624-
generateAndOutputMapcodes(lat, lon, 1);
613+
generateAndOutputMapcodes(lat, lon, 1, extraDigits);
625614

626615
if ((i % SHOW_PROGRESS) == 0) {
627616
showProgress(i);

mapcode_library.doc

20.5 KB
Binary file not shown.

mapcodelib/basics.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
1-
/*
2-
* Copyright (C) 2014 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-
171
#define UWORD unsigned short int // 2-byte unsigned integer
182

19-
#define mapcode_cversion "1.33"
3+
#define mapcode_cversion "1.40"
204
#define MAXWIDE 10
215
#define BASEX 31
226
#define MAXFITLONG 6

0 commit comments

Comments
 (0)