@@ -14,7 +14,7 @@ static void usage(const char* appName) {
1414 printf ("MAPCODE (C library version %s)\n" , mapcode_cversion );
1515 printf ("Copyright (C) 2014 Stichting Mapcode Foundation\n" );
1616 printf ("\n" );
17- printf ("Usage: \n" );
17+ printf ("Usage:\n" );
1818 printf (" %s [-d | --decode] <default-territory> <mapcode> [<mapcode> ...]\n" , appName );
1919 printf ("\n" );
2020 printf (" Decode a Mapcode to a lat/lon. The default territory code is used if\n" );
@@ -40,6 +40,8 @@ static void usage(const char* appName) {
4040 printf (" lat-deg, lon-deg : [-90..90], [-180..180]\n" );
4141 printf (" x, y, z : [-1..1]\n" );
4242 printf ("\n" );
43+ printf ("\n stdout: used for outputting 3D point data; stderr: used for statistics." );
44+ printf ("\n" );
4345 printf (" The lat/lon pairs will be distributed over the 3D surface of the Earth\n" );
4446 printf (" and the (x, y, z) coordinates are placed on a sphere with radius 1.\n" );
4547}
@@ -146,6 +148,11 @@ int main(const int argc, const char** argv)
146148 return -1 ;
147149 }
148150 const int nrPoints = atoi (argv [2 ]);
151+ if (nrPoints < 1 ) {
152+ fprintf (stderr , "error: total number of points to generate must be >= 1\n\n" );
153+ usage (appName );
154+ return -1 ;
155+ }
149156 int random = (strcmp (cmd , "-r" ) == 0 ) || (strcmp (cmd , "--random" ) == 0 );
150157 if (random ) {
151158 if (argc == 4 ) {
@@ -164,6 +171,12 @@ int main(const int argc, const char** argv)
164171 }
165172 }
166173
174+ // Statistics.
175+ int largestNrOfResults = 0 ;
176+ double latLargestNrOfResults = 0.0 ;
177+ double lonLargestNrOfResults = 0.0 ;
178+ int totalNrOfResults = 0 ;
179+
167180 const char * results [RESULTS_MAX ];
168181 int context = 0 ;
169182
@@ -202,12 +215,23 @@ int main(const int argc, const char** argv)
202215 fprintf (stderr , "error: cannot encode lat=%f, lon=%f)\n" , lat , lon );
203216 return -1 ;
204217 }
218+ if (nrResults > largestNrOfResults ) {
219+ largestNrOfResults = nrResults ;
220+ latLargestNrOfResults = lat ;
221+ lonLargestNrOfResults = lon ;
222+ }
223+ totalNrOfResults += nrResults ;
205224 printf ("%d %lf %lf %lf %lf %lf\n" , nrResults , lat , lon , x , y , z );
206225 for (int j = 0 ; j < nrResults ; ++ j ) {
207226 printf ("%s %s\n" , results [(j * 2 ) + 1 ], results [(j * 2 )]);
208227 }
209228 printf ("\n" );
210229 }
230+ fprintf (stderr , "Statistics:\n" );
231+ fprintf (stderr , "Total number of 3D points generated = %d\n" , nrPoints );
232+ fprintf (stderr , "Total number of Mapcodes generated = %d\n" , totalNrOfResults );
233+ fprintf (stderr , "Average number of Mapcodes per 3D point = %f\n" , ((float ) totalNrOfResults ) / ((float ) nrPoints ));
234+ fprintf (stderr , "Largest number of results for 1 Mapcode = %d at (%f, %f)\n" , largestNrOfResults , latLargestNrOfResults , lonLargestNrOfResults );
211235 }
212236 else {
213237
@@ -219,151 +243,3 @@ int main(const int argc, const char** argv)
219243 }
220244 return 0 ;
221245}
222-
223- /**
224- * This program can encode and decode Mapcodes. It can also generate reference Mapcodes,
225- * lat/lon pairs and their corresponding (x, y, z) coordinates.
226- *
227- * If you'd like to visualize the generated points, you can use "Processing" from
228- * processing.org with the following Processing (Java) source code:
229- *
230-
231- ---------------------------------------------------------------------------
232- // Visualize 3D points.
233- //
234- // Copyright (C) 2014, TomTom BV
235- // Rijn Buve, 2014-08-24
236-
237- final String DATA_FILE = "/Users/rijn/source/tomtom/mapcode-java/src/test/resources/random_1k.txt";
238-
239- final int N = 100000;
240- final int C = 150;
241- final int D = 1;
242- final boolean DRAW_LAT_LON = true;
243- final boolean STAR_SHAPE = true;
244-
245- float posX;
246- float posY;
247- float posZ;
248- float rotX = 1.2;
249- float rotY = 0;
250- float rotZ = 0.9;
251-
252- float[] lat = new float[N];
253- float[] lon = new float[N];
254-
255- float[] px = new float[N];
256- float[] py = new float[N];
257- float[] pz = new float[N];
258-
259- int total;
260-
261- void setup() {
262- size(650, 550, P3D);
263- posX = width / 2.0;
264- posY = height / 2.0;
265- posZ = 0.0;
266- total = 0;
267- BufferedReader reader = createReader(DATA_FILE);
268- try {
269- while (true) {
270- String line = reader.readLine();
271- if (line == null) {
272- break;
273- }
274-
275- // Parse line.
276- String[] items = split(line, " ");
277- int nrItems = Integer.parseInt(items[0]);
278- lat[total] = Float.parseFloat(items[1]) / 90.0;
279- lon[total] = Float.parseFloat(items[2]) / 180.0;
280- px[total] = Float.parseFloat(items[3]);
281- py[total] = Float.parseFloat(items[4]);
282- pz[total] = Float.parseFloat(items[5]);
283-
284- // Skip codes.
285- for (int i = 0; i < nrItems; ++i) {
286- reader.readLine();
287- }
288- reader.readLine();
289- ++total;
290- }
291- }
292- catch (IOException e) {
293- e.printStackTrace();
294- }
295- finally {
296- try {
297- reader.close();
298- }
299- catch (IOException e) {
300- // Ignored.
301- }
302- }
303- }
304-
305- void draw() {
306- clear();
307- translate(posX, posY, posZ);
308- rotateX(rotX);
309- rotateY(rotY);
310- rotateZ(rotZ);
311-
312- // Draw sphere.
313- stroke(255, 0, 0, 50);
314- fill(255, 0, 0, 50);
315- sphere(C * 0.95);
316- stroke(255, 0, 0, 255);
317-
318- // Draw poles.
319- line(0, 0, -C - 200, 0, 0, C + 200);
320-
321- // Draw sphere dots.
322- stroke(255, 255, 255, 200);
323- for (int i = 0; i < total; ++i) {
324- float x = C * px[i];
325- float y = C * py[i];
326- float z = C * pz[i];
327- if (STAR_SHAPE) {
328- line(x - D, y , z, x + D, y, z);
329- line(x, y - D , z, x, y + D, z);
330- }
331- line(x, y , z - D, x, y, z + D);
332- }
333-
334- if (DRAW_LAT_LON) {
335-
336- // Draw lat/lon bounds.
337- stroke(0, 255, 255, 200);
338- line(C + 5, -C - 50, C + 5, C + 5, -C - 50, -C - 5);
339- line(C + 5, -C - 50, -C - 5, -C - 5, -C - 50, -C - 5);
340- line(-C - 5, -C - 50, -C - 5, -C - 5, -C - 50, C + 5);
341- line(-C - 5, -C - 50, C + 5, C + 5, -C - 50, C + 5);
342-
343- // Draw lat/lon dots.
344- stroke(255, 255, 0, 200);
345- for (int i = 0; i < total; ++i) {
346- float x = C * lon[i];
347- float y = -C - 51;
348- float z = C * lat[i];
349- if (STAR_SHAPE) {
350- line(x - D , y, z, x + D, y, z);
351- line(x , y - D, z, x, y + D, z);
352- }
353- line(x , y, z - D, x, y, z + D);
354- }
355- }
356-
357- if (mousePressed) {
358- rotX += 0.01;
359- rotY -= 0.0016;
360- rotZ += 0.0043;
361- }
362- }
363- ---------------------------------------------------------------------------
364- *
365- * Have fun!
366- *
367- * Rijn Buve, August 2014.
368- */
369-
0 commit comments