# HG changeset patch # User jbe # Date 1581504254 -3600 # Node ID d06b066fb8ad5ee1206fe0f7388a36e91189737c # Parent 882b77aee653ea3025d879e5ab0602d566b09413 Created inline function for longitude normalization diff -r 882b77aee653 -r d06b066fb8ad latlon-v0010.c --- a/latlon-v0010.c Wed Feb 12 11:08:37 2020 +0100 +++ b/latlon-v0010.c Wed Feb 12 11:44:14 2020 +0100 @@ -265,6 +265,22 @@ return round(val * 1e12) / 1e12; } +/* normalize longitude to be between -180 and 180 */ +static inline double pgl_normalize(double lon, bool warn) { + if (lon < -180) { + if (warn) { + ereport(NOTICE, (errmsg("longitude west of 180th meridian normalized"))); + } + lon += 360 - trunc(lon / 360) * 360; + } else if (lon > 180) { + if (warn) { + ereport(NOTICE, (errmsg("longitude east of 180th meridian normalized"))); + } + lon -= 360 + trunc(lon / 360) * 360; + } + return lon; +} + /* compare two points */ /* (equality when same point on earth is described, otherwise an arbitrary linear order) */ @@ -1630,14 +1646,8 @@ ereport(WARNING, (errmsg("latitude exceeds north pole"))); lat = 90; } - /* check longitude bounds */ - if (lon < -180) { - ereport(NOTICE, (errmsg("longitude west of 180th meridian normalized"))); - lon += 360 - trunc(lon / 360) * 360; - } else if (lon > 180) { - ereport(NOTICE, (errmsg("longitude east of 180th meridian normalized"))); - lon -= 360 + trunc(lon / 360) * 360; - } + /* normalize longitude */ + lon = pgl_normalize(lon, true); /* store rounded latitude/longitude values for round-trip safety */ point->lat = pgl_round(lat); point->lon = pgl_round(lon); @@ -1802,10 +1812,8 @@ lon_max = 180; } else { /* normalize longitude bounds */ - if (lon_min < -180) lon_min += 360 - trunc(lon_min / 360) * 360; - else if (lon_min > 180) lon_min -= 360 + trunc(lon_min / 360) * 360; - if (lon_max < -180) lon_max += 360 - trunc(lon_max / 360) * 360; - else if (lon_max > 180) lon_max -= 360 + trunc(lon_max / 360) * 360; + lon_min = pgl_normalize(lon_min, false); + lon_max = pgl_normalize(lon_max, false); } /* store rounded latitude/longitude values for round-trip safety */ box->lat_min = pgl_round(lat_min);