[postgis-tickets] r15296 - Fix for 32-bit lwprint to support ST_AsLatLonText

Regina Obe lr at pcorp.us
Sat Jan 28 00:30:09 PST 2017


Author: robe
Date: 2017-01-28 00:30:09 -0800 (Sat, 28 Jan 2017)
New Revision: 15296

Modified:
   trunk/liblwgeom/lwprint.c
Log:
Fix for 32-bit lwprint to support ST_AsLatLonText
Closes #3688 for PostGIS 2.4 (trunk)

Modified: trunk/liblwgeom/lwprint.c
===================================================================
--- trunk/liblwgeom/lwprint.c	2017-01-28 04:10:49 UTC (rev 15295)
+++ trunk/liblwgeom/lwprint.c	2017-01-28 08:30:09 UTC (rev 15296)
@@ -106,6 +106,8 @@
 	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 @@
 	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 @@
 		{
 			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. */



More information about the postgis-tickets mailing list