PostGIS  2.5.7dev-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 1835 of file lwgeodetic.c.

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

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: