PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ edge_distance_to_point()

double edge_distance_to_point ( const GEOGRAPHIC_EDGE e,
const GEOGRAPHIC_POINT gp,
GEOGRAPHIC_POINT closest 
)

Definition at line 1222 of file lwgeodetic.c.

1223 {
1224  double d1 = 1000000000.0, d2, d3, d_nearest;
1225  POINT3D n, p, k;
1226  GEOGRAPHIC_POINT gk, g_nearest;
1227 
1228  /* Zero length edge, */
1229  if ( geographic_point_equals(&(e->start), &(e->end)) )
1230  {
1231  if (closest)
1232  *closest = e->start;
1233 
1234  return sphere_distance(&(e->start), gp);
1235  }
1236 
1237  robust_cross_product(&(e->start), &(e->end), &n);
1238  normalize(&n);
1239  geog2cart(gp, &p);
1240  vector_scale(&n, dot_product(&p, &n));
1241  vector_difference(&p, &n, &k);
1242  normalize(&k);
1243  cart2geog(&k, &gk);
1244  if ( edge_point_in_cone(e, &gk) )
1245  {
1246  d1 = sphere_distance(gp, &gk);
1247  }
1248  d2 = sphere_distance(gp, &(e->start));
1249  d3 = sphere_distance(gp, &(e->end));
1250 
1251  d_nearest = d1;
1252  g_nearest = gk;
1253 
1254  if ( d2 < d_nearest )
1255  {
1256  d_nearest = d2;
1257  g_nearest = e->start;
1258  }
1259  if ( d3 < d_nearest )
1260  {
1261  d_nearest = d3;
1262  g_nearest = e->end;
1263  }
1264  if (closest)
1265  *closest = g_nearest;
1266 
1267  return d_nearest;
1268 }
void normalize(POINT3D *p)
Normalize to a unit vector.
Definition: lwgeodetic.c:615
void vector_scale(POINT3D *n, double scale)
Scale a vector out by a factor.
Definition: lwgeodetic.c:487
void cart2geog(const POINT3D *p, GEOGRAPHIC_POINT *g)
Convert cartesian coordinates on unit sphere to spherical coordinates.
Definition: lwgeodetic.c:414
int edge_point_in_cone(const GEOGRAPHIC_EDGE *e, const GEOGRAPHIC_POINT *p)
Returns true if the point p is inside the cone defined by the two ends of the edge e.
Definition: lwgeodetic.c:788
void robust_cross_product(const GEOGRAPHIC_POINT *p, const GEOGRAPHIC_POINT *q, POINT3D *a)
Computes the cross product of two vectors using their lat, lng representations.
Definition: lwgeodetic.c:634
static void vector_difference(const POINT3D *a, const POINT3D *b, POINT3D *n)
Calculate the difference of two vectors.
Definition: lwgeodetic.c:476
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
static double dot_product(const POINT3D *p1, const POINT3D *p2)
Convert cartesian coordinates on unit sphere to lon/lat coordinates static void cart2ll(const POINT3D...
Definition: lwgeodetic.c:446
void geog2cart(const GEOGRAPHIC_POINT *g, POINT3D *p)
Convert spherical coordinates to cartesian coordinates on unit sphere.
Definition: lwgeodetic.c:404
int geographic_point_equals(const GEOGRAPHIC_POINT *g1, const GEOGRAPHIC_POINT *g2)
Definition: lwgeodetic.c:170
GEOGRAPHIC_POINT start
Definition: lwgeodetic.h:64
GEOGRAPHIC_POINT end
Definition: lwgeodetic.h:65
Point in spherical coordinates on the world.
Definition: lwgeodetic.h:54

References cart2geog(), dot_product(), edge_point_in_cone(), GEOGRAPHIC_EDGE::end, geog2cart(), geographic_point_equals(), normalize(), robust_cross_product(), sphere_distance(), GEOGRAPHIC_EDGE::start, vector_difference(), and vector_scale().

Referenced by circ_tree_contains_point(), circ_tree_distance_tree_internal(), edge_distance_to_edge(), ptarray_distance_spheroid(), ptarray_locate_point_spheroid(), and test_edge_distance_to_point().

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