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

Proposed by Michael Terry on 2016-02-24
Status: Rejected
Rejected by: Michael Terry on 2016-02-26
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 2016-02-24 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 on 2016-02-25

Expose latitude/longitude too

Unmerged commits

0fe003c... by Michael Terry on 2016-02-25

Expose latitude/longitude too

c0ea0eb... by Michael Terry on 2016-02-24

Expose country codes

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/geonames-mkdb.c b/src/geonames-mkdb.c
2index 43466e2..23c1eb9 100644
3--- a/src/geonames-mkdb.c
4+++ b/src/geonames-mkdb.c
5@@ -139,12 +139,15 @@ handle_city_line (gchar **fields,
6 if (country == NULL)
7 return;
8
9- g_variant_builder_add (&data->builder, "(@s@s@s@su)",
10+ g_variant_builder_add (&data->builder, "(@s@s@s@su@sdd)",
11 variant_new_normalize_string (fields[CITIES_NAME]),
12 variant_new_normalize_string (admin1),
13 variant_new_normalize_string (country),
14 variant_new_normalize_string (fields[CITIES_TIMEZONE]),
15- strtoul (fields[CITIES_POPULATION], NULL, 10));
16+ strtoul (fields[CITIES_POPULATION], NULL, 10),
17+ variant_new_normalize_string (fields[CITIES_COUNTRY_CODE]),
18+ g_ascii_strtod (fields[CITIES_LATITUDE], NULL),
19+ g_ascii_strtod (fields[CITIES_LONGITUDE], NULL));
20 }
21
22 static gboolean
23@@ -213,7 +216,7 @@ main (int argc, char **argv)
24
25 data.admin1 = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
26 data.countries = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
27- g_variant_builder_init (&data.builder, G_VARIANT_TYPE ("a(ssssu)"));
28+ g_variant_builder_init (&data.builder, G_VARIANT_TYPE ("a(ssssusdd)"));
29
30 if (!parse_geo_names_file (admin1_file, 4, handle_admin1_line, data.admin1, &error))
31 {
32diff --git a/src/geonames-query.c b/src/geonames-query.c
33index a956f55..b3e4df9 100644
34--- a/src/geonames-query.c
35+++ b/src/geonames-query.c
36@@ -126,7 +126,7 @@ geonames_query_cities_db (GVariant *db,
37 gsize i;
38 GArray *indices;
39
40- g_return_val_if_fail (g_variant_is_of_type (db, G_VARIANT_TYPE ("a(ssssu)")), NULL);
41+ g_return_val_if_fail (g_variant_is_of_type (db, G_VARIANT_TYPE ("a(ssssusdd)")), NULL);
42 g_return_val_if_fail (query != NULL, NULL);
43
44 query_tokens = g_str_tokenize_and_fold (query, NULL, NULL);
45@@ -139,7 +139,7 @@ geonames_query_cities_db (GVariant *db,
46 guint population;
47 gdouble weight;
48
49- g_variant_get_child (db, i, "(&s&s&s&su)", &name, NULL, NULL, NULL, &population);
50+ g_variant_get_child (db, i, "(&s&s&s&su&sdd)", &name, NULL, NULL, NULL, &population, NULL, NULL, NULL);
51
52 weight = match_query (query_tokens, name);
53 weight *= (gdouble) CLAMP (population, 1, 1000000) / 1000000;
54diff --git a/src/geonames.c b/src/geonames.c
55index e7a30ee..f23379b 100644
56--- a/src/geonames.c
57+++ b/src/geonames.c
58@@ -43,7 +43,7 @@ ensure_geonames_data (void)
59 data = g_resources_lookup_data ("/com/ubuntu/geonames/cities.compiled", G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
60 g_assert (data);
61
62- v = g_variant_new_from_bytes (G_VARIANT_TYPE ("a(ssssu)"), data, TRUE);
63+ v = g_variant_new_from_bytes (G_VARIANT_TYPE ("a(ssssusdd)"), data, TRUE);
64
65 g_once_init_leave (&geonames_data, v);
66 }
67@@ -270,6 +270,22 @@ geonames_city_get_country (GeonamesCity *city)
68 }
69
70 /**
71+ * geonames_city_get_country_code:
72+ * @city: a #GeonamesCity
73+ *
74+ * Returns: the ISO-3166 two-letter country code of @city
75+ */
76+const gchar *
77+geonames_city_get_country_code (GeonamesCity *city)
78+{
79+ const gchar *country_code;
80+
81+ g_variant_get_child (city, 5, "&s", &country_code);
82+
83+ return country_code;
84+}
85+
86+/**
87 * geonames_city_get_timezone:
88 * @city: a #GeonamesCity
89 *
90@@ -284,3 +300,35 @@ geonames_city_get_timezone (GeonamesCity *city)
91
92 return timezone;
93 }
94+
95+/**
96+ * geonames_city_get_latitude:
97+ * @city: a #GeonamesCity
98+ *
99+ * Returns: the latitude of @city
100+ */
101+double
102+geonames_city_get_latitude (GeonamesCity *city)
103+{
104+ double latitude;
105+
106+ g_variant_get_child (city, 6, "d", &latitude);
107+
108+ return latitude;
109+}
110+
111+/**
112+ * geonames_city_get_longitude:
113+ * @city: a #GeonamesCity
114+ *
115+ * Returns: the longitude of @city
116+ */
117+double
118+geonames_city_get_longitude (GeonamesCity *city)
119+{
120+ double longitude;
121+
122+ g_variant_get_child (city, 7, "d", &longitude);
123+
124+ return longitude;
125+}
126diff --git a/src/geonames.h b/src/geonames.h
127index f1a474b..c90da6d 100644
128--- a/src/geonames.h
129+++ b/src/geonames.h
130@@ -76,8 +76,17 @@ _GEONAMES_EXPORT
131 const gchar * geonames_city_get_country (GeonamesCity *city);
132
133 _GEONAMES_EXPORT
134+const gchar * geonames_city_get_country_code (GeonamesCity *city);
135+
136+_GEONAMES_EXPORT
137 const gchar * geonames_city_get_timezone (GeonamesCity *city);
138
139-G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeonamesCity, geonames_city_free);
140+_GEONAMES_EXPORT
141+double geonames_city_get_latitude (GeonamesCity *city);
142+
143+_GEONAMES_EXPORT
144+double geonames_city_get_longitude (GeonamesCity *city);
145+
146+G_DEFINE_AUTOPTR_CLEANUP_FUNC (GeonamesCity, geonames_city_free)
147
148 #endif
149diff --git a/tests/test-geonames.c b/tests/test-geonames.c
150index 2496751..705533d 100644
151--- a/tests/test-geonames.c
152+++ b/tests/test-geonames.c
153@@ -24,7 +24,10 @@
154 static void
155 assert_first (const gchar *query,
156 const gchar *expected_city,
157- const gchar *expected_country)
158+ const gchar *expected_country,
159+ const gchar *expected_country_code,
160+ double expected_latitude,
161+ double expected_longitude)
162 {
163 g_autofree gint *indices;
164 guint len;
165@@ -39,17 +42,20 @@ assert_first (const gchar *query,
166
167 g_assert_cmpstr (geonames_city_get_name (city), ==, expected_city);
168 g_assert_cmpstr (geonames_city_get_country (city), ==, expected_country);
169+ g_assert_cmpstr (geonames_city_get_country_code (city), ==, expected_country_code);
170+ g_assert_cmpfloat (geonames_city_get_latitude (city), ==, expected_latitude);
171+ g_assert_cmpfloat (geonames_city_get_longitude (city), ==, expected_longitude);
172 }
173
174 static void
175 test_common_cities (void)
176 {
177- assert_first ("berlin", "Berlin", "Germany");
178- assert_first ("new york", "New York City", "United States");
179- assert_first ("san fran", "San Francisco", "United States");
180- assert_first ("amster", "Amsterdam", "Netherlands");
181- assert_first ("montreal", "Montréal", "Canada");
182- assert_first ("montréal", "Montréal", "Canada");
183+ assert_first ("berlin", "Berlin", "Germany", "DE", 52.52437, 13.41053);
184+ assert_first ("new york", "New York City", "United States", "US", 40.71427, -74.00597);
185+ assert_first ("san fran", "San Francisco", "United States", "US", 37.77493, -122.41942);
186+ assert_first ("amster", "Amsterdam", "Netherlands", "NL", 52.37403, 4.88969);
187+ assert_first ("montreal", "Montréal", "Canada", "CA", 45.50884, -73.58781);
188+ assert_first ("montréal", "Montréal", "Canada", "CA", 45.50884, -73.58781);
189 }
190
191 static void

Subscribers

People subscribed via source and target branches