Merge ~mterry/geonames/+git/country-code:master into geonames:master

Proposed by Michael Terry
Status: Rejected
Rejected by: Michael Terry
Proposed branch: ~mterry/geonames/+git/country-code:master
Merge into: geonames:master
Diff against target: 191 lines (+80/-14)
5 files modified
src/geonames-mkdb.c (+6/-3)
src/geonames-query.c (+2/-2)
src/geonames.c (+49/-1)
src/geonames.h (+10/-1)
tests/test-geonames.c (+13/-7)
Reviewer Review Type Date Requested Status
Geonames developers Pending
Review via email: mp+287106@code.launchpad.net

Commit message

Add geonames_city_get_country_code, geonames_city_get_latitude, and geonames_city_get_longitude.

Description of the change

Expose country code, latitude, and longitude. This would be useful for the unity8 welcome wizard, to preseed the timezone selector with all countries that match the selected locale, and to use in a visual map of cities.

To post a comment you must log in.
0fe003c... by Michael Terry

Expose latitude/longitude too

Revision history for this message
Michael Terry (mterry) wrote :

Unmerged commits

0fe003c... by Michael Terry

Expose latitude/longitude too

c0ea0eb... by Michael Terry

Expose country codes

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/geonames-mkdb.c b/src/geonames-mkdb.c
index 43466e2..23c1eb9 100644
--- a/src/geonames-mkdb.c
+++ b/src/geonames-mkdb.c
@@ -139,12 +139,15 @@ handle_city_line (gchar **fields,
139 if (country == NULL)139 if (country == NULL)
140 return;140 return;
141141
142 g_variant_builder_add (&data->builder, "(@s@s@s@su)",142 g_variant_builder_add (&data->builder, "(@s@s@s@su@sdd)",
143 variant_new_normalize_string (fields[CITIES_NAME]),143 variant_new_normalize_string (fields[CITIES_NAME]),
144 variant_new_normalize_string (admin1),144 variant_new_normalize_string (admin1),
145 variant_new_normalize_string (country),145 variant_new_normalize_string (country),
146 variant_new_normalize_string (fields[CITIES_TIMEZONE]),146 variant_new_normalize_string (fields[CITIES_TIMEZONE]),
147 strtoul (fields[CITIES_POPULATION], NULL, 10));147 strtoul (fields[CITIES_POPULATION], NULL, 10),
148 variant_new_normalize_string (fields[CITIES_COUNTRY_CODE]),
149 g_ascii_strtod (fields[CITIES_LATITUDE], NULL),
150 g_ascii_strtod (fields[CITIES_LONGITUDE], NULL));
148}151}
149152
150static gboolean153static gboolean
@@ -213,7 +216,7 @@ main (int argc, char **argv)
213216
214 data.admin1 = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);217 data.admin1 = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
215 data.countries = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);218 data.countries = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
216 g_variant_builder_init (&data.builder, G_VARIANT_TYPE ("a(ssssu)"));219 g_variant_builder_init (&data.builder, G_VARIANT_TYPE ("a(ssssusdd)"));
217220
218 if (!parse_geo_names_file (admin1_file, 4, handle_admin1_line, data.admin1, &error))221 if (!parse_geo_names_file (admin1_file, 4, handle_admin1_line, data.admin1, &error))
219 {222 {
diff --git a/src/geonames-query.c b/src/geonames-query.c
index a956f55..b3e4df9 100644
--- a/src/geonames-query.c
+++ b/src/geonames-query.c
@@ -126,7 +126,7 @@ geonames_query_cities_db (GVariant *db,
126 gsize i;126 gsize i;
127 GArray *indices;127 GArray *indices;
128128
129 g_return_val_if_fail (g_variant_is_of_type (db, G_VARIANT_TYPE ("a(ssssu)")), NULL);129 g_return_val_if_fail (g_variant_is_of_type (db, G_VARIANT_TYPE ("a(ssssusdd)")), NULL);
130 g_return_val_if_fail (query != NULL, NULL);130 g_return_val_if_fail (query != NULL, NULL);
131131
132 query_tokens = g_str_tokenize_and_fold (query, NULL, NULL);132 query_tokens = g_str_tokenize_and_fold (query, NULL, NULL);
@@ -139,7 +139,7 @@ geonames_query_cities_db (GVariant *db,
139 guint population;139 guint population;
140 gdouble weight;140 gdouble weight;
141141
142 g_variant_get_child (db, i, "(&s&s&s&su)", &name, NULL, NULL, NULL, &population);142 g_variant_get_child (db, i, "(&s&s&s&su&sdd)", &name, NULL, NULL, NULL, &population, NULL, NULL, NULL);
143143
144 weight = match_query (query_tokens, name);144 weight = match_query (query_tokens, name);
145 weight *= (gdouble) CLAMP (population, 1, 1000000) / 1000000;145 weight *= (gdouble) CLAMP (population, 1, 1000000) / 1000000;
diff --git a/src/geonames.c b/src/geonames.c
index e7a30ee..f23379b 100644
--- a/src/geonames.c
+++ b/src/geonames.c
@@ -43,7 +43,7 @@ ensure_geonames_data (void)
43 data = g_resources_lookup_data ("/com/ubuntu/geonames/cities.compiled", G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);43 data = g_resources_lookup_data ("/com/ubuntu/geonames/cities.compiled", G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
44 g_assert (data);44 g_assert (data);
4545
46 v = g_variant_new_from_bytes (G_VARIANT_TYPE ("a(ssssu)"), data, TRUE);46 v = g_variant_new_from_bytes (G_VARIANT_TYPE ("a(ssssusdd)"), data, TRUE);
4747
48 g_once_init_leave (&geonames_data, v);48 g_once_init_leave (&geonames_data, v);
49 }49 }
@@ -270,6 +270,22 @@ geonames_city_get_country (GeonamesCity *city)
270}270}
271271
272/**272/**
273 * geonames_city_get_country_code:
274 * @city: a #GeonamesCity
275 *
276 * Returns: the ISO-3166 two-letter country code of @city
277 */
278const gchar *
279geonames_city_get_country_code (GeonamesCity *city)
280{
281 const gchar *country_code;
282
283 g_variant_get_child (city, 5, "&s", &country_code);
284
285 return country_code;
286}
287
288/**
273 * geonames_city_get_timezone:289 * geonames_city_get_timezone:
274 * @city: a #GeonamesCity290 * @city: a #GeonamesCity
275 *291 *
@@ -284,3 +300,35 @@ geonames_city_get_timezone (GeonamesCity *city)
284300
285 return timezone;301 return timezone;
286}302}
303
304/**
305 * geonames_city_get_latitude:
306 * @city: a #GeonamesCity
307 *
308 * Returns: the latitude of @city
309 */
310double
311geonames_city_get_latitude (GeonamesCity *city)
312{
313 double latitude;
314
315 g_variant_get_child (city, 6, "d", &latitude);
316
317 return latitude;
318}
319
320/**
321 * geonames_city_get_longitude:
322 * @city: a #GeonamesCity
323 *
324 * Returns: the longitude of @city
325 */
326double
327geonames_city_get_longitude (GeonamesCity *city)
328{
329 double longitude;
330
331 g_variant_get_child (city, 7, "d", &longitude);
332
333 return longitude;
334}
diff --git a/src/geonames.h b/src/geonames.h
index f1a474b..c90da6d 100644
--- a/src/geonames.h
+++ b/src/geonames.h
@@ -76,8 +76,17 @@ _GEONAMES_EXPORT
76const gchar * geonames_city_get_country (GeonamesCity *city);76const gchar * geonames_city_get_country (GeonamesCity *city);
7777
78_GEONAMES_EXPORT78_GEONAMES_EXPORT
79const gchar * geonames_city_get_country_code (GeonamesCity *city);
80
81_GEONAMES_EXPORT
79const gchar * geonames_city_get_timezone (GeonamesCity *city);82const gchar * geonames_city_get_timezone (GeonamesCity *city);
8083
81G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeonamesCity, geonames_city_free);84_GEONAMES_EXPORT
85double geonames_city_get_latitude (GeonamesCity *city);
86
87_GEONAMES_EXPORT
88double geonames_city_get_longitude (GeonamesCity *city);
89
90G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeonamesCity, geonames_city_free)
8291
83#endif92#endif
diff --git a/tests/test-geonames.c b/tests/test-geonames.c
index 2496751..705533d 100644
--- a/tests/test-geonames.c
+++ b/tests/test-geonames.c
@@ -24,7 +24,10 @@
24static void24static void
25assert_first (const gchar *query,25assert_first (const gchar *query,
26 const gchar *expected_city,26 const gchar *expected_city,
27 const gchar *expected_country)27 const gchar *expected_country,
28 const gchar *expected_country_code,
29 double expected_latitude,
30 double expected_longitude)
28{31{
29 g_autofree gint *indices;32 g_autofree gint *indices;
30 guint len;33 guint len;
@@ -39,17 +42,20 @@ assert_first (const gchar *query,
3942
40 g_assert_cmpstr (geonames_city_get_name (city), ==, expected_city);43 g_assert_cmpstr (geonames_city_get_name (city), ==, expected_city);
41 g_assert_cmpstr (geonames_city_get_country (city), ==, expected_country);44 g_assert_cmpstr (geonames_city_get_country (city), ==, expected_country);
45 g_assert_cmpstr (geonames_city_get_country_code (city), ==, expected_country_code);
46 g_assert_cmpfloat (geonames_city_get_latitude (city), ==, expected_latitude);
47 g_assert_cmpfloat (geonames_city_get_longitude (city), ==, expected_longitude);
42}48}
4349
44static void50static void
45test_common_cities (void)51test_common_cities (void)
46{52{
47 assert_first ("berlin", "Berlin", "Germany");53 assert_first ("berlin", "Berlin", "Germany", "DE", 52.52437, 13.41053);
48 assert_first ("new york", "New York City", "United States");54 assert_first ("new york", "New York City", "United States", "US", 40.71427, -74.00597);
49 assert_first ("san fran", "San Francisco", "United States");55 assert_first ("san fran", "San Francisco", "United States", "US", 37.77493, -122.41942);
50 assert_first ("amster", "Amsterdam", "Netherlands");56 assert_first ("amster", "Amsterdam", "Netherlands", "NL", 52.37403, 4.88969);
51 assert_first ("montreal", "Montréal", "Canada");57 assert_first ("montreal", "Montréal", "Canada", "CA", 45.50884, -73.58781);
52 assert_first ("montréal", "Montréal", "Canada");58 assert_first ("montréal", "Montréal", "Canada", "CA", 45.50884, -73.58781);
53}59}
5460
55static void61static void

Subscribers

People subscribed via source and target branches