summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBastien Nocera <hadess@hadess.net>2021-01-07 12:46:34 +0100
committerBenjamin Berg <bberg@redhat.com>2021-03-25 23:07:30 +0100
commit321a58fbe08096ce96908e2b4ac245a710411757 (patch)
tree895421f31ad0b48e04c81ef76e4a03da91db8888
parent0af78743a01a6fb60bcac8ff32f847f41393c752 (diff)
downloadlibgweather-321a58fbe08096ce96908e2b4ac245a710411757.tar.gz
gweather: Only use 4 significant decimals for locations
1/1000th of a degree of longitude or latitude corresponds to around 100 meters. There's no reason for the weather to be any more precise than this. See https://www.thoughtco.com/degree-of-latitude-and-longitude-distance-4070616 Closes: #69
-rw-r--r--libgweather/gweather-private.c16
-rw-r--r--libgweather/gweather-private.h3
-rw-r--r--libgweather/test_libgweather.c16
-rw-r--r--libgweather/weather-iwin.c7
-rw-r--r--libgweather/weather-owm.c7
-rw-r--r--libgweather/weather-yrno.c7
6 files changed, 47 insertions, 9 deletions
diff --git a/libgweather/gweather-private.c b/libgweather/gweather-private.c
index 3f7fdce..0ccc21b 100644
--- a/libgweather/gweather-private.c
+++ b/libgweather/gweather-private.c
@@ -22,3 +22,19 @@
#include "gweather-private.h"
+/* sign, 3 digits, separator, 4 decimals, nul-char */
+#define DEGREES_STR_SIZE (1 + 3 + 1 + 4 + 1)
+
+char *
+_radians_to_degrees_str (gdouble radians)
+{
+ char *str;
+ double degrees;
+
+ str = g_malloc0 (DEGREES_STR_SIZE);
+ /* Max 4 decimals */
+ degrees = (double) ((int) (RADIANS_TO_DEGREES (radians) * 10000)) / 10000;
+ /* Too many digits */
+ g_return_val_if_fail (degrees <= 1000 || degrees >= -1000, NULL);
+ return g_ascii_formatd (str, G_ASCII_DTOSTR_BUF_SIZE, "%g", degrees);
+}
diff --git a/libgweather/gweather-private.h b/libgweather/gweather-private.h
index ddf0a41..be73768 100644
--- a/libgweather/gweather-private.h
+++ b/libgweather/gweather-private.h
@@ -167,6 +167,9 @@ struct _GWeatherInfoPrivate {
#define RADIANS_TO_DEGREES(rad) ((rad) * 180. / M_PI)
#define RADIANS_TO_HOURS(rad) ((rad) * 12. / M_PI)
+GWEATHER_EXTERN
+char *_radians_to_degrees_str (gdouble radians);
+
/*
* Planetary Mean Orbit and their progressions from J2000 are based on the
* values in http://ssd.jpl.nasa.gov/txt/aprx_pos_planets.pdf
diff --git a/libgweather/test_libgweather.c b/libgweather/test_libgweather.c
index 8a21911..e0e4b81 100644
--- a/libgweather/test_libgweather.c
+++ b/libgweather/test_libgweather.c
@@ -26,6 +26,7 @@
#include <gweather-version.h>
#include "gweather-location.h"
#include "gweather-weather.h"
+#include "gweather-private.h"
extern void _gweather_location_reset_world (void);
@@ -759,6 +760,20 @@ test_weather_loop_use_after_free (void)
}
static void
+test_radians_to_degrees_str (void)
+{
+ char long_version[G_ASCII_DTOSTR_BUF_SIZE];
+ g_autofree char *short_version = NULL;
+ double coord = 1.260765526077;
+
+ g_ascii_dtostr (long_version, G_ASCII_DTOSTR_BUF_SIZE, RADIANS_TO_DEGREES (coord));
+ short_version = _radians_to_degrees_str (coord);
+
+ g_assert_cmpint (strlen (long_version), >, strlen (short_version));
+ g_assert_cmpstr (short_version, ==, "72.2365");
+}
+
+static void
log_handler (const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer user_data)
{
g_print ("%s\n", message);
@@ -780,6 +795,7 @@ main (int argc, char *argv[])
FALSE);
set_gsettings ();
+ g_test_add_func ("/weather/radians-to-degrees_str", test_radians_to_degrees_str);
g_test_add_func ("/weather/named-timezones", test_named_timezones);
g_test_add_func ("/weather/named-timezones-deserialized", test_named_timezones_deserialized);
g_test_add_func ("/weather/no-code-serialize", test_no_code_serialize);
diff --git a/libgweather/weather-iwin.c b/libgweather/weather-iwin.c
index c3d414d..7102039 100644
--- a/libgweather/weather-iwin.c
+++ b/libgweather/weather-iwin.c
@@ -353,7 +353,8 @@ iwin_start_open (GWeatherInfo *info)
SoupMessage *msg;
struct tm tm;
time_t now;
- gchar latstr[G_ASCII_DTOSTR_BUF_SIZE], lonstr[G_ASCII_DTOSTR_BUF_SIZE];
+ g_autofree char *latstr = NULL;
+ g_autofree char *lonstr = NULL;
g_assert (info != NULL);
@@ -376,8 +377,8 @@ iwin_start_open (GWeatherInfo *info)
now = time (NULL);
localtime_r (&now, &tm);
- g_ascii_dtostr (latstr, sizeof(latstr), RADIANS_TO_DEGREES (loc->latitude));
- g_ascii_dtostr (lonstr, sizeof(lonstr), RADIANS_TO_DEGREES (loc->longitude));
+ latstr = _radians_to_degrees_str (loc->latitude);
+ lonstr = _radians_to_degrees_str (loc->longitude);
url = g_strdup_printf ("https://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?&lat=%s&lon=%s&format=24+hourly&startDate=%04d-%02d-%02d&numDays=7",
latstr, lonstr, 1900 + tm.tm_year, 1 + tm.tm_mon, tm.tm_mday);
g_debug ("iwin_start_open, requesting: %s", url);
diff --git a/libgweather/weather-owm.c b/libgweather/weather-owm.c
index a58950c..eb45f52 100644
--- a/libgweather/weather-owm.c
+++ b/libgweather/weather-owm.c
@@ -428,7 +428,8 @@ owm_start_open (GWeatherInfo *info)
gchar *url;
SoupMessage *message;
WeatherLocation *loc;
- gchar latstr[G_ASCII_DTOSTR_BUF_SIZE], lonstr[G_ASCII_DTOSTR_BUF_SIZE];
+ g_autofree char *latstr = NULL;
+ g_autofree char *lonstr = NULL;
priv = info->priv;
loc = &priv->location;
@@ -438,8 +439,8 @@ owm_start_open (GWeatherInfo *info)
/* see the description here: http://bugs.openweathermap.org/projects/api/wiki/Api_2_5_forecast */
- g_ascii_dtostr (latstr, sizeof(latstr), RADIANS_TO_DEGREES (loc->latitude));
- g_ascii_dtostr (lonstr, sizeof(lonstr), RADIANS_TO_DEGREES (loc->longitude));
+ latstr = _radians_to_degrees_str (loc->latitude);
+ lonstr = _radians_to_degrees_str (loc->longitude);
#define TEMPLATE_START "https://api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&mode=xml&units=metric"
#ifdef OWM_APIKEY
diff --git a/libgweather/weather-yrno.c b/libgweather/weather-yrno.c
index 8324121..7834076 100644
--- a/libgweather/weather-yrno.c
+++ b/libgweather/weather-yrno.c
@@ -435,7 +435,8 @@ yrno_start_open (GWeatherInfo *info)
gchar *url;
SoupMessage *message;
WeatherLocation *loc;
- gchar latstr[G_ASCII_DTOSTR_BUF_SIZE], lonstr[G_ASCII_DTOSTR_BUF_SIZE];
+ g_autofree char *latstr = NULL;
+ g_autofree char *lonstr = NULL;
priv = info->priv;
loc = &priv->location;
@@ -445,8 +446,8 @@ yrno_start_open (GWeatherInfo *info)
/* see the description here: https://api.met.no/ */
- g_ascii_dtostr (latstr, sizeof(latstr), RADIANS_TO_DEGREES (loc->latitude));
- g_ascii_dtostr (lonstr, sizeof(lonstr), RADIANS_TO_DEGREES (loc->longitude));
+ latstr = _radians_to_degrees_str (loc->latitude);
+ lonstr = _radians_to_degrees_str (loc->longitude);
url = g_strdup_printf("https://api.met.no/weatherapi/locationforecast/1.9/?lat=%s;lon=%s", latstr, lonstr);
g_debug ("yrno_start_open, requesting: %s", url);