PostGIS  3.6.1dev-r@@SVN_REVISION@@

◆ ptarray_locate_point()

double ptarray_locate_point ( const POINTARRAY pa,
const POINT4D p4d,
double *  mindistout,
POINT4D proj4d 
)

Definition at line 1513 of file ptarray.c.

1514 {
1515  double mindist=DBL_MAX;
1516  double tlen, plen;
1517  uint32_t t, seg=0;
1518  POINT4D start4d, end4d, projtmp;
1519  POINT2D proj, p;
1520  const POINT2D *start = NULL, *end = NULL;
1521 
1522  /* Initialize our 2D copy of the input parameter */
1523  p.x = p4d->x;
1524  p.y = p4d->y;
1525 
1526  if ( ! proj4d ) proj4d = &projtmp;
1527 
1528  /* Check for special cases (length 0 and 1) */
1529  if ( pa->npoints <= 1 )
1530  {
1531  if ( pa->npoints == 1 )
1532  {
1533  getPoint4d_p(pa, 0, proj4d);
1534  if ( mindistout )
1535  *mindistout = distance2d_pt_pt(&p, getPoint2d_cp(pa, 0));
1536  }
1537  return 0.0;
1538  }
1539 
1540  start = getPoint2d_cp(pa, 0);
1541  /* Loop through pointarray looking for nearest segment */
1542  for (t=1; t<pa->npoints; t++)
1543  {
1544  double dist_sqr;
1545  end = getPoint2d_cp(pa, t);
1546  dist_sqr = distance2d_sqr_pt_seg(&p, start, end);
1547 
1548  if (dist_sqr < mindist)
1549  {
1550  mindist = dist_sqr;
1551  seg=t-1;
1552  if ( mindist == 0 )
1553  {
1554  LWDEBUG(3, "Breaking on mindist=0");
1555  break;
1556  }
1557  }
1558 
1559  start = end;
1560  }
1561  mindist = sqrt(mindist);
1562 
1563  if ( mindistout ) *mindistout = mindist;
1564 
1565  LWDEBUGF(3, "Closest segment: %d", seg);
1566  LWDEBUGF(3, "mindist: %g", mindist);
1567 
1568  /*
1569  * We need to project the
1570  * point on the closest segment.
1571  */
1572  getPoint4d_p(pa, seg, &start4d);
1573  getPoint4d_p(pa, seg+1, &end4d);
1574  closest_point_on_segment(p4d, &start4d, &end4d, proj4d);
1575 
1576  /* Copy 4D values into 2D holder */
1577  proj.x = proj4d->x;
1578  proj.y = proj4d->y;
1579 
1580  LWDEBUGF(3, "Closest segment:%d, npoints:%d", seg, pa->npoints);
1581 
1582  /* For robustness, force 1 when closest point == endpoint */
1583  if ( (seg >= (pa->npoints-2)) && p2d_same(&proj, end) )
1584  {
1585  return 1.0;
1586  }
1587 
1588  LWDEBUGF(3, "Closest point on segment: %g,%g", proj.x, proj.y);
1589 
1590  tlen = ptarray_length_2d(pa);
1591 
1592  LWDEBUGF(3, "tlen %g", tlen);
1593 
1594  /* Location of any point on a zero-length line is 0 */
1595  /* See http://trac.osgeo.org/postgis/ticket/1772#comment:2 */
1596  if ( tlen == 0 ) return 0;
1597 
1598  plen=0;
1599  start = getPoint2d_cp(pa, 0);
1600  for (t=0; t<seg; t++, start=end)
1601  {
1602  end = getPoint2d_cp(pa, t+1);
1603  plen += distance2d_pt_pt(start, end);
1604 
1605  LWDEBUGF(4, "Segment %d made plen %g", t, plen);
1606  }
1607 
1608  plen+=distance2d_pt_pt(&proj, start);
1609 
1610  LWDEBUGF(3, "plen %g, tlen %g", plen, tlen);
1611 
1612  return plen/tlen;
1613 }
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2344
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:2354
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:1403
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1963
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
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: