[Groonga-commit] groonga/grnxx at 9c9e94a [master] Fix invalid latitude/longitude.

Back to archive index

susumu.yata null+****@clear*****
Fri Jul 4 17:58:58 JST 2014


susumu.yata	2014-07-04 17:58:58 +0900 (Fri, 04 Jul 2014)

  New Revision: 9c9e94ac957952cbc4d3fce3199ae32c3ae46424
  https://github.com/groonga/grnxx/commit/9c9e94ac957952cbc4d3fce3199ae32c3ae46424

  Message:
    Fix invalid latitude/longitude.

  Added files:
    lib/grnxx/types.cpp
  Modified files:
    include/grnxx/types.hpp
    lib/grnxx/Makefile.am

  Modified: include/grnxx/types.hpp (+17 -9)
===================================================================
--- include/grnxx/types.hpp    2014-07-04 12:30:14 +0900 (7411b3b)
+++ include/grnxx/types.hpp    2014-07-04 17:58:58 +0900 (8ba2e08)
@@ -214,16 +214,18 @@ class GeoPoint {
 
   // The upper 32 bits of "latitude" and "longitude" are ignored.
   GeoPoint(Int latitude, Int longitude)
-      : latitude_(static_cast<int32_t>(latitude)),
-        longitude_(static_cast<int32_t>(longitude)) {}
-
-  // The upper 32 bits of "latitude" are ignored.
-  void set_latitude(Int latitude) {
+      : latitude_(),
+        longitude_() {
+    if ((latitude < DEGREES(-90)) || (latitude > DEGREES(90)) ||
+        (longitude < DEGREES(-180)) || (longitude >= DEGREES(180))) {
+      // Fix out-of-range values.
+      fix(&latitude, &longitude);
+    }
+    // The south pole or the north pole.
+    if ((latitude == DEGREES(-90)) || (latitude == DEGREES(90))) {
+      longitude = 0;
+    }
     latitude_ = static_cast<int32_t>(latitude);
-  }
-
-  // The upper 32 bits of "longitude" are ignored.
-  void set_longitude(Int longitude) {
     longitude_ = static_cast<int32_t>(longitude);
   }
 
@@ -237,6 +239,12 @@ class GeoPoint {
  private:
   int32_t latitude_;  // Latitude in milliseconds.
   int32_t longitude_;  // Longitude in milliseconds.
+
+  static constexpr Int DEGREES(Int value) {
+    return value * 60 * 60 * 1000;
+  }
+
+  static void fix(Int *latitude, Int *longitude);
 };
 
 inline bool operator==(GeoPoint lhs, GeoPoint rhs) {

  Modified: lib/grnxx/Makefile.am (+2 -1)
===================================================================
--- lib/grnxx/Makefile.am    2014-07-04 12:30:14 +0900 (6b87d95)
+++ lib/grnxx/Makefile.am    2014-07-04 17:58:58 +0900 (6b92d83)
@@ -15,7 +15,8 @@ libgrnxx_la_SOURCES =			\
 	index.cpp			\
 	library.cpp			\
 	name.cpp			\
-	table.cpp
+	table.cpp			\
+	types.cpp
 
 libgrnxx_includedir = ${includedir}/grnxx
 libgrnxx_include_HEADERS =		\

  Added: lib/grnxx/types.cpp (+41 -0) 100644
===================================================================
--- /dev/null
+++ lib/grnxx/types.cpp    2014-07-04 17:58:58 +0900 (b8b73c8)
@@ -0,0 +1,41 @@
+#include "grnxx/types.hpp"
+
+namespace grnxx {
+
+void GeoPoint::fix(Int *latitude, Int *longitude) {
+  // Fix the latitude to [0, 360).
+  if ((*latitude <= DEGREES(-360)) || (*latitude >= DEGREES(360))) {
+    *latitude %= DEGREES(360);
+  }
+  if (*latitude < DEGREES(0)) {
+    *latitude += DEGREES(360);
+  }
+  // Fix the longitude to [0, 360).
+  if ((*longitude <= DEGREES(-360)) || (*longitude >= DEGREES(360))) {
+    *longitude %= DEGREES(360);
+  }
+  if (*longitude < DEGREES(0)) {
+    *longitude += DEGREES(360);
+  }
+  // Fix the latitude in (90, 270).
+  if ((*latitude > DEGREES(90)) && (*latitude < DEGREES(270))) {
+    *latitude = DEGREES(180) - *latitude;
+    if (*latitude < DEGREES(0)) {
+      *latitude += DEGREES(360);
+    }
+    *longitude += DEGREES(180);
+    if (*longitude >= DEGREES(360)) {
+      *longitude -= DEGREES(360);
+    }
+  }
+  // Fix the latitude to [-90, 90].
+  if (*latitude >= DEGREES(270)) {
+    *latitude -= DEGREES(360);
+  }
+  // Fix the longitude to [-180, 180).
+  if (*longitude >= DEGREES(180)) {
+    *longitude -= DEGREES(360);
+  }
+}
+
+}  // namespace grnxx
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index