[niepce] Deal with the GPS coord format dd,mm,ss.



commit 4c188624a69b64d4e843db18607f1ded6a627430
Author: Hubert Figuière <hub figuiere net>
Date:   Thu Jul 10 20:47:28 2014 -0400

    Deal with the GPS coord format dd,mm,ss.

 src/fwk/utils/exempi.cpp  |   53 ++++++++++++++++++++++++++++++++++-----------
 src/fwk/utils/testxmp.cpp |    9 +++++++
 2 files changed, 49 insertions(+), 13 deletions(-)
---
diff --git a/src/fwk/utils/exempi.cpp b/src/fwk/utils/exempi.cpp
index 996e7b7..6573898 100644
--- a/src/fwk/utils/exempi.cpp
+++ b/src/fwk/utils/exempi.cpp
@@ -286,30 +286,57 @@ XmpMeta::gpsCoordFromXmp(const std::string & xmps)
         return NAN;
     }
 
+    // extract minutes. There are two formats
+    double fminutes = 0.;
     const char *next = strchr(current, ',');
     if (next) {
-        // DD,mm,ss
-
+        // DD,mm,ss format
+        next++;
+        if (!*next) {
+            return NAN;
+        }
+        len = strlen(next);
+        if (len <= 1) {
+            // too short
+            return NAN;
+        }
+        std::string seconds = std::string(next, len - 1);
+        double sseconds = 0.;
+        try {
+            sseconds = boost::lexical_cast<double>(seconds) / 60;
+            std::string minutes = std::string(current, next - current - 1);
+            fminutes = boost::lexical_cast<double>(minutes);
+            fminutes += sseconds;
+        }
+        catch(const std::exception & e) {
+            return NAN;
+        }
     }
     else {
-        // DD,mm.mm ?
-        std::string minutes = std::string(current, len - 1);
-        double fminutes = 0.;
+        // DD,mm.mm format
         try {
-            coord = boost::lexical_cast<int>(degs);
-            if (coord > 180) {
-                return NAN;
-            }
+            std::string minutes = std::string(current, len - 1);
             fminutes = boost::lexical_cast<double>(minutes);
         }
         catch(const std::exception & e) {
             return NAN;
         }
-        coord += fminutes / 60.f;
+    }
 
-        if (*orientation == 'S' || *orientation == 'W') {
-            coord = -coord;
-        }
+    // degrees.
+    try {
+        coord = boost::lexical_cast<int>(degs);
+    }
+    catch(const std::exception & e) {
+        return NAN;
+    }
+    if (coord > 180) {
+        return NAN;
+    }
+    coord += fminutes / 60.f;
+
+    if (*orientation == 'S' || *orientation == 'W') {
+        coord = -coord;
     }
 
     return coord;
diff --git a/src/fwk/utils/testxmp.cpp b/src/fwk/utils/testxmp.cpp
index 4e7f604..54c15a2 100644
--- a/src/fwk/utils/testxmp.cpp
+++ b/src/fwk/utils/testxmp.cpp
@@ -64,6 +64,10 @@ int test_main( int, char *[] )             // note the name!
         output = fwk::XmpMeta::gpsCoordFromXmp("45,W");
         BOOST_CHECK(isnan(output));
 
+        // malformed 3
+        output = fwk::XmpMeta::gpsCoordFromXmp("45,29,N");
+        BOOST_CHECK(isnan(output));
+
         // out of bounds
         output = fwk::XmpMeta::gpsCoordFromXmp("200,29.6681666667N");
         BOOST_CHECK(isnan(output));
@@ -78,6 +82,11 @@ int test_main( int, char *[] )             // note the name!
         output = fwk::XmpMeta::gpsCoordFromXmp(gps2);
         BOOST_CHECK(output == -73.6381194444449960201382054947316646575927734375);
 
+        // well-formed 3
+        std::string gps3 = "45,29,30.45N";
+        output = fwk::XmpMeta::gpsCoordFromXmp(gps3);
+        BOOST_CHECK(output == 45.49179166666666418450404307805001735687255859375);
+
        return 0;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]