[geocode-glib] lib: Make the rev geocoding results use XEP attributes



commit 8ca3fcde2eb7d3c9da95da44889287a883f67962
Author: Bastien Nocera <hadess hadess net>
Date:   Sun Nov 25 20:48:17 2012 +0100

    lib: Make the rev geocoding results use XEP attributes
    
    Translate all the attributes we can to XEP attributes, and leave
    the others untouched (for debugging purposes, or because there is
    knowledge of the web service, eg. Place Finder returns timezone and
    airport information in some cases).

 TODO                           |    1 -
 geocode-glib/geocode-reverse.c |   52 ++++++++++++++++++++++++++++++++++++++-
 geocode-glib/test-gcglib.c     |    4 +-
 3 files changed, 52 insertions(+), 5 deletions(-)
---
diff --git a/TODO b/TODO
index 967acd3..3e19882 100644
--- a/TODO
+++ b/TODO
@@ -2,7 +2,6 @@
 - Write examples
 
 - Make geocode_foward_new_for_params() output warnings if a key is unknown to us
-- Translate the parameter names from the reverse geocoding to not be web service specific
 - Create better descriptions from the search results, and fix Paris test case for it
 - Add test case for search json parsing
 - Make answer count a property and immutable if XEP params are passed
diff --git a/geocode-glib/geocode-reverse.c b/geocode-glib/geocode-reverse.c
index 31badd6..9bcccb5 100644
--- a/geocode-glib/geocode-reverse.c
+++ b/geocode-glib/geocode-reverse.c
@@ -101,6 +101,47 @@ geocode_reverse_new_for_location (GeocodeLocation *location)
 	return object;
 }
 
+static struct {
+	const char *pf_attr;
+	const char *xep_attr;
+} attrs_map[] = {
+	{ "longitude", "longitude" },
+	{ "latitude", "latitude" },
+	{ "offsetlat", NULL },
+	{ "offsetlon", NULL },
+	{ "name", "description" },
+	{ "line1", "building" },
+	{ "line2", NULL },
+	{ "line3", NULL },
+	{ "line4", NULL },
+	{ "street", "street" },
+	{ "postal", "postalcode" },
+	{ "neighborhood", "area" },
+	{ "city", "locality" },
+	{ "county", NULL },
+	{ "state", "region" },
+	{ "country", "country" },
+	{ "countrycode", "countrycode" },
+	{ "countycode", NULL },
+	{ "timezone", NULL },
+	{ "uzip", NULL },
+};
+
+static const char *
+pf_to_xep (const char *attr)
+{
+	guint i;
+
+	for (i = 0; i < G_N_ELEMENTS (attrs_map); i++) {
+		if (g_str_equal (attr, attrs_map[i].pf_attr))
+			return attrs_map[i].xep_attr;
+	}
+
+	g_debug ("Can't convert unknown attribute '%s'", attr);
+
+	return NULL;
+}
+
 GHashTable *
 _geocode_parse_resolve_json (const char *contents,
 			     GError    **error)
@@ -222,8 +263,15 @@ _geocode_parse_resolve_json (const char *contents,
 		if (value && *value == '\0')
 			value = NULL;
 
-		if (value != NULL)
-			g_hash_table_insert (ret, g_strdup (members[i]), g_strdup (value));
+		if (value != NULL) {
+			const char *xep_attr;
+
+			xep_attr = pf_to_xep (members[i]);
+			if (xep_attr != NULL)
+				g_hash_table_insert (ret, g_strdup (xep_attr), g_strdup (value));
+			else
+				g_hash_table_insert (ret, g_strdup (members[i]), g_strdup (value));
+		}
 		json_reader_end_member (reader);
 	}
 	g_strfreev (members);
diff --git a/geocode-glib/test-gcglib.c b/geocode-glib/test-gcglib.c
index 808f07f..0348beb 100644
--- a/geocode-glib/test-gcglib.c
+++ b/geocode-glib/test-gcglib.c
@@ -101,7 +101,7 @@ test_rev (void)
 	g_assert (ht != NULL);
 	g_object_unref (rev);
 
-	g_assert (g_strcmp0 (g_hash_table_lookup (ht, "neighborhood"), "Onslow Village") == 0);
+	g_assert (g_strcmp0 (g_hash_table_lookup (ht, "area"), "Onslow Village") == 0);
 
 	g_print ("Got geocode answer:\n");
 	g_hash_table_foreach (ht, (GHFunc) print_res, NULL);
