pgLatLon

changeset 70:d06b066fb8ad

Created inline function for longitude normalization
author jbe
date Wed Feb 12 11:44:14 2020 +0100 (6 weeks ago)
parents 882b77aee653
children 57ec3281fbb7
files latlon-v0010.c
line diff
     1.1 --- a/latlon-v0010.c	Wed Feb 12 11:08:37 2020 +0100
     1.2 +++ b/latlon-v0010.c	Wed Feb 12 11:44:14 2020 +0100
     1.3 @@ -265,6 +265,22 @@
     1.4    return round(val * 1e12) / 1e12;
     1.5  }
     1.6  
     1.7 +/* normalize longitude to be between -180 and 180 */
     1.8 +static inline double pgl_normalize(double lon, bool warn) {
     1.9 +  if (lon < -180) {
    1.10 +    if (warn) {
    1.11 +      ereport(NOTICE, (errmsg("longitude west of 180th meridian normalized")));
    1.12 +    }
    1.13 +    lon += 360 - trunc(lon / 360) * 360;
    1.14 +  } else if (lon > 180) {
    1.15 +    if (warn) {
    1.16 +      ereport(NOTICE, (errmsg("longitude east of 180th meridian normalized")));
    1.17 +    }
    1.18 +    lon -= 360 + trunc(lon / 360) * 360;
    1.19 +  }
    1.20 +  return lon;
    1.21 +}
    1.22 +
    1.23  /* compare two points */
    1.24  /* (equality when same point on earth is described, otherwise an arbitrary
    1.25     linear order) */
    1.26 @@ -1630,14 +1646,8 @@
    1.27      ereport(WARNING, (errmsg("latitude exceeds north pole")));
    1.28      lat = 90;
    1.29    }
    1.30 -  /* check longitude bounds */
    1.31 -  if (lon < -180) {
    1.32 -    ereport(NOTICE, (errmsg("longitude west of 180th meridian normalized")));
    1.33 -    lon += 360 - trunc(lon / 360) * 360;
    1.34 -  } else if (lon > 180) {
    1.35 -    ereport(NOTICE, (errmsg("longitude east of 180th meridian normalized")));
    1.36 -    lon -= 360 + trunc(lon / 360) * 360;
    1.37 -  }
    1.38 +  /* normalize longitude */
    1.39 +  lon = pgl_normalize(lon, true);
    1.40    /* store rounded latitude/longitude values for round-trip safety */
    1.41    point->lat = pgl_round(lat);
    1.42    point->lon = pgl_round(lon);
    1.43 @@ -1802,10 +1812,8 @@
    1.44      lon_max = 180;
    1.45    } else {
    1.46      /* normalize longitude bounds */
    1.47 -    if      (lon_min < -180) lon_min += 360 - trunc(lon_min / 360) * 360;
    1.48 -    else if (lon_min >  180) lon_min -= 360 + trunc(lon_min / 360) * 360;
    1.49 -    if      (lon_max < -180) lon_max += 360 - trunc(lon_max / 360) * 360;
    1.50 -    else if (lon_max >  180) lon_max -= 360 + trunc(lon_max / 360) * 360;
    1.51 +    lon_min = pgl_normalize(lon_min, false);
    1.52 +    lon_max = pgl_normalize(lon_max, false);
    1.53    }
    1.54    /* store rounded latitude/longitude values for round-trip safety */
    1.55    box->lat_min = pgl_round(lat_min);

Impressum / About Us