PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ LWGEOM_line_substring()

Datum LWGEOM_line_substring ( PG_FUNCTION_ARGS  )

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

503 {
504  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
505  double from = PG_GETARG_FLOAT8(1);
506  double to = PG_GETARG_FLOAT8(2);
507  LWGEOM *olwgeom;
508  POINTARRAY *ipa, *opa;
509  GSERIALIZED *ret;
510  int type = gserialized_get_type(geom);
511 
512  if ( from < 0 || from > 1 )
513  {
514  elog(ERROR,"line_interpolate_point: 2nd arg isn't within [0,1]");
515  PG_RETURN_NULL();
516  }
517 
518  if ( to < 0 || to > 1 )
519  {
520  elog(ERROR,"line_interpolate_point: 3rd arg isn't within [0,1]");
521  PG_RETURN_NULL();
522  }
523 
524  if ( from > to )
525  {
526  elog(ERROR, "2nd arg must be smaller then 3rd arg");
527  PG_RETURN_NULL();
528  }
529 
530  if ( type == LINETYPE )
531  {
533 
534  if ( lwgeom_is_empty((LWGEOM*)iline) )
535  {
536  /* TODO return empty line */
537  lwline_release(iline);
538  PG_FREE_IF_COPY(geom, 0);
539  PG_RETURN_NULL();
540  }
541 
542  ipa = iline->points;
543 
544  opa = ptarray_substring(ipa, from, to, 0);
545 
546  if ( opa->npoints == 1 ) /* Point returned */
547  olwgeom = (LWGEOM *)lwpoint_construct(iline->srid, NULL, opa);
548  else
549  olwgeom = (LWGEOM *)lwline_construct(iline->srid, NULL, opa);
550 
551  }
552  else if ( type == MULTILINETYPE )
553  {
554  LWMLINE *iline;
555  int i = 0, g = 0;
556  int homogeneous = LW_TRUE;
557  LWGEOM **geoms = NULL;
558  double length = 0.0, sublength = 0.0, minprop = 0.0, maxprop = 0.0;
559 
561 
562  if ( lwgeom_is_empty((LWGEOM*)iline) )
563  {
564  /* TODO return empty collection */
565  lwmline_release(iline);
566  PG_FREE_IF_COPY(geom, 0);
567  PG_RETURN_NULL();
568  }
569 
570  /* Calculate the total length of the mline */
571  for ( i = 0; i < iline->ngeoms; i++ )
572  {
573  LWLINE *subline = (LWLINE*)iline->geoms[i];
574  if ( subline->points && subline->points->npoints > 1 )
575  length += ptarray_length_2d(subline->points);
576  }
577 
578  geoms = lwalloc(sizeof(LWGEOM*) * iline->ngeoms);
579 
580  /* Slice each sub-geometry of the multiline */
581  for ( i = 0; i < iline->ngeoms; i++ )
582  {
583  LWLINE *subline = (LWLINE*)iline->geoms[i];
584  double subfrom = 0.0, subto = 0.0;
585 
586  if ( subline->points && subline->points->npoints > 1 )
587  sublength += ptarray_length_2d(subline->points);
588 
589  /* Calculate proportions for this subline */
590  minprop = maxprop;
591  maxprop = sublength / length;
592 
593  /* This subline doesn't reach the lowest proportion requested
594  or is beyond the highest proporton */
595  if ( from > maxprop || to < minprop )
596  continue;
597 
598  if ( from <= minprop )
599  subfrom = 0.0;
600  if ( to >= maxprop )
601  subto = 1.0;
602 
603  if ( from > minprop && from <= maxprop )
604  subfrom = (from - minprop) / (maxprop - minprop);
605 
606  if ( to < maxprop && to >= minprop )
607  subto = (to - minprop) / (maxprop - minprop);
608 
609 
610  opa = ptarray_substring(subline->points, subfrom, subto, 0);
611  if ( opa && opa->npoints > 0 )
612  {
613  if ( opa->npoints == 1 ) /* Point returned */
614  {
615  geoms[g] = (LWGEOM *)lwpoint_construct(SRID_UNKNOWN, NULL, opa);
616  homogeneous = LW_FALSE;
617  }
618  else
619  {
620  geoms[g] = (LWGEOM *)lwline_construct(SRID_UNKNOWN, NULL, opa);
621  }
622  g++;
623  }
624 
625 
626 
627  }
628  /* If we got any points, we need to return a GEOMETRYCOLLECTION */
629  if ( ! homogeneous )
630  type = COLLECTIONTYPE;
631 
632  olwgeom = (LWGEOM*)lwcollection_construct(type, iline->srid, NULL, g, geoms);
633  }
634  else
635  {
636  elog(ERROR,"line_substring: 1st arg isn't a line");
637  PG_RETURN_NULL();
638  }
639 
640  ret = geometry_serialize(olwgeom);
641  lwgeom_free(olwgeom);
642  PG_FREE_IF_COPY(geom, 0);
643  PG_RETURN_POINTER(ret);
644 
645 }
#define LINETYPE
Definition: liblwgeom.h:86
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.
int npoints
Definition: liblwgeom.h:371
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
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:1061
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it&#39;s 3d)
Definition: ptarray.c:1692
int32_t srid
Definition: liblwgeom.h:399
LWGEOM * geom
#define LW_FALSE
Definition: liblwgeom.h:77
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
LWMLINE * lwgeom_as_lwmline(const LWGEOM *lwgeom)
Definition: lwgeom.c:210
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
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:1346
void lwmline_release(LWMLINE *lwline)
Definition: lwmline.c:32
#define MULTILINETYPE
Definition: liblwgeom.h:89
void lwline_release(LWLINE *lwline)
Definition: lwline.c:134
if(!(yy_init))
#define COLLECTIONTYPE
Definition: liblwgeom.h:91
POINTARRAY * points
Definition: liblwgeom.h:422
Here is the call graph for this function:
Here is the caller graph for this function: