[mapcode] 18/32: Added boundaries check

Stefan Fritsch sf at moszumanska.debian.org
Wed Nov 2 23:27:16 UTC 2016


This is an automated email from the git hooks/post-receive script.

sf pushed a commit to annotated tag v1.33
in repository mapcode.

commit e1907bb4ead3f5c5f743d9bc50e4ca2617af4ac4
Author: Rijn Buve <rijn at buve.nl>
Date:   Fri Aug 29 20:35:37 2014 +0200

    Added boundaries check
---
 mapcode   | Bin 713200 -> 0 bytes
 mapcode.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++------------
 2 files changed, 128 insertions(+), 30 deletions(-)

diff --git a/mapcode b/mapcode
deleted file mode 100755
index 3dd3aee..0000000
Binary files a/mapcode and /dev/null differ
diff --git a/mapcode.c b/mapcode.c
index 169b670..3d02efd 100644
--- a/mapcode.c
+++ b/mapcode.c
@@ -9,6 +9,7 @@
 
 static const double PI = 3.14159265358979323846;
 static const int RESULTS_MAX = 64;
+static const int SHOW_PROGRESS = 250;
 
 static void usage(const char* appName) {
     printf("MAPCODE (C library version %s)\n", mapcode_cversion);
@@ -25,11 +26,13 @@ static void usage(const char* appName) {
     printf("       Encode a lat/lon to a Mapcode. If the territory code is specified, the\n");
     printf("       encoding will only succeeed if the lat/lon is located in the territory.\n");
     printf("\n");
-    printf("    %s [-g | --grid] <nrPoints> [<seed>]\n", appName);
+    printf("    %s [-b | --boundaries]\n", appName);
+    printf("    %s [-g | --grid] <nrPoints>\n", appName);
     printf("    %s [-r | --random] <nrPoints> [<seed>]\n", appName);
     printf("\n");
-    printf("       Create a test set of a number of a grid or random uniformly distributed\n");
-    printf("       lat/lon pairs, (x, y, z) points and the Mapcode aliases.\n");
+    printf("       Create a test set of lat/lon pairs based on the Mapcode boundaries database\n");
+    printf("       as a fixed 3D grid or random uniformly distributed set of lat/lons with their\n");
+    printf("       (x, y, z) coordinates and all Mapcode aliases.\n");
     printf("\n");
     printf("       The output format is:\n");
     printf("           <number-of-aliases> <lat-deg> <lon-deg> <x> <y> <z>\n");
@@ -46,30 +49,67 @@ static void usage(const char* appName) {
     printf("       and the (x, y, z) coordinates are placed on a sphere with radius 1.\n");
 }
 
+static double radToDeg(double rad){
+    return (rad / PI) * 180.0;
+}
+
+static double degToRad(double deg){
+    return (deg / 180.0) * PI;
+}
+
 /**
  * Given a single number between 0..1, generate a latitude, longitude (in degrees) and a 3D
  * (x, y, z) point on a sphere with a radius of 1.
  */
-static void unitToLatLonDegXYZ(
-    const double unit1, const double unit2,
-    double* latDeg, double* lonDeg, double* x, double* y, double* z) {
+static void unitToLatLonDeg(
+    const double unit1, const double unit2, double* latDeg, double* lonDeg) {
 
     // Calculate uniformly distributed 3D point on sphere (radius = 1.0):
     // http://mathproofs.blogspot.co.il/2005/04/uniform-random-distribution-on-sphere.html
     const double theta0 = (2.0 * PI) * unit1;
     const double theta1 = acos(1.0 - (2.0 * unit2));
-    *x = sin(theta0) * sin(theta1);
-    *y = cos(theta0) * sin(theta1);
-    *z = cos(theta1);
+    double x = sin(theta0) * sin(theta1);
+    double y = cos(theta0) * sin(theta1);
+    double z = cos(theta1);
 
     // Convert Carthesian 3D point into lat/lon (radius = 1.0):
     // http://stackoverflow.com/questions/1185408/converting-from-longitude-latitude-to-cartesian-coordinates
-    const double latRad = asin(*z);
-    const double lonRad = atan2(*y, *x);
+    const double latRad = asin(z);
+    const double lonRad = atan2(y, x);
 
     // Convert radians to degrees.
-    *latDeg = isnan(latRad) ? 90.0 : (latRad * (180.0 / PI));
-    *lonDeg = isnan(lonRad) ? 180.0 : (lonRad * (180.0 / PI));
+    *latDeg = isnan(latRad) ? 90.0 : radToDeg(latRad);
+    *lonDeg = isnan(lonRad) ? 180.0 : radToDeg(lonRad);
+}
+
+static void convertLatLonToXYZ(double latDeg, double lonDeg, double* x, double* y, double* z) {
+    double latRad = degToRad(latDeg);
+    double lonRad = degToRad(lonDeg);
+    *x = cos(latRad) * cos(lonRad);
+    *y = cos(latRad) * sin(lonRad);
+    *z = sin(latRad);
+}
+
+static int printMapcodes(double lat, double lon, int iShowError) {
+    const char* results[RESULTS_MAX];
+    int context = 0;
+    const int nrResults = coord2mc(results, lat, lon, context);
+    if (nrResults <= 0) {
+        if (iShowError) {
+            fprintf(stderr, "error: cannot encode lat=%f, lon=%f)\n", lat, lon);
+        }
+        return -1;
+    }
+    double x;
+    double y;
+    double z;
+    convertLatLonToXYZ(lat, lon, &x, &y, &z);
+    printf("%d %lf %lf %lf %lf %lf\n", nrResults, lat, lon, x, y, z);
+    for (int j = 0; j < nrResults; ++j) {
+        printf("%s %s\n", results[(j * 2) + 1], results[(j * 2)]);
+    }
+    printf("\n");
+    return nrResults;
 }
 
 int main(const int argc, const char** argv)
@@ -135,6 +175,76 @@ int main(const int argc, const char** argv)
             printf("%s %s\n", results[(i * 2) + 1], results[(i * 2)]);
         }
     }
+    else if ((strcmp(cmd, "-b") == 0) || (strcmp(cmd, "--boundaries") == 0)) {
+
+        // ------------------------------------------------------------------
+        // Generate a test set based on the Mapcode boundaries.
+        // ------------------------------------------------------------------
+        if (argc != 2) {
+            fprintf(stderr, "error: incorrect number of arguments\n\n");
+            usage(appName);
+            return -1;
+        }
+        for (int i = 0; i < NR_RECS; ++i) {
+            long minLonE6;
+            long maxLonE6;
+            long minLatE6;
+            long maxLatE6;
+            double minLon;
+            double maxLon;
+            double minLat;
+            double maxLat;
+            double lat;
+            double lon;
+
+            get_boundaries(i, &minLonE6, &minLatE6, &maxLonE6, &maxLatE6);
+            minLon = ((double) minLonE6) / 1.0E6;
+            maxLon = ((double) maxLonE6) / 1.0E6;
+            minLat = ((double) minLatE6) / 1.0E6;
+            maxLat = ((double) maxLatE6) / 1.0E6;
+
+            // Try center.
+            lat = (maxLat - minLat ) / 2.0;
+            lon = (maxLon - minLon ) / 2.0;
+
+            // Try center.
+            printMapcodes(lat, lon, 0);
+
+            // Try corners.
+            printMapcodes(minLat, minLon, 0);
+            printMapcodes(minLat, maxLon, 0);
+            printMapcodes(maxLat, minLon, 0);
+            printMapcodes(maxLat, maxLon, 0);
+
+            // Try JUST inside.
+            double factor = 1.0;
+            for (int j = 1; j < 6; ++j) {
+
+                double d = 1.0 / factor;
+                printMapcodes(minLat + d, minLon + d, 0);
+                printMapcodes(minLat + d, maxLon - d, 0);
+                printMapcodes(maxLat - d, minLon + d, 0);
+                printMapcodes(maxLat - d, maxLon - d, 0);
+
+                // Try JUST outside.
+                printMapcodes(minLat - d, minLon - d, 0);
+                printMapcodes(minLat - d, maxLon + d, 0);
+                printMapcodes(maxLat + d, minLon - d, 0);
+                printMapcodes(maxLat + d, maxLon + d, 0);
+                factor = factor * 10.0;
+            }
+
+            // Try 22m outside.
+            printMapcodes(minLat - 22, (maxLon - minLon) / 2, 0);
+            printMapcodes(minLat - 22, (maxLon - minLon) / 2, 0);
+            printMapcodes(maxLat + 22, (maxLon - minLon) / 2, 0);
+            printMapcodes(maxLat + 22, (maxLon - minLon) / 2, 0);
+
+            if ((i % SHOW_PROGRESS) == 0) {
+                fprintf(stderr, "Processed %d of %d regions...\r", i, NR_RECS);
+            }
+        }
+    }
     else if ((strcmp(cmd, "-g") == 0) || (strcmp(cmd, "--grid") == 0) ||
         (strcmp(cmd, "-r") == 0) || (strcmp(cmd, "--random") == 0)) {
 
@@ -177,18 +287,12 @@ int main(const int argc, const char** argv)
         double lonLargestNrOfResults = 0.0;
         int totalNrOfResults = 0;
 
-        const char* results[RESULTS_MAX];
-        int context = 0;
-
         int gridX = 0;
         int gridY = 0;
         int line = round(sqrt(nrPoints));
         for (int i = 0; i < nrPoints; ++i) {
             double lat;
             double lon;
-            double x;
-            double y;
-            double z;
             double unit1;
             double unit2;
 
@@ -209,25 +313,19 @@ int main(const int argc, const char** argv)
                 }
             }
 
-            unitToLatLonDegXYZ(unit1, unit2, &lat, &lon, &x, &y, &z);
-            const int nrResults = coord2mc(results, lat, lon, context);
-            if (nrResults <= 0) {
-                fprintf(stderr, "error: cannot encode lat=%f, lon=%f)\n", lat, lon);
-                return -1;
-            }
+            unitToLatLonDeg(unit1, unit2, &lat, &lon);
+            const int nrResults = printMapcodes(lat, lon, 1);
             if (nrResults > largestNrOfResults) {
                 largestNrOfResults = nrResults;
                 latLargestNrOfResults = lat;
                 lonLargestNrOfResults = lon;
             }
             totalNrOfResults += nrResults;
-            printf("%d %lf %lf %lf %lf %lf\n", nrResults, lat, lon, x, y, z);
-            for (int j = 0; j < nrResults; ++j) {
-                printf("%s %s\n", results[(j * 2) + 1], results[(j * 2)]);
+            if ((i % SHOW_PROGRESS) == 0) {
+                fprintf(stderr, "Created %d of %d 3D %s data points...\r", i, nrPoints, random ? "random" : "grid");
             }
-            printf("\n");
         }
-        fprintf(stderr, "Statistics:\n");
+        fprintf(stderr, "\nStatistics:\n");
         fprintf(stderr, "Total number of 3D points generated     = %d\n", nrPoints);
         fprintf(stderr, "Total number of Mapcodes generated      = %d\n", totalNrOfResults);
         fprintf(stderr, "Average number of Mapcodes per 3D point = %f\n", ((float) totalNrOfResults) / ((float) nrPoints));

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/mapcode.git



More information about the Pkg-grass-devel mailing list