Skip to content

Commit e0bb80c

Browse files
committed
Added PGs changes to printing lat/lon
1 parent 3930e94 commit e0bb80c

File tree

7 files changed

+62
-26
lines changed

7 files changed

+62
-26
lines changed

LICENSE

100644100755
File mode changed.

NOTICE

100644100755
File mode changed.

RELEASE_NOTES

100644100755
File mode changed.

example/mapcode.cpp

100644100755
Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@
2424
*/
2525

2626
#include <stdio.h>
27-
#include <time.h>
2827
#include <math.h>
28+
#include <time.h>
2929
#include "../mapcodelib/mapcoder.c"
3030

31+
#define my_isnan(x) (false)
32+
#define my_round(x) ((long) (floor((x) + 0.5)))
33+
34+
3135
static const char* VERSION = "1";
3236
static const int SELF_CHECK = 1;
3337
static const int SELF_CHECK_EXIT = 0;
@@ -145,8 +149,8 @@ static void unitToLatLonDeg(
145149
const double lonRad = atan2(y, x);
146150

147151
// Convert radians to degrees.
148-
*latDeg = (latRad == NAN) ? 90.0 : radToDeg(latRad);
149-
*lonDeg = (lonRad == NAN) ? 180.0 : radToDeg(lonRad);
152+
*latDeg = my_isnan(latRad) ? 90.0 : radToDeg(latRad);
153+
*lonDeg = my_isnan(lonRad) ? 180.0 : radToDeg(lonRad);
150154
}
151155

152156

@@ -159,7 +163,6 @@ static void convertLatLonToXYZ(double latDeg, double lonDeg, double* x, double*
159163
double latRad = degToRad(latDeg);
160164
double lonRad = degToRad(lonDeg);
161165
*x = cos(latRad) * cos(lonRad);
162-
*y = cos(latRad) * sin(lonRad);
163166
*z = sin(latRad);
164167
}
165168

@@ -239,6 +242,32 @@ static void selfCheckMapcodeToLatLon(const char* territory, const char* mapcode,
239242

240243

241244

245+
/**
246+
* The method asCoordinate() generates and returns a printable coordinate
247+
* precisely as it would be interpreted internally by Mapcode encoding
248+
* (i.e. correctly rounded to the nearest one-millionth of a degree).
249+
* As target, pass a buffer for at least 12 characters (including zero termination).
250+
* If target = 0, an internal scratch buffer is used (the THIRD call will
251+
* overwrite the first call).
252+
*/
253+
static char asCoordinateBuffer[24];
254+
static int ascoptr;
255+
static const char* asCoordinate(double coord, char* target)
256+
{
257+
long c = (long) ((coord * 1000000) + ((coord < 0) ? -0.5 : 0.5));
258+
int negative = (c < 0);
259+
if (negative) {
260+
c = -c;
261+
}
262+
if (target == 0) {
263+
target = &asCoordinateBuffer[ascoptr];
264+
ascoptr= ((ascoptr != 0) ? 0 : 12);
265+
}
266+
sprintf(target,"%s%d.%06d", (negative ? "-" : ""), c / 1000000, c % 1000000);
267+
return target;
268+
}
269+
270+
242271
/**
243272
* The method printMapcode() generates and outputs Mapcodes for a lat/lon pair.
244273
* If iShowError != 0, then encoding errors are output to stderr, otherwise they
@@ -251,7 +280,7 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError) {
251280
const int nrResults = encodeLatLonToMapcodes(results, lat, lon, context);
252281
if (nrResults <= 0) {
253282
if (iShowError) {
254-
fprintf(stderr, "error: cannot encode lat=%f, lon=%f)\n", lat, lon);
283+
fprintf(stderr, "error: cannot encode lat=%s, lon=%s)\n", asCoordinate(lat, 0), asCoordinate(lon, 0));
255284
exit(NORMAL_ERROR);
256285
}
257286
}
@@ -260,7 +289,7 @@ static void generateAndOutputMapcodes(double lat, double lon, int iShowError) {
260289
double y;
261290
double z;
262291
convertLatLonToXYZ(lat, lon, &x, &y, &z);
263-
printf("%d %lf %lf %lf %lf %lf\n", nrResults, lat, lon, x, y, z);
292+
printf("%d %s %s %lf %lf %lf\n", nrResults, asCoordinate(lat, 0), asCoordinate(lon, 0), x, y, z);
264293
for (int j = 0; j < nrResults; ++j) {
265294
const char* foundMapcode = results[(j * 2)];
266295
const char* foundTerritory = results[(j * 2) + 1];
@@ -541,7 +570,7 @@ int main(const int argc, const char** argv)
541570

542571
int gridX = 0;
543572
int gridY = 0;
544-
int line = (int) (sqrt(totalNrOfPoints) + 0.5);
573+
int line = my_round(sqrt(totalNrOfPoints));
545574
for (int i = 0; i < totalNrOfPoints; ++i) {
546575
double lat;
547576
double lon;

mapcodelib/basics.h

100644100755
File mode changed.

mapcodelib/mapcoder.c

100644100755
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@ int current_ccode=-1; // cache for setup_country
4242

4343
#define MAXGLOBALRESULTS 32 // The worst actually seems to be 14, which is at 52.050500, 113.468600
4444
#define WORSTCASE_MAPCODE_BYTES 16 // worst case is high-precision earth xxxxx.yyyy-zz, rounded upwords to multiple of 4 bytes (assuming latin-only results)
45-
char global_buffer[MAXGLOBALRESULTS*2][WORSTCASE_MAPCODE_BYTES]; // cache for worst-case result
45+
char global_storage[2048]; // cyclic cache for results
46+
int storage_ptr;
47+
char *addstorage(const char *str)
48+
{
49+
int len=strlen(str)+1; // bytes needed;
50+
storage_ptr &= (2048-1);
51+
if (storage_ptr<0 || storage_ptr+len>2048-2) storage_ptr=0;
52+
strcpy(global_storage+storage_ptr,str);
53+
storage_ptr+=len;
54+
return global_storage+storage_ptr-len;
55+
}
4656
char **global_results;
4757
int nr_global_results;
4858

@@ -1090,10 +1100,8 @@ void addresult(char *resultbuffer, char *result, long x,long y, int ccode)
10901100
if (result_override>=0) ccode=result_override; // 1.32 true recursive processing
10911101
#endif
10921102
if (*result && global_results && nr_global_results>=0 && nr_global_results+1<(2*MAXGLOBALRESULTS)) {
1093-
global_results[nr_global_results]=global_buffer[nr_global_results];
1094-
strcpy(global_results[nr_global_results++],result);
1095-
global_results[nr_global_results]=global_buffer[nr_global_results];
1096-
strcpy(global_results[nr_global_results++],makeiso(ccode,1));
1103+
global_results[nr_global_results++] = addstorage(result);
1104+
global_results[nr_global_results++] = addstorage(makeiso(ccode,1));
10971105
}
10981106
// add to buffer (if any)
10991107
if (resultbuffer)

mapcodelib/mapcoder.h

100644100755
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,17 @@ const UWORD* encodeToAlphabet(const char* mapcode, int alphabet);
206206

207207

208208
/**
209-
* List of #defines to support legacy systems. These names are deprecated but supported for
210-
* existing systems. Please use the names above.
209+
* list of #defines to support legacy systems
211210
*/
212-
#define coord2mc encodeLatLonToMapcodes
213-
#define coord2mc1 encodeLatLonToSingleMapcode
214-
#define mc2coord decodeMapcodeToLatLon
215-
#define lookslikemapcode compareWithMapcodeFormat
216-
#define text2tc convertTerritoryIsoNameToCode
217-
#define tc2text convertTerritoryCodeToIsoName
218-
#define tccontext getCountryOrParentCountry
219-
#define tcparent getParentCountryOfState
220-
#define decode_to_roman decodeToRoman
221-
#define encode_to_alphabet encodeToAlphabet
222-
#define MAX_MAPCODE_TERRITORY_CODE MAX_CCODE
223-
#define NR_BOUNDARY_RECS NR_RECS
211+
#define coord2mc encodeLatLonToMapcodes
212+
#define coord2mc1 encodeLatLonToSingleMapcode
213+
#define mc2coord decodeMapcodeToLatLon
214+
#define lookslikemapcode compareWithMapcodeFormat
215+
#define text2tc convertTerritoryIsoNameToCode
216+
#define tc2text convertTerritoryCodeToIsoName
217+
#define tccontext getCountryOrParentCountry
218+
#define tcparent getParentCountryOfState
219+
#define decode_to_roman decodeToRoman
220+
#define encode_to_alphabet encodeToAlphabet
221+
#define MAX_MAPCODE_TERRITORY_CODE MAX_CCODE
222+
#define NR_BOUNDARY_RECS NR_RECS

0 commit comments

Comments
 (0)