PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ LWGEOM_addpoint()

Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2307 of file lwgeom_functions_basic.c.

2308 {
2309  GSERIALIZED *pglwg1, *pglwg2, *result;
2310  LWPOINT *point;
2311  LWLINE *line, *linecopy;
2312  uint32_t uwhere = 0;
2313 
2314  POSTGIS_DEBUGF(2, "%s called.", __func__);
2315 
2316  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2317  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2318 
2319  if (gserialized_get_type(pglwg1) != LINETYPE)
2320  {
2321  elog(ERROR, "First argument must be a LINESTRING");
2322  PG_RETURN_NULL();
2323  }
2324 
2325  if (gserialized_get_type(pglwg2) != POINTTYPE)
2326  {
2327  elog(ERROR, "Second argument must be a POINT");
2328  PG_RETURN_NULL();
2329  }
2330 
2331  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2332 
2333  if (PG_NARGS() <= 2)
2334  {
2335  uwhere = line->points->npoints;
2336  }
2337  else
2338  {
2339  int32 where = PG_GETARG_INT32(2);
2340  if (where == -1)
2341  {
2342  uwhere = line->points->npoints;
2343  }
2344  else if (where < 0 || where > (int32)line->points->npoints)
2345  {
2346  elog(ERROR, "%s: Invalid offset", __func__);
2347  PG_RETURN_NULL();
2348  }
2349  else
2350  {
2351  uwhere = where;
2352  }
2353  }
2354 
2355  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2357  lwline_free(line);
2358 
2359  if (lwline_add_lwpoint(linecopy, point, uwhere) == LW_FAILURE)
2360  {
2361  elog(ERROR, "Point insert failed");
2362  PG_RETURN_NULL();
2363  }
2364 
2365  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2366 
2367  /* Release memory */
2368  PG_FREE_IF_COPY(pglwg1, 0);
2369  PG_FREE_IF_COPY(pglwg2, 1);
2370  lwpoint_free(point);
2371 
2372  PG_RETURN_POINTER(result);
2373 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:179
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:339
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
#define LW_FAILURE
Definition: liblwgeom.h:96
#define LINETYPE
Definition: liblwgeom.h:103
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:529
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
void lwline_free(LWLINE *line)
Definition: lwline.c:67
int lwline_add_lwpoint(LWLINE *line, LWPOINT *point, uint32_t where)
Add a LWPOINT to an LWLINE.
Definition: lwline.c:327
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
unsigned int int32
Definition: shpopen.c:54
POINTARRAY * points
Definition: liblwgeom.h:483
uint32_t npoints
Definition: liblwgeom.h:427

References gserialized_get_type(), LINETYPE, LW_FAILURE, lwgeom_as_lwline(), lwgeom_as_lwpoint(), lwgeom_clone_deep(), lwgeom_from_gserialized(), lwline_add_lwpoint(), lwline_as_lwgeom(), lwline_free(), lwpoint_free(), POINTARRAY::npoints, LWLINE::points, POINTTYPE, and result.

Here is the call graph for this function: