[geocode-glib] forward: Parse Nominatim boundingbox attribute



commit 590f746bbb772ddeb81048ac512302583c1a95f8
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Mon Jan 27 23:33:24 2014 +0100

    forward: Parse Nominatim boundingbox attribute
    
    https://bugzilla.gnome.org/show_bug.cgi?id=723095

 geocode-glib/geocode-forward.c |   24 +++++++++++++++++++++++-
 geocode-glib/geocode-reverse.c |   33 ++++++++++++++++++++++++++++-----
 geocode-glib/test-gcglib.c     |   24 ++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 6 deletions(-)
---
diff --git a/geocode-glib/geocode-forward.c b/geocode-glib/geocode-forward.c
index 42c1738..1439af3 100644
--- a/geocode-glib/geocode-forward.c
+++ b/geocode-glib/geocode-forward.c
@@ -755,7 +755,7 @@ _geocode_create_place_from_attributes (GHashTable *ht)
 {
         GeocodePlace *place;
         GeocodeLocation *loc = NULL;
-        const char *name, *street, *building;
+        const char *name, *street, *building, *bbox_corner;
         GeocodePlaceType place_type;
         gdouble longitude, latitude;
 
@@ -767,6 +767,28 @@ _geocode_create_place_from_attributes (GHashTable *ht)
 
         place = geocode_place_new (name, place_type);
 
+        /* If one corner exists, then all exists */
+        bbox_corner = g_hash_table_lookup (ht, "boundingbox-top");
+        if (bbox_corner != NULL) {
+            GeocodeBoundingBox *bbox;
+            gdouble top, bottom, left, right;
+
+            top = g_ascii_strtod (bbox_corner, NULL);
+
+            bbox_corner = g_hash_table_lookup (ht, "boundingbox-bottom");
+            bottom = g_ascii_strtod (bbox_corner, NULL);
+
+            bbox_corner = g_hash_table_lookup (ht, "boundingbox-left");
+            left = g_ascii_strtod (bbox_corner, NULL);
+
+            bbox_corner = g_hash_table_lookup (ht, "boundingbox-right");
+            right = g_ascii_strtod (bbox_corner, NULL);
+
+            bbox = geocode_bounding_box_new (top, bottom, left, right);
+            geocode_place_set_bounding_box (place, bbox);
+            g_object_unref (bbox);
+        }
+
         /* Nominatim doesn't give us street addresses as such */
         street = g_hash_table_lookup (ht, "road");
         building = g_hash_table_lookup (ht, "house_number");
diff --git a/geocode-glib/geocode-reverse.c b/geocode-glib/geocode-reverse.c
index 71917bd..23503d5 100644
--- a/geocode-glib/geocode-reverse.c
+++ b/geocode-glib/geocode-reverse.c
@@ -128,13 +128,15 @@ _geocode_read_nominatim_attributes (JsonReader *reader,
        members = json_reader_list_members (reader);
 
        for (i = 0; members[i] != NULL; i++) {
-                const char *value;
+                const char *value = NULL;
 
                 json_reader_read_member (reader, members[i]);
 
-                value = json_reader_get_string_value (reader);
-                if (value && *value == '\0')
-                        value = NULL;
+                if (json_reader_is_value (reader)) {
+                        value = json_reader_get_string_value (reader);
+                        if (value && *value == '\0')
+                                value = NULL;
+                }
 
                 if (value != NULL) {
                         g_hash_table_insert (ht, g_strdup (members[i]), g_strdup (value));
@@ -152,8 +154,29 @@ _geocode_read_nominatim_attributes (JsonReader *reader,
                                 char *name = g_strdup_printf (_("%s %s"), house_number, value);
                                 g_hash_table_insert (ht, g_strdup ("name"), name);
                         }
+                } else if (g_strcmp0 (members[i], "boundingbox") == 0) {
+                        const char *bbox_val;
+
+                        json_reader_read_element (reader, 0);
+                        bbox_val = json_reader_get_string_value (reader);
+                        g_hash_table_insert(ht, g_strdup ("boundingbox-bottom"), g_strdup (bbox_val));
+                        json_reader_end_element (reader);
+
+                        json_reader_read_element (reader, 1);
+                        bbox_val = json_reader_get_string_value (reader);
+                        g_hash_table_insert(ht, g_strdup ("boundingbox-top"), g_strdup (bbox_val));
+                        json_reader_end_element (reader);
+
+                        json_reader_read_element (reader, 2);
+                        bbox_val = json_reader_get_string_value (reader);
+                        g_hash_table_insert(ht, g_strdup ("boundingbox-left"), g_strdup (bbox_val));
+                        json_reader_end_element (reader);
+
+                        json_reader_read_element (reader, 3);
+                        bbox_val = json_reader_get_string_value (reader);
+                        g_hash_table_insert(ht, g_strdup ("boundingbox-right"), g_strdup (bbox_val));
+                        json_reader_end_element (reader);
                 }
-
                 json_reader_end_member (reader);
         }
 
diff --git a/geocode-glib/test-gcglib.c b/geocode-glib/test-gcglib.c
index 9558787..d16f124 100644
--- a/geocode-glib/test-gcglib.c
+++ b/geocode-glib/test-gcglib.c
@@ -84,6 +84,25 @@ got_geocode_search_cb (GObject *source_object,
        exit (0);
 }
 
+static gboolean
+bbox_includes_location (GeocodeBoundingBox *bbox,
+                       GeocodeLocation *loc)
+{
+       if (geocode_bounding_box_get_left (bbox) > geocode_location_get_longitude (loc))
+               return FALSE;
+
+       if (geocode_bounding_box_get_right (bbox) < geocode_location_get_longitude (loc))
+               return FALSE;
+
+       if (geocode_bounding_box_get_bottom (bbox) > geocode_location_get_latitude (loc))
+               return FALSE;
+
+       if (geocode_bounding_box_get_top (bbox) < geocode_location_get_latitude (loc))
+               return FALSE;
+
+       return TRUE;
+}
+
 static void
 test_rev (void)
 {
@@ -274,6 +293,7 @@ test_search_lat_long (void)
        GList *res;
        GeocodePlace *place;
        GeocodeLocation *loc;
+       GeocodeBoundingBox *bbox;
 
        object = geocode_forward_new_for_string ("Santa María del Río");
        res = geocode_forward_search (object, &error);
@@ -289,8 +309,12 @@ test_search_lat_long (void)
        loc = geocode_place_get_location (place);
        g_assert (loc != NULL);
 
+       bbox = geocode_place_get_bounding_box (place);
+       g_assert (bbox != NULL);
+
        g_assert_cmpfloat (geocode_location_get_latitude (loc) - 21.8021297, <, 0.000001);
        g_assert_cmpfloat (geocode_location_get_longitude (loc) - -100.7374556, <, 0.000001);
+       g_assert (bbox_includes_location (bbox, geocode_place_get_location (place)));
        g_assert_cmpstr (geocode_place_get_name (place), ==, "Santa Maria Del Rio, Mexico");
        g_assert_cmpstr (geocode_place_get_town (place), ==, "Santa Maria Del Rio");
        g_assert_cmpstr (geocode_place_get_state (place), ==, "San Luis Potosi");


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