PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ ptarray_locate_point()

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

Definition at line 1311 of file ptarray.c.

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