From: Regina Obe Date: Sat, 28 Jan 2017 08:30:09 +0000 (+0000) Subject: Fix for 32-bit lwprint to support ST_AsLatLonText X-Git-Tag: 2.4.0alpha~179 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4a3c82022e22b6c3e3c181cdea9bd75ce8529f07;p=postgis Fix for 32-bit lwprint to support ST_AsLatLonText Closes #3688 for PostGIS 2.4 (trunk) git-svn-id: http://svn.osgeo.org/postgis/trunk@15296 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/liblwgeom/lwprint.c b/liblwgeom/lwprint.c index df3add67b..6557ec02a 100644 --- a/liblwgeom/lwprint.c +++ b/liblwgeom/lwprint.c @@ -106,6 +106,8 @@ static char * lwdouble_to_dms(double val, const char *pos_dir_symbol, const char int sec_dec_digits = 0; int sec_piece = -1; + int round_pow = 0; + int format_length = ((NULL == format) ? 0 : strlen(format)); char * result; @@ -313,8 +315,8 @@ static char * lwdouble_to_dms(double val, const char *pos_dir_symbol, const char degrees = val; if (min_digits > 0) { - degrees = (long)degrees; - minutes = fmod(val * 10, 10) * 6; + /* Break degrees to integer and use fraction for minutes */ + minutes = modf(val, °rees) * 60; } if (sec_digits > 0) { @@ -322,8 +324,17 @@ static char * lwdouble_to_dms(double val, const char *pos_dir_symbol, const char { lwerror("Bad format, cannot include seconds (SS.SSS) without including minutes (MM.MMM)."); } - minutes = (long)minutes; - seconds = (val - (degrees + (minutes / 60))) * 3600; + seconds = modf(minutes, &minutes) * 60; + if (sec_piece >= 0) + { + /* See if the formatted seconds round up to 60. If so, increment minutes and reset seconds. */ + round_pow = pow(10, sec_dec_digits); + if (floorf(seconds * round_pow) / round_pow >= 60) + { + minutes += 1; + seconds = 0; + } + } } /* Handle the compass direction. If not using compass dir, display degrees as a positive/negative number. */