PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ lwline_measured_from_lwline()

LWLINE* lwline_measured_from_lwline ( const LWLINE lwline,
double  m_start,
double  m_end 
)

Add a measure dimension to a line, interpolating linearly from the start to the end value.

Add a measure dimension to a line, interpolating linearly from the start to the end value.

Definition at line 379 of file lwline.c.

380 {
381  int i = 0;
382  int hasm = 0, hasz = 0;
383  int npoints = 0;
384  double length = 0.0;
385  double length_so_far = 0.0;
386  double m_range = m_end - m_start;
387  double m;
388  POINTARRAY *pa = NULL;
389  POINT3DZ p1, p2;
390 
391  if ( lwline->type != LINETYPE )
392  {
393  lwerror("lwline_construct_from_lwline: only line types supported");
394  return NULL;
395  }
396 
397  hasz = FLAGS_GET_Z(lwline->flags);
398  hasm = 1;
399 
400  /* Null points or npoints == 0 will result in empty return geometry */
401  if ( lwline->points )
402  {
403  npoints = lwline->points->npoints;
404  length = ptarray_length_2d(lwline->points);
405  getPoint3dz_p(lwline->points, 0, &p1);
406  }
407 
408  pa = ptarray_construct(hasz, hasm, npoints);
409 
410  for ( i = 0; i < npoints; i++ )
411  {
412  POINT4D q;
413  POINT2D a, b;
414  getPoint3dz_p(lwline->points, i, &p2);
415  a.x = p1.x;
416  a.y = p1.y;
417  b.x = p2.x;
418  b.y = p2.y;
419  length_so_far += distance2d_pt_pt(&a, &b);
420  if ( length > 0.0 )
421  m = m_start + m_range * length_so_far / length;
422  /* #3172, support (valid) zero-length inputs */
423  else if ( length == 0.0 && npoints > 1 )
424  m = m_start + m_range * i / (npoints-1);
425  else
426  m = 0.0;
427  q.x = p2.x;
428  q.y = p2.y;
429  q.z = p2.z;
430  q.m = m;
431  ptarray_set_point4d(pa, i, &q);
432  p1 = p2;
433  }
434 
435  return lwline_construct(lwline->srid, NULL, pa);
436 }
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: measures.c:2397
#define LINETYPE
Definition: liblwgeom.h:117
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
Definition: ptarray.c:51
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1700
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:179
int getPoint3dz_p(const POINTARRAY *pa, uint32_t n, POINT3DZ *point)
Definition: lwgeom_api.c:215
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:376
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
lwflags_t flags
Definition: liblwgeom.h:471
POINTARRAY * points
Definition: liblwgeom.h:469
uint8_t type
Definition: liblwgeom.h:472
int32_t srid
Definition: liblwgeom.h:470
double y
Definition: liblwgeom.h:376
double x
Definition: liblwgeom.h:376
double z
Definition: liblwgeom.h:382
double x
Definition: liblwgeom.h:382
double y
Definition: liblwgeom.h:382
double m
Definition: liblwgeom.h:400
double x
Definition: liblwgeom.h:400
double z
Definition: liblwgeom.h:400
double y
Definition: liblwgeom.h:400
uint32_t npoints
Definition: liblwgeom.h:413

References distance2d_pt_pt(), LWLINE::flags, FLAGS_GET_Z, getPoint3dz_p(), LINETYPE, lwerror(), lwline_construct(), POINT4D::m, POINTARRAY::npoints, LWLINE::points, ptarray_construct(), ptarray_length_2d(), ptarray_set_point4d(), LWLINE::srid, LWLINE::type, POINT2D::x, POINT3DZ::x, POINT4D::x, POINT2D::y, POINT3DZ::y, POINT4D::y, POINT3DZ::z, and POINT4D::z.

Referenced by lwline_locate_along(), lwmline_measured_from_lwmline(), and ST_AddMeasure().

Here is the call graph for this function:
Here is the caller graph for this function: