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/* *
4231#define my_isnan (x ) (false )
4332#define my_round (x ) ((long ) (floor((x) + 0.5 )))
4433
45-
4634static const char * VERSION = " 1" ;
4735static const int SELF_CHECK = 1 ;
4836static 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 */
388376int 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);
0 commit comments