PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ LWGEOM_line_substring()

Datum LWGEOM_line_substring ( PG_FUNCTION_ARGS  )

Definition at line 472 of file lwgeom_functions_analytic.c.

References COLLECTIONTYPE, dumpnode::geom, geometry_serialize(), gserialized_get_type(), if(), LINETYPE, LW_FALSE, LW_TRUE, lwalloc(), lwcollection_construct(), lwgeom_as_lwline(), lwgeom_as_lwmline(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_is_empty(), lwline_construct(), lwline_release(), lwmline_release(), lwpoint_construct(), MULTILINETYPE, POINTARRAY::npoints, LWLINE::points, ptarray_length_2d(), ptarray_substring(), LWGEOM::srid, SRID_UNKNOWN, and ovdump::type.

Referenced by ST_LineCrossingDirection().

473 {
474  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
475  double from = PG_GETARG_FLOAT8(1);
476  double to = PG_GETARG_FLOAT8(2);
477  LWGEOM *olwgeom;
478  POINTARRAY *ipa, *opa;
479  GSERIALIZED *ret;
480  int type = gserialized_get_type(geom);
481 
482  if ( from < 0 || from > 1 )
483  {
484  elog(ERROR,"line_interpolate_point: 2nd arg isn't within [0,1]");
485  PG_RETURN_NULL();
486  }
487 
488  if ( to < 0 || to > 1 )
489  {
490  elog(ERROR,"line_interpolate_point: 3rd arg isn't within [0,1]");
491  PG_RETURN_NULL();
492  }
493 
494  if ( from > to )
495  {
496  elog(ERROR, "2nd arg must be smaller then 3rd arg");
497  PG_RETURN_NULL();
498  }
499 
500  if ( type == LINETYPE )
501  {
503 
504  if ( lwgeom_is_empty((LWGEOM*)iline) )
505  {
506  /* TODO return empty line */
507  lwline_release(iline);
508  PG_FREE_IF_COPY(geom, 0);
509  PG_RETURN_NULL();
510  }
511 
512  ipa = iline->points;
513 
514  opa = ptarray_substring(ipa, from, to, 0);
515 
516  if ( opa->npoints == 1 ) /* Point returned */
517  olwgeom = (LWGEOM *)lwpoint_construct(iline->srid, NULL, opa);
518  else
519  olwgeom = (LWGEOM *)lwline_construct(iline->srid, NULL, opa);
520 
521  }
522  else if ( type == MULTILINETYPE )
523  {
524  LWMLINE *iline;
525  uint32_t i = 0, g = 0;
526  int homogeneous = LW_TRUE;
527  LWGEOM **geoms = NULL;
528  double length = 0.0, sublength = 0.0, minprop = 0.0, maxprop = 0.0;
529 
531 
532  if ( lwgeom_is_empty((LWGEOM*)iline) )
533  {
534  /* TODO return empty collection */
535  lwmline_release(iline);
536  PG_FREE_IF_COPY(geom, 0);
537  PG_RETURN_NULL();
538  }
539 
540  /* Calculate the total length of the mline */
541  for ( i = 0; i < iline->ngeoms; i++ )
542  {
543  LWLINE *subline = (LWLINE*)iline->geoms[i];
544  if ( subline->points && subline->points->npoints > 1 )
545  length += ptarray_length_2d(subline->points);
546  }
547 
548  geoms = lwalloc(sizeof(LWGEOM*) * iline->ngeoms);
549 
550  /* Slice each sub-geometry of the multiline */
551  for ( i = 0; i < iline->ngeoms; i++ )
552  {
553  LWLINE *subline = (LWLINE*)iline->geoms[i];
554  double subfrom = 0.0, subto = 0.0;
555 
556  if ( subline->points && subline->points->npoints > 1 )
557  sublength += ptarray_length_2d(subline->points);
558 
559  /* Calculate proportions for this subline */
560  minprop = maxprop;
561  maxprop = sublength / length;
562 
563  /* This subline doesn't reach the lowest proportion requested
564  or is beyond the highest proporton */
565  if ( from > maxprop || to < minprop )
566  continue;
567 
568  if ( from <= minprop )
569  subfrom = 0.0;
570  if ( to >= maxprop )
571  subto = 1.0;
572 
573  if ( from > minprop && from <= maxprop )
574  subfrom = (from - minprop) / (maxprop - minprop);
575 
576  if ( to < maxprop && to >= minprop )
577  subto = (to - minprop) / (maxprop - minprop);
578 
579 
580  opa = ptarray_substring(subline->points, subfrom, subto, 0);
581  if ( opa && opa->npoints > 0 )
582  {
583  if ( opa->npoints == 1 ) /* Point returned */
584  {
585  geoms[g] = (LWGEOM *)lwpoint_construct(SRID_UNKNOWN, NULL, opa);
586  homogeneous = LW_FALSE;
587  }
588  else
589  {
590  geoms[g] = (LWGEOM *)lwline_construct(SRID_UNKNOWN, NULL, opa);
591  }
592  g++;
593  }
594 
595 
596 
597  }
598  /* If we got any points, we need to return a GEOMETRYCOLLECTION */
599  if ( ! homogeneous )
600  type = COLLECTIONTYPE;
601 
602  olwgeom = (LWGEOM*)lwcollection_construct(type, iline->srid, NULL, g, geoms);
603  }
604  else
605  {
606  elog(ERROR,"line_substring: 1st arg isn't a line");
607  PG_RETURN_NULL();
608  }
609 
610  ret = geometry_serialize(olwgeom);
611  lwgeom_free(olwgeom);
612  PG_FREE_IF_COPY(geom, 0);
613  PG_RETURN_POINTER(ret);
614 
615 }
#define LINETYPE
Definition: liblwgeom.h:85
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
POINTARRAY * ptarray_substring(POINTARRAY *pa, double d1, double d2, double tolerance)
start location (distance from start / total distance) end location (distance from start / total dist...
Definition: ptarray.c:1053
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
int32_t srid
Definition: liblwgeom.h:401
unsigned int uint32_t
Definition: uthash.h:78
LWGEOM * geom
#define LW_FALSE
Definition: liblwgeom.h:76
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
LWMLINE * lwgeom_as_lwmline(const LWGEOM *lwgeom)
Definition: lwgeom.c:242
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:170
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
type
Definition: ovdump.py:41
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:129
void * lwalloc(size_t size)
Definition: lwutil.c:229
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1393
void lwmline_release(LWMLINE *lwline)
Definition: lwmline.c:32
#define MULTILINETYPE
Definition: liblwgeom.h:88
void lwline_release(LWLINE *lwline)
Definition: lwline.c:134
if(!(yy_init))
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
POINTARRAY * points
Definition: liblwgeom.h:424
uint32_t npoints
Definition: liblwgeom.h:373
Here is the call graph for this function:
Here is the caller graph for this function: