PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ ptarray_locate_point()

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

Definition at line 1298 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().

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