@@ -325,7 +325,7 @@ test_resolve_json (void)
 		const char *key;
 		const char *value;
 	} tests[] = {
-		{ "{\"ResultSet\":{\"version\":\"1.0\",\"Error\":0,\"ErrorMessage\":\"No error\",\"Locale\":\"us_US\",\"Quality\":99,\"Found\":1,\"Results\":[{\"quality\":99,\"latitude\":\"51.237100\",\"longitude\":\"-0.589669\",\"offsetlat\":\"51.237100\",\"offsetlon\":\"-0.589669\",\"radius\":500,\"name\":\"51.2371, -0.589669\",\"line1\":\"9 Old Palace Road\",\"line2\":\"Guildford\",\"line3\":\"GU2 7\",\"line4\":\"United Kingdom\",\"house\":\"9\",\"street\":\"Old Palace Road\",\"xstreet\":\"\",\"unittype\":\"\",\"unit\":\"\",\"postal\":\"GU2 7\",\"neighborhood\":\"Onslow Village\",\"city\":\"Guildford\",\"county\":\"Surrey\",\"state\":\"England\",\"country\":\"United Kingdom\",\"countrycode\":\"GB\",\"statecode\":\"ENG\",\"countycode\":\"SRY\",\"timezone\":\"Europe\\/London\",\"hash\":\"\",\"woeid\":26347368,\"woetype\":11,\"uzip\":\"GU2 7\",\"airport\":\"LHR\"}]}}", NULL, "neighborhood", "Onslow Village" },
+		{ "{\"ResultSet\":{\"version\":\"1.0\",\"Error\":0,\"ErrorMessage\":\"No error\",\"Locale\":\"us_US\",\"Quality\":99,\"Found\":1,\"Results\":[{\"quality\":99,\"latitude\":\"51.237100\",\"longitude\":\"-0.589669\",\"offsetlat\":\"51.237100\",\"offsetlon\":\"-0.589669\",\"radius\":500,\"name\":\"51.2371, -0.589669\",\"line1\":\"9 Old Palace Road\",\"line2\":\"Guildford\",\"line3\":\"GU2 7\",\"line4\":\"United Kingdom\",\"house\":\"9\",\"street\":\"Old Palace Road\",\"xstreet\":\"\",\"unittype\":\"\",\"unit\":\"\",\"postal\":\"GU2 7\",\"neighborhood\":\"Onslow Village\",\"city\":\"Guildford\",\"county\":\"Surrey\",\"state\":\"England\",\"country\":\"United Kingdom\",\"countrycode\":\"GB\",\"statecode\":\"ENG\",\"countycode\":\"SRY\",\"timezone\":\"Europe\\/London\",\"hash\":\"\",\"woeid\":26347368,\"woetype\":11,\"uzip\":\"GU2 7\",\"airport\":\"LHR\"}]}}", NULL, "area", "Onslow Village" },
 		{ "{\"ResultSet\":{\"version\":\"1.0\",\"Error\":107,\"ErrorMessage\":\"You gotz done!\",\"Locale\":\"us_US\",\"Quality\":99,\"Found\":1,\"Results\":[{\"quality\":99,\"latitude\":\"51.237100\",\"longitude\":\"-0.589669\",\"offsetlat\":\"51.237100\",\"offsetlon\":\"-0.589669\",\"radius\":500,\"name\":\"51.2371, -0.589669\",\"line1\":\"9 Old Palace Road\",\"line2\":\"Guildford\",\"line3\":\"GU2 7\",\"line4\":\"United Kingdom\",\"house\":\"9\",\"street\":\"Old Palace Road\",\"xstreet\":\"\",\"unittype\":\"\",\"unit\":\"\",\"postal\":\"GU2 7\",\"neighborhood\":\"Onslow Village\",\"city\":\"Guildford\",\"county\":\"Surrey\",\"state\":\"England\",\"country\":\"United Kingdom\",\"countrycode\":\"GB\",\"statecode\":\"ENG\",\"countycode\":\"SRY\",\"timezone\":\"Europe\\/London\",\"hash\":\"\",\"woeid\":26347368,\"woetype\":11,\"uzip\":\"GU2 7\",\"airport\":\"LHR\"}]}}", "You gotz done!" },
 		{ "{\"ResultSet\":{\"version\":\"1.0\",\"Error\":0,\"ErrorMessage\":\"No error\",\"Locale\":\"us_US\",\"Quality\":10,\"Found\":0}}", "No matches found for request", NULL, NULL },
 	};



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