PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ ptarray_distance_spheroid()

static double ptarray_distance_spheroid ( const POINTARRAY pa1,
const POINTARRAY pa2,
const SPHEROID s,
double  tolerance,
int  check_intersection 
)
static

Definition at line 1841 of file lwgeodetic.c.

1842 {
1843  GEOGRAPHIC_EDGE e1, e2;
1844  GEOGRAPHIC_POINT g1, g2;
1845  GEOGRAPHIC_POINT nearest1, nearest2;
1846  POINT3D A1, A2, B1, B2;
1847  const POINT2D *p;
1848  double distance;
1849  uint32_t i, j;
1850  int use_sphere = (s->a == s->b ? 1 : 0);
1851 
1852  /* Make result really big, so that everything will be smaller than it */
1853  distance = FLT_MAX;
1854 
1855  /* Empty point arrays? Return negative */
1856  if ( pa1->npoints == 0 || pa2->npoints == 0 )
1857  return -1.0;
1858 
1859  /* Handle point/point case here */
1860  if ( pa1->npoints == 1 && pa2->npoints == 1 )
1861  {
1862  p = getPoint2d_cp(pa1, 0);
1863  geographic_point_init(p->x, p->y, &g1);
1864  p = getPoint2d_cp(pa2, 0);
1865  geographic_point_init(p->x, p->y, &g2);
1866  /* Sphere special case, axes equal */
1867  distance = s->radius * sphere_distance(&g1, &g2);
1868  if ( use_sphere )
1869  return distance;
1870  /* Below tolerance, actual distance isn't of interest */
1871  else if ( distance < 0.95 * tolerance )
1872  return distance;
1873  /* Close or greater than tolerance, get the real answer to be sure */
1874  else
1875  return spheroid_distance(&g1, &g2, s);
1876  }
1877 
1878  /* Handle point/line case here */
1879  if ( pa1->npoints == 1 || pa2->npoints == 1 )
1880  {
1881  /* Handle one/many case here */
1882  uint32_t i;
1883  const POINTARRAY *pa_one;
1884  const POINTARRAY *pa_many;
1885 
1886  if ( pa1->npoints == 1 )
1887  {
1888  pa_one = pa1;
1889  pa_many = pa2;
1890  }
1891  else
1892  {
1893  pa_one = pa2;
1894  pa_many = pa1;
1895  }
1896 
1897  /* Initialize our point */
1898  p = getPoint2d_cp(pa_one, 0);
1899  geographic_point_init(p->x, p->y, &g1);
1900 
1901  /* Initialize start of line */
1902  p = getPoint2d_cp(pa_many, 0);
1903  geographic_point_init(p->x, p->y, &(e1.start));
1904 
1905  /* Iterate through the edges in our line */
1906  for ( i = 1; i < pa_many->npoints; i++ )
1907  {
1908  double d;
1909  p = getPoint2d_cp(pa_many, i);
1910  geographic_point_init(p->x, p->y, &(e1.end));
1911  /* Get the spherical distance between point and edge */
1912  d = s->radius * edge_distance_to_point(&e1, &g1, &g2);
1913  /* New shortest distance! Record this distance / location */
1914  if ( d < distance )
1915  {
1916  distance = d;
1917  nearest2 = g2;
1918  }
1919  /* We've gotten closer than the tolerance... */
1920  if ( d <= tolerance )
1921  {
1922  /* Working on a sphere? The answer is correct, return */
1923  if ( use_sphere )
1924  {
1925  return d;
1926  }
1927  /* Far enough past the tolerance that the spheroid calculation won't change things */
1928  else if ( d <= tolerance * 0.95 )
1929  {
1930  return d;
1931  }
1932  /* On a spheroid and near the tolerance? Confirm that we are *actually* closer than tolerance */
1933  else
1934  {
1935  d = spheroid_distance(&g1, &nearest2, s);
1936  /* Yes, closer than tolerance, return! */
1937  if ( d <= tolerance )
1938  return d;
1939  }
1940  }
1941  e1.start = e1.end;
1942  }
1943 
1944  /* On sphere, return answer */
1945  if ( use_sphere )
1946  return distance;
1947  /* On spheroid, calculate final answer based on closest approach */
1948  else
1949  return spheroid_distance(&g1, &nearest2, s);
1950 
1951  }
1952 
1953  /* Initialize start of line 1 */
1954  p = getPoint2d_cp(pa1, 0);
1955  geographic_point_init(p->x, p->y, &(e1.start));
1956  geog2cart(&(e1.start), &A1);
1957 
1958 
1959  /* Handle line/line case */
1960  for ( i = 1; i < pa1->npoints; i++ )
1961  {
1962  p = getPoint2d_cp(pa1, i);
1963  geographic_point_init(p->x, p->y, &(e1.end));
1964  geog2cart(&(e1.end), &A2);
1965 
1966  /* Initialize start of line 2 */
1967  p = getPoint2d_cp(pa2, 0);
1968  geographic_point_init(p->x, p->y, &(e2.start));
1969  geog2cart(&(e2.start), &B1);
1970 
1971  for ( j = 1; j < pa2->npoints; j++ )
1972  {
1973  double d;
1974 
1975  p = getPoint2d_cp(pa2, j);
1976  geographic_point_init(p->x, p->y, &(e2.end));
1977  geog2cart(&(e2.end), &B2);
1978 
1979  LWDEBUGF(4, "e1.start == GPOINT(%.6g %.6g) ", e1.start.lat, e1.start.lon);
1980  LWDEBUGF(4, "e1.end == GPOINT(%.6g %.6g) ", e1.end.lat, e1.end.lon);
1981  LWDEBUGF(4, "e2.start == GPOINT(%.6g %.6g) ", e2.start.lat, e2.start.lon);
1982  LWDEBUGF(4, "e2.end == GPOINT(%.6g %.6g) ", e2.end.lat, e2.end.lon);
1983 
1984  if ( check_intersection && edge_intersects(&A1, &A2, &B1, &B2) )
1985  {
1986  LWDEBUG(4,"edge intersection! returning 0.0");
1987  return 0.0;
1988  }
1989  d = s->radius * edge_distance_to_edge(&e1, &e2, &g1, &g2);
1990  LWDEBUGF(4,"got edge_distance_to_edge %.8g", d);
1991 
1992  if ( d < distance )
1993  {
1994  distance = d;
1995  nearest1 = g1;
1996  nearest2 = g2;
1997  }
1998  if ( d <= tolerance )
1999  {
2000  if ( use_sphere )
2001  {
2002  return d;
2003  }
2004  else
2005  {
2006  d = spheroid_distance(&nearest1, &nearest2, s);
2007  if ( d <= tolerance )
2008  return d;
2009  }
2010  }
2011 
2012  /* Copy end to start to allow a new end value in next iteration */
2013  e2.start = e2.end;
2014  B1 = B2;
2015  }
2016 
2017  /* Copy end to start to allow a new end value in next iteration */
2018  e1.start = e1.end;
2019  A1 = A2;
2020  LW_ON_INTERRUPT(return -1.0);
2021  }
2022  LWDEBUGF(4,"finished all loops, returning %.8g", distance);
2023 
2024  if ( use_sphere )
2025  return distance;
2026  else
2027  return spheroid_distance(&nearest1, &nearest2, s);
2028 }
char * s
Definition: cu_in_wkt.c:23
#define LW_ON_INTERRUPT(x)
void geographic_point_init(double lon, double lat, GEOGRAPHIC_POINT *g)
Initialize a geographic point.
Definition: lwgeodetic.c:180
double sphere_distance(const GEOGRAPHIC_POINT *s, const GEOGRAPHIC_POINT *e)
Given two points on a unit sphere, calculate their distance apart in radians.
Definition: lwgeodetic.c:948
uint32_t edge_intersects(const POINT3D *A1, const POINT3D *A2, const POINT3D *B1, const POINT3D *B2)
Returns non-zero if edges A and B interact.
Definition: lwgeodetic.c:3540
double edge_distance_to_point(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *gp, GEOGRAPHIC_POINT *closest)
Definition: lwgeodetic.c:1222
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:404
double edge_distance_to_edge(const GEOGRAPHIC_EDGE *e1, const GEOGRAPHIC_EDGE *e2, GEOGRAPHIC_POINT *closest1, GEOGRAPHIC_POINT *closest2)
Calculate the distance between two edges.
Definition: lwgeodetic.c:1275
double spheroid_distance(const GEOGRAPHIC_POINT *a, const GEOGRAPHIC_POINT *b, const SPHEROID *spheroid)
Computes the shortest distance along the surface of the spheroid between two points,...
Definition: lwspheroid.c:79
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static double distance(double x1, double y1, double x2, double y2)
Definition: lwtree.c:1032
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:64
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:65
Two-point great circle segment from a to b.
Definition: lwgeodetic.h:63
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:54
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

References distance(), edge_distance_to_edge(), edge_distance_to_point(), edge_intersects(), GEOGRAPHIC_EDGE::end, geog2cart(), geographic_point_init(), getPoint2d_cp(), GEOGRAPHIC_POINT::lat, GEOGRAPHIC_POINT::lon, LW_ON_INTERRUPT, LWDEBUG, LWDEBUGF, POINTARRAY::npoints, s, sphere_distance(), spheroid_distance(), GEOGRAPHIC_EDGE::start, POINT2D::x, and POINT2D::y.

Referenced by lwgeom_distance_spheroid().

Here is the call graph for this function:
Here is the caller graph for this function: