]> granicus.if.org Git - postgis/commitdiff
Fix for 32-bit lwprint to support ST_AsLatLonText
authorRegina Obe <lr@pcorp.us>
Sat, 28 Jan 2017 08:30:09 +0000 (08:30 +0000)
committerRegina Obe <lr@pcorp.us>
Sat, 28 Jan 2017 08:30:09 +0000 (08:30 +0000)
Closes #3688 for PostGIS 2.4 (trunk)

git-svn-id: http://svn.osgeo.org/postgis/trunk@15296 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwprint.c

index df3add67be269154f5cc4b835b72a4904fadb099..6557ec02a81c219728d735ba0c0edaadf7a1b2d5 100644 (file)
@@ -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, &degrees) * 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. */