PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ ptarray_locate_point()

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

Definition at line 1373 of file ptarray.c.

1374 {
1375  double mindist=DBL_MAX;
1376  double tlen, plen;
1377  uint32_t t, seg=0;
1378  POINT4D start4d, end4d, projtmp;
1379  POINT2D proj, p;
1380  const POINT2D *start = NULL, *end = NULL;
1381 
1382  /* Initialize our 2D copy of the input parameter */
1383  p.x = p4d->x;
1384  p.y = p4d->y;
1385 
1386  if ( ! proj4d ) proj4d = &projtmp;
1387 
1388  /* Check for special cases (length 0 and 1) */
1389  if ( pa->npoints <= 1 )
1390  {
1391  if ( pa->npoints == 1 )
1392  {
1393  getPoint4d_p(pa, 0, proj4d);
1394  if ( mindistout )
1395  *mindistout = distance2d_pt_pt(&p, getPoint2d_cp(pa, 0));
1396  }
1397  return 0.0;
1398  }
1399 
1400  start = getPoint2d_cp(pa, 0);
1401  /* Loop through pointarray looking for nearest segment */
1402  for (t=1; t<pa->npoints; t++)
1403  {
1404  double dist_sqr;
1405  end = getPoint2d_cp(pa, t);
1406  dist_sqr = distance2d_sqr_pt_seg(&p, start, end);
1407 
1408  if (dist_sqr < mindist)
1409  {
1410  mindist = dist_sqr;
1411  seg=t-1;
1412  if ( mindist == 0 )
1413  {
1414  LWDEBUG(3, "Breaking on mindist=0");
1415  break;
1416  }
1417  }
1418 
1419  start = end;
1420  }
1421  mindist = sqrt(mindist);
1422 
1423  if ( mindistout ) *mindistout = mindist;
1424 
1425  LWDEBUGF(3, "Closest segment: %d", seg);
1426  LWDEBUGF(3, "mindist: %g", mindist);
1427 
1428  /*
1429  * We need to project the
1430  * point on the closest segment.
1431  */
1432  getPoint4d_p(pa, seg, &start4d);
1433  getPoint4d_p(pa, seg+1, &end4d);
1434  closest_point_on_segment(p4d, &start4d, &end4d, proj4d);
1435 
1436  /* Copy 4D values into 2D holder */
1437  proj.x = proj4d->x;
1438  proj.y = proj4d->y;
1439 
1440  LWDEBUGF(3, "Closest segment:%d, npoints:%d", seg, pa->npoints);
1441 
1442  /* For robustness, force 1 when closest point == endpoint */
1443  if ( (seg >= (pa->npoints-2)) && p2d_same(&proj, end) )
1444  {
1445  return 1.0;
1446  }
1447 
1448  LWDEBUGF(3, "Closest point on segment: %g,%g", proj.x, proj.y);
1449 
1450  tlen = ptarray_length_2d(pa);
1451 
1452  LWDEBUGF(3, "tlen %g", tlen);
1453 
1454  /* Location of any point on a zero-length line is 0 */
1455  /* See http://trac.osgeo.org/postgis/ticket/1772#comment:2 */
1456  if ( tlen == 0 ) return 0;
1457 
1458  plen=0;
1459  start = getPoint2d_cp(pa, 0);
1460  for (t=0; t<seg; t++, start=end)
1461  {
1462  end = getPoint2d_cp(pa, t+1);
1463  plen += distance2d_pt_pt(start, end);
1464 
1465  LWDEBUGF(4, "Segment %d made plen %g", t, plen);
1466  }
1467 
1468  plen+=distance2d_pt_pt(&proj, start);
1469 
1470  LWDEBUGF(3, "plen %g, tlen %g", plen, tlen);
1471 
1472  return plen/tlen;
1473 }
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:126
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:50
#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
void closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, POINT4D *ret)
Definition: ptarray.c:1263
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1819
double y
Definition: liblwgeom.h:405
double x
Definition: liblwgeom.h:405
uint32_t npoints
Definition: liblwgeom.h:442

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: