PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ ptarray_locate_point()

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

Definition at line 1303 of file ptarray.c.

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

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: