PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ LWGEOM_addpoint()

Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2306 of file lwgeom_functions_basic.c.

2307 {
2308  GSERIALIZED *pglwg1, *pglwg2, *result;
2309  LWPOINT *point;
2310  LWLINE *line, *linecopy;
2311  uint32_t uwhere = 0;
2312 
2313  POSTGIS_DEBUGF(2, "%s called.", __func__);
2314 
2315  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2316  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2317 
2318  if (gserialized_get_type(pglwg1) != LINETYPE)
2319  {
2320  elog(ERROR, "First argument must be a LINESTRING");
2321  PG_RETURN_NULL();
2322  }
2323 
2324  if (gserialized_get_type(pglwg2) != POINTTYPE)
2325  {
2326  elog(ERROR, "Second argument must be a POINT");
2327  PG_RETURN_NULL();
2328  }
2329 
2330  if (gserialized_is_empty(pglwg2))
2331  {
2332  PG_RETURN_POINTER(pglwg1);
2333  }
2334 
2335  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2336 
2337  if (PG_NARGS() <= 2)
2338  {
2339  uwhere = line->points->npoints;
2340  }
2341  else
2342  {
2343  int32 where = PG_GETARG_INT32(2);
2344  if (where == -1)
2345  {
2346  uwhere = line->points->npoints;
2347  }
2348  else if (where < 0 || where > (int32)line->points->npoints)
2349  {
2350  elog(ERROR, "%s: Invalid offset", __func__);
2351  PG_RETURN_NULL();
2352  }
2353  else
2354  {
2355  uwhere = where;
2356  }
2357  }
2358 
2359  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2361  lwline_free(line);
2362 
2363  if (lwline_add_lwpoint(linecopy, point, uwhere) == LW_FAILURE)
2364  {
2365  elog(ERROR, "Point insert failed");
2366  PG_RETURN_NULL();
2367  }
2368 
2369  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2370 
2371  /* Release memory */
2372  PG_FREE_IF_COPY(pglwg1, 0);
2373  PG_FREE_IF_COPY(pglwg2, 1);
2374  lwpoint_free(point);
2375 
2376  PG_RETURN_POINTER(result);
2377 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: gserialized.c:152
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:111
#define LINETYPE
Definition: liblwgeom.h:118
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:117
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:498
uint32_t npoints
Definition: liblwgeom.h:442

References gserialized_get_type(), gserialized_is_empty(), 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: