PostGIS  2.2.8dev-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 367 of file lwline.c.

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

368 {
369  int i = 0;
370  int hasm = 0, hasz = 0;
371  int npoints = 0;
372  double length = 0.0;
373  double length_so_far = 0.0;
374  double m_range = m_end - m_start;
375  double m;
376  POINTARRAY *pa = NULL;
377  POINT3DZ p1, p2;
378 
379  if ( lwline->type != LINETYPE )
380  {
381  lwerror("lwline_construct_from_lwline: only line types supported");
382  return NULL;
383  }
384 
385  hasz = FLAGS_GET_Z(lwline->flags);
386  hasm = 1;
387 
388  /* Null points or npoints == 0 will result in empty return geometry */
389  if ( lwline->points )
390  {
391  npoints = lwline->points->npoints;
392  length = ptarray_length_2d(lwline->points);
393  getPoint3dz_p(lwline->points, 0, &p1);
394  }
395 
396  pa = ptarray_construct(hasz, hasm, npoints);
397 
398  for ( i = 0; i < npoints; i++ )
399  {
400  POINT4D q;
401  POINT2D a, b;
402  getPoint3dz_p(lwline->points, i, &p2);
403  a.x = p1.x;
404  a.y = p1.y;
405  b.x = p2.x;
406  b.y = p2.y;
407  length_so_far += distance2d_pt_pt(&a, &b);
408  if ( length > 0.0 )
409  m = m_start + m_range * length_so_far / length;
410  /* #3172, support (valid) zero-length inputs */
411  else if ( length == 0.0 && npoints > 1 )
412  m = m_start + m_range * i / (npoints-1);
413  else
414  m = 0.0;
415  q.x = p2.x;
416  q.y = p2.y;
417  q.z = p2.z;
418  q.m = m;
419  ptarray_set_point4d(pa, i, &q);
420  p1 = p2;
421  }
422 
423  return lwline_construct(lwline->srid, NULL, pa);
424 }
void ptarray_set_point4d(POINTARRAY *pa, int n, const POINT4D *p4d)
Definition: lwgeom_api.c:536
double x
Definition: liblwgeom.h:336
#define LINETYPE
Definition: liblwgeom.h:71
uint8_t type
Definition: liblwgeom.h:402
double z
Definition: liblwgeom.h:318
double y
Definition: liblwgeom.h:318
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:62
double m
Definition: liblwgeom.h:336
double x
Definition: liblwgeom.h:318
int npoints
Definition: liblwgeom.h:355
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it&#39;s 3d)
Definition: ptarray.c:1645
double distance2d_pt_pt(const POINT2D *p1, const POINT2D *p2)
The old function nessecary for ptarray_segmentize2d in ptarray.c.
Definition: measures.c:2302
int32_t srid
Definition: liblwgeom.h:405
double x
Definition: liblwgeom.h:312
int getPoint3dz_p(const POINTARRAY *pa, int n, POINT3DZ *point)
Definition: lwgeom_api.c:319
double y
Definition: liblwgeom.h:312
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:124
double z
Definition: liblwgeom.h:336
double y
Definition: liblwgeom.h:336
uint8_t flags
Definition: liblwgeom.h:403
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
POINTARRAY * points
Definition: liblwgeom.h:406
Here is the call graph for this function:
Here is the caller graph for this function: