PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ ptarray_locate_point()

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

Definition at line 1305 of file ptarray.c.

1306 {
1307  double mindist=DBL_MAX;
1308  double tlen, plen;
1309  uint32_t t, seg=0;
1310  POINT4D start4d, end4d, projtmp;
1311  POINT2D proj, p;
1312  const POINT2D *start = NULL, *end = NULL;
1313 
1314  /* Initialize our 2D copy of the input parameter */
1315  p.x = p4d->x;
1316  p.y = p4d->y;
1317 
1318  if ( ! proj4d ) proj4d = &projtmp;
1319 
1320  /* Check for special cases (length 0 and 1) */
1321  if ( pa->npoints <= 1 )
1322  {
1323  if ( pa->npoints == 1 )
1324  {
1325  getPoint4d_p(pa, 0, proj4d);
1326  if ( mindistout )
1327  *mindistout = distance2d_pt_pt(&p, getPoint2d_cp(pa, 0));
1328  }
1329  return 0.0;
1330  }
1331 
1332  start = getPoint2d_cp(pa, 0);
1333  /* Loop through pointarray looking for nearest segment */
1334  for (t=1; t<pa->npoints; t++)
1335  {
1336  double dist;
1337  end = getPoint2d_cp(pa, t);
1338  dist = distance2d_pt_seg(&p, start, end);
1339 
1340  if ( dist < mindist )
1341  {
1342  mindist = dist;
1343  seg=t-1;
1344  if ( mindist == 0 )
1345  {
1346  LWDEBUG(3, "Breaking on mindist=0");
1347  break;
1348  }
1349  }
1350 
1351  start = end;
1352  }
1353 
1354  if ( mindistout ) *mindistout = mindist;
1355 
1356  LWDEBUGF(3, "Closest segment: %d", seg);
1357  LWDEBUGF(3, "mindist: %g", mindist);
1358 
1359  /*
1360  * We need to project the
1361  * point on the closest segment.
1362  */
1363  getPoint4d_p(pa, seg, &start4d);
1364  getPoint4d_p(pa, seg+1, &end4d);
1365  closest_point_on_segment(p4d, &start4d, &end4d, proj4d);
1366 
1367  /* Copy 4D values into 2D holder */
1368  proj.x = proj4d->x;
1369  proj.y = proj4d->y;
1370 
1371  LWDEBUGF(3, "Closest segment:%d, npoints:%d", seg, pa->npoints);
1372 
1373  /* For robustness, force 1 when closest point == endpoint */
1374  if ( (seg >= (pa->npoints-2)) && p2d_same(&proj, end) )
1375  {
1376  return 1.0;
1377  }
1378 
1379  LWDEBUGF(3, "Closest point on segment: %g,%g", proj.x, proj.y);
1380 
1381  tlen = ptarray_length_2d(pa);
1382 
1383  LWDEBUGF(3, "tlen %g", tlen);
1384 
1385  /* Location of any point on a zero-length line is 0 */
1386  /* See http://trac.osgeo.org/postgis/ticket/1772#comment:2 */
1387  if ( tlen == 0 ) return 0;
1388 
1389  plen=0;
1390  start = getPoint2d_cp(pa, 0);
1391  for (t=0; t<seg; t++, start=end)
1392  {
1393  end = getPoint2d_cp(pa, t+1);
1394  plen += distance2d_pt_pt(start, end);
1395 
1396  LWDEBUGF(4, "Segment %d made plen %g", t, plen);
1397  }
1398 
1399  plen+=distance2d_pt_pt(&proj, start);
1400 
1401  LWDEBUGF(3, "plen %g, tlen %g", plen, tlen);
1402 
1403  return plen/tlen;
1404 }
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2314
double distance2d_pt_seg(const POINT2D *p, const POINT2D *A, const POINT2D *B)
Definition: measures.c:2332
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:123
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
int p2d_same(const POINT2D *p1, const POINT2D *p2)
Definition: lwalgorithm.c:49
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void closest_point_on_segment(const POINT4D *p, const POINT4D *A, const POINT4D *B, POINT4D *ret)
Definition: ptarray.c:1257
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1689
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 closest_point_on_segment(), distance2d_pt_pt(), distance2d_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: