Skip to content

Commit 1d825dc

Browse files
committed
Initial commit
0 parents  commit 1d825dc

File tree

6 files changed

+19354
-0
lines changed

6 files changed

+19354
-0
lines changed

.gitignore

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# -----------------------------------------------------------------------------
2+
# Compiled sources
3+
# -----------------------------------------------------------------------------
4+
*.com
5+
*.class
6+
*.dll
7+
*.exe
8+
*.o
9+
*.so
10+
11+
# -----------------------------------------------------------------------------
12+
# Packages and archives
13+
# -----------------------------------------------------------------------------
14+
# It's better to unpack these files and commit the raw source
15+
# git has its own built in compression methods.
16+
# *.7z
17+
# *.dmg
18+
# *.gz
19+
# *.iso
20+
# *.jar
21+
# *.rar
22+
# *.tar
23+
# *.zip
24+
25+
# -----------------------------------------------------------------------------
26+
# Logs and databases
27+
# -----------------------------------------------------------------------------
28+
*.log
29+
# *.sql - we need to commit raw SQL for test database creation
30+
*.sqlite
31+
*.tlog
32+
*.epoch
33+
*.swp
34+
*.hprof
35+
*.hprof.index
36+
*~
37+
38+
# -----------------------------------------------------------------------------
39+
# OS generated files
40+
# -----------------------------------------------------------------------------
41+
.DS_Store*
42+
ehthumbs.db
43+
Icon?
44+
Thumbs.db
45+
46+
# -----------------------------------------------------------------------------
47+
# Maven generated files
48+
# -----------------------------------------------------------------------------
49+
target/
50+
*/target/
51+
out/
52+
53+
# -----------------------------------------------------------------------------
54+
# IntelliJ IDEA
55+
# -----------------------------------------------------------------------------
56+
# Not safe to share any of this across different IDEA major versions. Use the POMs.
57+
*.iml
58+
*.iws
59+
*.releaseBackup
60+
61+
.idea
62+
.idea/.name
63+
.idea/artifacts/
64+
.idea/workspace.xml
65+
.idea/dataSources.xml
66+
.idea/sqlDataSources.xml
67+
.idea/dynamic.xml
68+
.idea/compiler.xml
69+
.idea/misc.xml
70+
.idea/tasks.xml
71+
.idea/uiDesigner.xml
72+
.idea/libraries/*
73+
.idea/dictionaries/*
74+
.idea/modules.xml
75+
.idea/scopes/*
76+
77+
# .idea/inspectionProfiles/*
78+
79+
atlassian-ide-plugin.xml
80+
81+
# -----------------------------------------------------------------------------
82+
# Eclipse files
83+
# -----------------------------------------------------------------------------
84+
.classpath
85+
.project
86+
.settings
87+
.cache
88+
89+
# -----------------------------------------------------------------------------
90+
# Netbeans files
91+
# -----------------------------------------------------------------------------
92+
nb-configuration.xml
93+
*.orig

README.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
+----
2+
| Copyright (C) 2014 Stichting Mapcode Foundation
3+
| For terms of use refer to http://www.mapcode.com/downloads.html
4+
+----
5+
6+
This directory contains the original C++ Mapcode sources and an application to
7+
- encode lat/lon to Mapcodes,
8+
- decode Mapcodes to lat/lon,
9+
- generate 3D uniformly distributed lat/lon pairs and their Mapcodes.
10+
11+
To build the original Mapcode tool:
12+
gcc mapcode.c -o mapcode
13+
14+
For help, simply execute 'mapcode' without ny arguments.

mapcode.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* Copyright (C) 2014 Stichting Mapcode Foundation
3+
* For terms of use refer to http://www.mapcode.com/downloads.html
4+
*/
5+
6+
#include <stdio.h>
7+
#include <math.h>
8+
#include "mapcoder/mapcoder.c"
9+
10+
static const double PI = 3.14159265358979323846;
11+
12+
static void usage(const char* appName) {
13+
printf("Usage: \n");
14+
printf(" %s [-d | --decode] <default-country-ISO3> <mapcode> [<mapcode> ...]\n", appName);
15+
printf("\n");
16+
printf(" Decode a Mapcode to a lat/lon. The default country code is used if\n");
17+
printf(" the Mapcode is a shorthand local code\n");
18+
printf("\n");
19+
printf(" %s [-e | --encode] <lat:-90..90> <lon:-180..180> [country-ISO3]>\n", appName);
20+
printf("\n");
21+
printf(" Encode a lat/lon to a Mapcode. If the country code is specified, the\n");
22+
printf(" encoding will only succeeed if the lat/lon is located in the country.\n");
23+
printf("\n");
24+
printf(" %s [-g | --generate] <nrPoints> [<seed>]\n", appName);
25+
printf("\n");
26+
printf(" Create a test set of a number of uniformly distributed lat/lon pairs,\n");
27+
printf(" 3D x/y/z points and their Mapcodes). The output format is:\n");
28+
printf(" <nr> <lat> <lon> <x> <y> <z>\n");
29+
printf(" <country> <mapcode> (repeated 'nr' rtimes)\n");
30+
printf(" <1 empty line>\n");
31+
printf("\n");
32+
printf(" The points will be uniformly distributed over the 3D surface of the Earth\n");
33+
printf(" rather than using uniformly distributed lat/lon values.\n");
34+
printf(" Ranges:\n");
35+
printf(" nr > 1 lat = -90..90 lon = -180..180 x,y,z = -1..1\n");
36+
printf("\n");
37+
}
38+
39+
int main(const int argc, const char** argv)
40+
{
41+
// Provide usage message if no arguments specified.
42+
const char* appName = argv[0];
43+
if (argc < 2) {
44+
usage(appName);
45+
return -1;
46+
}
47+
48+
// First argument: command.
49+
const char* cmd = argv[1];
50+
if ((strcmp(cmd, "-d") == 0) || (strcmp(cmd, "--decode") == 0)) {
51+
52+
// Decode: [-d | --decode] <default-country-ISO3> <mapcode> [<mapcode> ...]
53+
if (argc < 3) {
54+
usage(appName);
55+
return -1;
56+
}
57+
58+
const char* defaultCountry = argv[2];
59+
double lat;
60+
double lon;
61+
int context = text2tc(defaultCountry, 0);
62+
for (int i = 3; i <= argc; ++i) {
63+
int err = mc2coord(&lat, &lon, argv[i], context);
64+
if (err != 0) {
65+
printf("error: cannot decode '%s' (default country='%s')\n", argv[i], argv[1]);
66+
return -1;
67+
}
68+
printf("%g %g\n", lat, lon);
69+
}
70+
}
71+
else if ((strcmp(cmd, "-e") == 0) || (strcmp(cmd, "--encode") == 0)) {
72+
73+
// Encode: [-e | --encode] <lat:-90..90> <lon:-180..180> [country-ISO3]>
74+
if ((argc != 3) && (argc != 4)) {
75+
usage(appName);
76+
return -1;
77+
}
78+
const double lat = atof(argv[1]);
79+
const double lon = atof(argv[2]);
80+
81+
const char* results[32];
82+
int context = 0;
83+
if (argc == 4) {
84+
context = text2tc(argv[3], 0);
85+
}
86+
const int nrResults = coord2mc(results, lat, lon, context);
87+
if (nrResults == 0) {
88+
printf("error: cannot encode lat=%s, lon=%s (default country='%s')\n", argv[1], argv[2], (argc == 4) ? argv[3] : "none");
89+
return -1;
90+
}
91+
for (int i = 0; i < nrResults; ++i) {
92+
printf("%s %s\n", results[(i * 2) + 1], results[(i * 2)]);
93+
}
94+
}
95+
else if ((strcmp(cmd, "-g") == 0) || (strcmp(cmd, "--generate") == 0)) {
96+
97+
// Generate uniform test set: [-g | --generate] <nrPoints> [<seed>]
98+
if ((argc != 3) && (argc != 4)) {
99+
usage(appName);
100+
return -1;
101+
}
102+
const int nrPoints = atoi(argv[2]);
103+
104+
if (argc == 4) {
105+
const int seed = atoi(argv[3]);
106+
srand(seed);
107+
}
108+
else {
109+
srand(time(0));
110+
}
111+
const char* results[32];
112+
int context = 0;
113+
for (int i = 0; i < nrPoints; ++i) {
114+
115+
// Calculate uniformly distributed 3D point on sphere (radius = 1.0):
116+
// http://mathproofs.blogspot.co.il/2005/04/uniform-random-distribution-on-sphere.html
117+
const double unitRand = ((double) rand()) / RAND_MAX;
118+
const double theta0 = (2.0 * PI) * unitRand;
119+
const double theta1 = acos(1.0 - (2.0 * unitRand));
120+
const double x = sin(theta0) * sin(theta1);
121+
const double y = cos(theta0) * sin(theta1);
122+
const double z = cos(theta1);
123+
124+
// Convert Carthesian 3D point into lat/lon (radius = 1.0):
125+
// http://stackoverflow.com/questions/1185408/converting-from-longitude-latitude-to-cartesian-coordinates
126+
const double latRad = asin(z);
127+
const double lonRad = atan2(y, x);
128+
129+
// Convert radians to degrees.
130+
const double lat = latRad * (180.0 / PI);
131+
const double lon = lonRad * (180.0 / PI);
132+
133+
const int nrResults = coord2mc(results, lat, lon, context);
134+
if (nrResults <= 0) {
135+
printf("error: cannot encode lat=%g, lon=%g)\n", lat, lon);
136+
return -1;
137+
}
138+
printf("%d %g %g %g %g %g\n", nrResults, lat, lon, x, y, z);
139+
for (int i = 0; i < nrResults; ++i) {
140+
printf("%s %s\n", results[i*2 + 1], results[i*2]);
141+
}
142+
printf("\n");
143+
}
144+
}
145+
else {
146+
147+
// Usage.
148+
usage(appName);
149+
return -1;
150+
}
151+
return 0;
152+
}

0 commit comments

Comments
 (0)