PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ ptarray_locate_point()

double ptarray_locate_point ( const POINTARRAY pa,
const POINT4D pt,
double *  dist,
POINT4D p_located 
)

Definition at line 1390 of file ptarray.c.

1391 {
1392  double mindist=DBL_MAX;
1393  double tlen, plen;
1394  uint32_t t, seg=0;
1395  POINT4D start4d, end4d, projtmp;
1396  POINT2D proj, p;
1397  const POINT2D *start = NULL, *end = NULL;
1398 
1399  /* Initialize our 2D copy of the input parameter */
1400  p.x = p4d->x;
1401  p.y = p4d->y;
1402 
1403  if ( ! proj4d ) proj4d = &projtmp;
1404 
1405  /* Check for special cases (length 0 and 1) */
1406  if ( pa->npoints <= 1 )
1407  {
1408  if ( pa->npoints == 1 )
1409  {
1410  getPoint4d_p(pa, 0, proj4d);
1411  if ( mindistout )
1412  *mindistout = distance2d_pt_pt(&p, getPoint2d_cp(pa, 0));
1413  }
1414  return 0.0;
1415  }
1416 
1417  start = getPoint2d_cp(pa, 0);
1418  /* Loop through pointarray looking for nearest segment */
1419  for (t=1; t<pa->npoints; t++)
1420  {
1421  double dist_sqr;
1422  end = getPoint2d_cp(pa, t);
1423  dist_sqr = distance2d_sqr_pt_seg(&p, start, end);
1424 
1425  if (dist_sqr < mindist)
1426  {
1427  mindist = dist_sqr;
1428  seg=t-1;
1429  if ( mindist == 0 )
1430  {
1431  LWDEBUG(3, "Breaking on mindist=0");
1432  break;
1433  }
1434  }
1435 
1436  start = end;
1437  }
1438  mindist = sqrt(mindist);
1439 
1440  if ( mindistout ) *mindistout = mindist;
1441 
1442  LWDEBUGF(3, "Closest segment: %d", seg);
1443  LWDEBUGF(3, "mindist: %g", mindist);
1444 
1445  /*
1446  * We need to project the
1447  * point on the closest segment.
1448  */
1449  getPoint4d_p(pa, seg, &start4d);
1450  getPoint4d_p(pa, seg+1, &end4d);
1451  closest_point_on_segment(p4d, &start4d, &end4d, proj4d);
1452 
1453  /* Copy 4D values into 2D holder */
1454  proj.x = proj4d->x;
1455  proj.y = proj4d->y;
1456 
1457  LWDEBUGF(3, "Closest segment:%d, npoints:%d", seg, pa->npoints);
1458 
1459  /* For robustness, force 1 when closest point == endpoint */
1460  if ( (seg >= (pa->npoints-2)) && p2d_same(&proj, end) )
1461  {
1462  return 1.0;
1463  }
1464 
1465  LWDEBUGF(3, "Closest point on segment: %g,%g", proj.x, proj.y);
1466 
1467  tlen = ptarray_length_2d(pa);
1468 
1469  LWDEBUGF(3, "tlen %g", tlen);
1470 
1471  /* Location of any point on a zero-length line is 0 */
1472  /* See http://trac.osgeo.org/postgis/ticket/1772#comment:2 */
1473  if ( tlen == 0 ) return 0;
1474 
1475  plen=0;
1476  start = getPoint2d_cp(pa, 0);
1477  for (t=0; t<seg; t++, start=end)
1478  {
1479  end = getPoint2d_cp(pa, t+1);
1480  plen += distance2d_pt_pt(start, end);
1481 
1482  LWDEBUGF(4, "Segment %d made plen %g", t, plen);
1483  }
1484 
1485  plen+=distance2d_pt_pt(&proj, start);
1486 
1487  LWDEBUGF(3, "plen %g, tlen %g", plen, tlen);
1488 
1489  return plen/tlen;
1490 }
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2445
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
double distance2d_sqr_pt_seg(const POINT2D *p, const POINT2D *A, const POINT2D *B)
Definition: measures.c:2455
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:57
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:101
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:106
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:97
void closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, POINT4D *ret)
Definition: ptarray.c:1280
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1840
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

References closest_point_on_segment(), distance2d_pt_pt(), distance2d_sqr_pt_seg(), getPoint2d_cp(), getPoint4d_p(), LWDEBUG, LWDEBUGF, POINTARRAY::npoints, p2d_same(), ptarray_length_2d(), POINT2D::x, POINT4D::x, POINT2D::y, and POINT4D::y.

Referenced by lwgeom_interpolate_point(), LWGEOM_line_locate_point(), and test_ptarray_locate_point().

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