PostGIS  3.0.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 1292 of file ptarray.c.

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().

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