PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum LWGEOM_pointn_linestring ( PG_FUNCTION_ARGS  )

Definition at line 544 of file lwgeom_ogc.c.

References CIRCSTRINGTYPE, COMPOUNDTYPE, genraster::count, geometry_serialize(), LINETYPE, lwcompound_get_lwpoint(), lwgeom_count_vertices(), lwgeom_free(), lwgeom_from_gserialized(), lwline_get_lwpoint(), lwpoint_as_lwgeom(), and LWGEOM::type.

545 {
546  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
547  int where = PG_GETARG_INT32(1);
548  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
549  LWPOINT *lwpoint = NULL;
550  int type = lwgeom->type;
551 
552  /* If index is negative, count backward */
553  if( where < 1 )
554  {
555  int count = -1;
556  if ( type == LINETYPE || type == CIRCSTRINGTYPE || type == COMPOUNDTYPE )
557  count = lwgeom_count_vertices(lwgeom);
558  if(count >0)
559  {
560  /* only work if we found the total point number */
561  /* converting where to positive backward indexing, +1 because 1 indexing */
562  where = where + count + 1;
563  }
564  if (where < 1)
565  PG_RETURN_NULL();
566  }
567 
568  if ( type == LINETYPE || type == CIRCSTRINGTYPE )
569  {
570  /* OGC index starts at one, so we substract first. */
571  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, where - 1);
572  }
573  else if ( type == COMPOUNDTYPE )
574  {
575  lwpoint = lwcompound_get_lwpoint((LWCOMPOUND*)lwgeom, where - 1);
576  }
577 
578  lwgeom_free(lwgeom);
579  PG_FREE_IF_COPY(geom, 0);
580 
581  if ( ! lwpoint )
582  PG_RETURN_NULL();
583 
584  PG_RETURN_POINTER(geometry_serialize(lwpoint_as_lwgeom(lwpoint)));
585 }
#define LINETYPE
Definition: liblwgeom.h:85
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
#define COMPOUNDTYPE
Definition: liblwgeom.h:92
int count
Definition: genraster.py:56
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1228
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
uint8_t type
Definition: liblwgeom.h:395
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:334
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:91
LWPOINT * lwcompound_get_lwpoint(const LWCOMPOUND *lwcmp, uint32_t where)
Definition: lwcompound.c:213
LWPOINT * lwline_get_lwpoint(const LWLINE *line, uint32_t where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:318

Here is the call graph for this function: