[geocode-glib/wip/place-details: 3/3] wip: geocode_forward_search returns places
- From: Zeeshan Ali Khattak <zeeshanak src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [geocode-glib/wip/place-details: 3/3] wip: geocode_forward_search returns places
- Date: Thu, 25 Apr 2013 01:48:25 +0000 (UTC)
commit b261a894195b3534cab75f21e22c22c754bc2356
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date: Thu Apr 25 04:45:10 2013 +0300
wip: geocode_forward_search returns places
geocode-glib/geocode-forward.c | 188 ++++++++++++------------------------
geocode-glib/geocode-glib.symbols | 3 +
geocode-glib/test-gcglib.c | 72 ++++++++++----
3 files changed, 117 insertions(+), 146 deletions(-)
---
diff --git a/geocode-glib/geocode-forward.c b/geocode-glib/geocode-forward.c
index 8be2dd5..a241d62 100644
--- a/geocode-glib/geocode-forward.c
+++ b/geocode-glib/geocode-forward.c
@@ -98,6 +98,8 @@ _geocode_parse_single_result_json (const char *contents,
GError **error)
{
GHashTable *ht;
+ GeocodePlace *place = NULL;
+ char *name;
GeocodeLocation *loc;
gdouble longitude;
gdouble latitude;
@@ -108,13 +110,17 @@ _geocode_parse_single_result_json (const char *contents,
longitude = g_ascii_strtod (g_hash_table_lookup (ht, "longitude"), NULL);
latitude = g_ascii_strtod (g_hash_table_lookup (ht, "latitude"), NULL);
+ name = g_hash_table_lookup (ht, "line2");
loc = geocode_location_new_with_description (longitude,
latitude,
GEOCODE_LOCATION_ACCURACY_UNKNOWN,
- g_hash_table_lookup (ht, "line2"));
+ name);
+ place = geocode_place_new_with_location (name, GEOCODE_PLACE_TYPE_UNKNOWN, loc);
+
+ g_object_unref (loc);
g_hash_table_destroy (ht);
- return g_list_append (NULL, loc);
+ return g_list_append (NULL, place);
}
static struct {
@@ -472,8 +478,8 @@ geocode_forward_search_async (GeocodeForward *forward,
*
* Finishes a forward geocoding operation. See geocode_forward_search_async().
*
- * Returns: (element-type GeocodeLocation) (transfer container): A list of
- * locations or %NULL in case of errors. Free the returned list with
+ * Returns: (element-type GeocodePlace) (transfer container): A list of
+ * places or %NULL in case of errors. Free the returned list with
* g_list_free() when done.
**/
GList *
@@ -556,64 +562,57 @@ end:
json_reader_end_member (reader);
}
-static gboolean
-node_free_func (GNode *node,
- gpointer user_data)
+/* Modified copy of gvir_config_genum_get_value from libvirt-glib */
+static int
+get_enum_value_by_nick (GType enum_type,
+ const char *nick,
+ gint default_value)
{
- /* Leaf nodes are GeocodeLocation objects
- * which we reuse for the results */
- if (G_NODE_IS_LEAF (node) == FALSE)
- g_free (node->data);
+ GEnumClass *enum_class;
+ GEnumValue *enum_value;
+
+ g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), default_value);
+ g_return_val_if_fail (nick != NULL, default_value);
- return FALSE;
+ enum_class = g_type_class_ref (enum_type);
+ enum_value = g_enum_get_value_by_nick (enum_class, nick);
+ g_type_class_unref (enum_class);
+
+ if (enum_value != NULL)
+ return enum_value->value;
+
+ g_print ("\n%s\n", nick);
+ g_return_val_if_reached (default_value);
}
-#define N_ATTRS 7
-static const char const *attributes[7] = {
- "country",
- "admin1",
- "admin2",
- "admin3",
- "postal",
- "placeTypeName",
- "locality1"
-};
+static GeocodePlaceType
+get_place_type_from_name (const char *name)
+{
+ GeocodePlaceType place_type;
+ char *type_name;
+
+ if (g_strcmp0 (name, "District") == 0)
+ return GEOCODE_PLACE_TYPE_COUNTY;
+
+ type_name = g_ascii_strdown (name, -1);
+ place_type = get_enum_value_by_nick (GEOCODE_TYPE_PLACE_TYPE,
+ type_name,
+ GEOCODE_PLACE_TYPE_UNKNOWN);
+ g_free (type_name);
+
+ return place_type;
+}
static void
-insert_place_into_tree (GNode *location_tree, GHashTable *ht)
+insert_place_into_list (GList **place_list, GHashTable *ht)
{
- GNode *start = location_tree, *child = NULL;
+ GeocodePlaceType place_type;
+ GeocodePlace *place = NULL;
GeocodeLocation *loc = NULL;
- char *attr_val = NULL;
char *name;
gdouble longitude, latitude;
- guint i;
- for (i = 0; i < G_N_ELEMENTS(attributes); i++) {
- attr_val = g_hash_table_lookup (ht, attributes[i]);
- if (!attr_val) {
- /* Add a dummy node if the attribute value is not
- * available for the place */
- child = g_node_insert_data (start, -1, NULL);
- } else {
- /* If the attr value (eg for country United States)
- * already exists, then keep on adding other attributes under that node. */
- child = g_node_first_child (start);
- while (child &&
- child->data &&
- g_ascii_strcasecmp (child->data, attr_val) != 0) {
- child = g_node_next_sibling (child);
- }
- if (!child) {
- /* create a new node */
- child = g_node_insert_data (start, -1, g_strdup (attr_val));
- }
- }
- start = child;
- }
-
- /* Get latitude and longitude and create GeocodeLocation object.
- * The leaf node of the tree is the GeocodeLocation object */
+ /* Get latitude and longitude and create GeocodeLocation object. */
longitude = g_ascii_strtod (g_hash_table_lookup (ht, "longitude"), NULL);
latitude = g_ascii_strtod (g_hash_table_lookup (ht, "latitude"), NULL);
name = g_hash_table_lookup (ht, "name");
@@ -623,64 +622,18 @@ insert_place_into_tree (GNode *location_tree, GHashTable *ht)
GEOCODE_LOCATION_ACCURACY_UNKNOWN,
name);
- g_node_insert_data (start, -1, loc);
-}
-static void
-make_location_list_from_tree (GNode *node,
- char **s_array,
- GList **location_list,
- int i)
-{
- GNode *child;
- gboolean add_attribute = FALSE;
-
- if (node == NULL)
- return;
-
- if (G_NODE_IS_LEAF (node)) {
- GPtrArray *rev_s_array;
- GeocodeLocation *loc;
- const char *name;
- char *description;
- int counter = 0;
-
- rev_s_array = g_ptr_array_new ();
-
- /* If leaf node, then add all the attributes in the s_array
- * and set it to the description of the loc object */
- loc = (GeocodeLocation *) node->data;
-
- name = geocode_location_get_description (loc);
-
- /* To print the attributes in a meaningful manner
- * reverse the s_array */
- g_ptr_array_add (rev_s_array, (gpointer) name);
- for (counter = 1; counter <= i; counter++)
- g_ptr_array_add (rev_s_array, s_array[i - counter]);
- g_ptr_array_add (rev_s_array, NULL);
- description = g_strjoinv (", ", (char **) rev_s_array->pdata);
- g_ptr_array_unref (rev_s_array);
+ place_type = get_place_type_from_name (g_hash_table_lookup (ht, "placeTypeName"));
+ place = geocode_place_new_with_location (name, place_type, loc);
- geocode_location_set_description (loc, description);
- g_free (description);
+ /* FIXME: We need to
+ * - fill-in other details about place too
+ * - combine attributes into one string for location description (e.g
+ * city, state, country)?
+ */
+ g_object_unref (loc);
- *location_list = g_list_prepend (*location_list, loc);
- } else {
- /* If there are other attributes with a different value,
- * add those attributes to the string to differentiate them */
- if (g_node_prev_sibling (node) ||
- g_node_next_sibling (node))
- add_attribute = TRUE;
-
- if (add_attribute) {
- s_array[i] = node->data;
- i++;
- }
- }
-
- for (child = node->children; child != NULL; child = child->next)
- make_location_list_from_tree (child, s_array, location_list, i);
+ *place_list = g_list_append (*place_list, place);
}
GList *
@@ -693,8 +646,6 @@ _geocode_parse_search_json (const char *contents,
JsonReader *reader;
const GError *err = NULL;
int num_places, i;
- GNode *location_tree;
- char *s_array[N_ATTRS];
ret = NULL;
@@ -716,8 +667,6 @@ _geocode_parse_search_json (const char *contents,
if (num_places < 0)
goto parse;
- location_tree = g_node_new (NULL);
-
for (i = 0; i < num_places; i++) {
GHashTable *ht;
char **members;
@@ -732,8 +681,8 @@ _geocode_parse_search_json (const char *contents,
for (j = 0; members != NULL && members[j] != NULL; j++)
insert_place_attr (ht, reader, members[j]);
- /* Populate the tree with place details */
- insert_place_into_tree (location_tree, ht);
+ /* Populate the list with place details */
+ insert_place_into_list (&ret, ht);
g_hash_table_destroy (ht);
g_strfreev (members);
@@ -741,17 +690,6 @@ _geocode_parse_search_json (const char *contents,
json_reader_end_element (reader);
}
- make_location_list_from_tree (location_tree, s_array, &ret, 0);
-
- g_node_traverse (location_tree,
- G_IN_ORDER,
- G_TRAVERSE_ALL,
- -1,
- (GNodeTraverseFunc) node_free_func,
- NULL);
-
- g_node_destroy (location_tree);
-
g_object_unref (parser);
g_object_unref (reader);
ret = g_list_reverse (ret);
@@ -773,8 +711,8 @@ parse:
* Gets the result of a forward geocoding
* query using a web service.
*
- * Returns: (element-type GeocodeLocation) (transfer container): A list of
- * locations or %NULL in case of errors. Free the returned list with
+ * Returns: (element-type GeocodePlace) (transfer container): A list of
+ * places or %NULL in case of errors. Free the returned list with
* g_list_free() when done.
**/
GList *
diff --git a/geocode-glib/geocode-glib.symbols b/geocode-glib/geocode-glib.symbols
index 0c382c9..2b33a4b 100644
--- a/geocode-glib/geocode-glib.symbols
+++ b/geocode-glib/geocode-glib.symbols
@@ -35,6 +35,9 @@ geocode_place_get_type
geocode_place_type_get_type
geocode_place_new
geocode_place_new_with_location
+geocode_place_get_name
+geocode_place_get_place_type
+geocode_place_get_location
geocode_place_set_street_address
geocode_place_get_street_address
geocode_place_set_postal_code
diff --git a/geocode-glib/test-gcglib.c b/geocode-glib/test-gcglib.c
index e90a1f7..6128c3f 100644
--- a/geocode-glib/test-gcglib.c
+++ b/geocode-glib/test-gcglib.c
@@ -22,6 +22,15 @@ print_loc (GeocodeLocation *loc)
}
static void
+print_place (GeocodePlace *place)
+{
+ /* For now just print the underlying location */
+ GeocodeLocation *loc = geocode_place_get_location (place);
+
+ print_loc (loc);
+}
+
+static void
print_res (const char *key,
const char *value,
gpointer data)
@@ -71,11 +80,11 @@ got_geocode_search_cb (GObject *source_object,
}
for (l = results; l != NULL; l = l->next) {
- GeocodeLocation *loc = l->data;
+ GeocodePlace *place = l->data;
g_print ("Got geocode search answer:\n");
- print_loc (loc);
- g_object_unref (loc);
+ print_place (place);
+ g_object_unref (place);
}
g_list_free (results);
@@ -129,6 +138,7 @@ test_xep (void)
GHashTable *tp;
GeocodeForward *object;
GList *res;
+ GeocodePlace *place;
GeocodeLocation *loc;
GError *error = NULL;
@@ -155,11 +165,13 @@ test_xep (void)
g_object_unref (object);
- loc = res->data;
+ place = res->data;
+ loc = geocode_place_get_location (place);
+ g_assert (loc != NULL);
g_assert_cmpfloat (geocode_location_get_latitude (loc), ==, -0.589669);
g_assert_cmpfloat (geocode_location_get_longitude (loc), ==, 51.237070);
- g_object_unref (loc);
+ g_object_unref (place);
g_list_free (res);
}
@@ -169,6 +181,7 @@ test_pub (void)
GeocodeForward *object;
GError *error = NULL;
GList *res;
+ GeocodePlace *place;
GeocodeLocation *loc;
object = geocode_forward_new_for_string ("9, old palace road, guildford, surrey");
@@ -183,12 +196,14 @@ test_pub (void)
g_object_unref (object);
g_assert_cmpint (g_list_length (res), ==, 1);
- loc = res->data;
+ place = res->data;
+ loc = geocode_place_get_location (place);
+ g_assert (loc != NULL);
g_assert_cmpfloat (geocode_location_get_latitude (loc), ==, -0.589669);
g_assert_cmpfloat (geocode_location_get_longitude (loc), ==, 51.237070);
- g_object_unref (loc);
+ g_object_unref (place);
g_list_free (res);
}
@@ -221,14 +236,16 @@ test_search (void)
got_france = FALSE;
got_texas = FALSE;
for (l = results; l != NULL; l = l->next) {
- GeocodeLocation *loc = l->data;
+ GeocodePlace *place = l->data;
+ g_assert (g_strcmp0 (geocode_place_get_name (place), "Paris") == 0);
- if (g_strcmp0 (geocode_location_get_description (loc), "Paris, France") == 0)
+ if (g_strcmp0 (geocode_place_get_country (place), "France") == 0)
got_france = TRUE;
- else if (g_strcmp0 (geocode_location_get_description (loc), "Paris, Texas, United States") ==
0)
+ else if (g_strcmp0 (geocode_place_get_state (place), "Texas") == 0 &&
+ g_strcmp0 (geocode_place_get_country (place), "United States") == 0)
got_texas = TRUE;
- g_object_unref (loc);
+ g_object_unref (place);
if (got_france && got_texas)
break;
@@ -248,6 +265,7 @@ test_search_lat_long (void)
GeocodeForward *object;
GError *error = NULL;
GList *res;
+ GeocodePlace *place;
GeocodeLocation *loc;
object = geocode_forward_new_for_string ("Santa María del Río");
@@ -259,7 +277,10 @@ test_search_lat_long (void)
g_assert (res != NULL);
g_object_unref (object);
- loc = res->data;
+ place = res->data;
+ loc = geocode_place_get_location (place);
+ g_assert (loc != NULL);
+
g_assert_cmpfloat (geocode_location_get_latitude (loc) - 21.800699, <, 0.000001);
g_assert_cmpfloat (geocode_location_get_longitude (loc) - -100.735626, <, 0.000001);
@@ -287,6 +308,7 @@ test_locale (void)
GeocodeForward *object;
GError *error = NULL;
GList *res;
+ GeocodePlace *place;
GeocodeLocation *loc;
char *old_locale;
@@ -303,11 +325,16 @@ test_locale (void)
g_assert (res != NULL);
g_object_unref (object);
- loc = res->data;
- g_assert_cmpstr (geocode_location_get_description (loc), ==, "Moskva, Rusko");
+ place = res->data;
+ g_assert_cmpstr (geocode_place_get_name (place), ==, "Moskva");
+ g_assert_cmpstr (geocode_place_get_country (place), ==, "Rusko");
+
+ loc = geocode_place_get_location (place);
+ g_assert (loc != NULL);
+
g_assert_cmpfloat (geocode_location_get_latitude (loc) - 55.756950, <, 0.000001);
g_assert_cmpfloat (geocode_location_get_longitude (loc) - 37.614971, <, 0.000001);
- print_loc (loc);
+ print_place (place);
g_list_free_full (res, (GDestroyNotify) g_object_unref);
@@ -322,9 +349,11 @@ test_locale (void)
g_assert (res != NULL);
g_object_unref (object);
- loc = res->data;
- g_assert_cmpstr (geocode_location_get_description (loc), ==, "Bonneville, Rhône-Alpes, France");
- print_loc (loc);
+ place = res->data;
+ g_assert_cmpstr (geocode_place_get_name (place), ==, "Bonneville");
+ g_assert_cmpstr (geocode_place_get_state (place), ==, "Rhône-Alpes");
+ g_assert_cmpstr (geocode_place_get_country (place), ==, "France");
+ print_place (place);
g_list_free_full (res, (GDestroyNotify) g_object_unref);
@@ -388,7 +417,7 @@ test_search_json (void)
GError *error = NULL;
GList *list;
char *contents;
- GeocodeLocation *loc;
+ GeocodePlace *place;
if (g_file_get_contents (TEST_SRCDIR "/geoplanet-rio.json",
&contents, NULL, &error) == FALSE) {
@@ -400,8 +429,9 @@ test_search_json (void)
g_assert (list != NULL);
g_assert_cmpint (g_list_length (list), ==, 10);
- loc = list->data;
- g_assert_cmpstr (geocode_location_get_description (loc), ==, "Rio de Janeiro, Brazil");
+ place = list->data;
+ g_assert_cmpstr (geocode_place_get_name (place), ==, "Rio de Janeiro");
+ g_assert_cmpstr (geocode_place_get_country (place), ==, "Brazil");
g_list_free_full (list, (GDestroyNotify) g_object_unref);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]