PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ LWGEOM_addpoint()

Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2194 of file lwgeom_functions_basic.c.

2195 {
2196  GSERIALIZED *pglwg1, *pglwg2, *result;
2197  LWPOINT *point;
2198  LWLINE *line, *linecopy;
2199  uint32_t uwhere = 0;
2200 
2201  POSTGIS_DEBUGF(2, "%s called.", __func__);
2202 
2203  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2204  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2205 
2206  if (gserialized_get_type(pglwg1) != LINETYPE)
2207  {
2208  elog(ERROR, "First argument must be a LINESTRING");
2209  PG_RETURN_NULL();
2210  }
2211 
2212  if (gserialized_get_type(pglwg2) != POINTTYPE)
2213  {
2214  elog(ERROR, "Second argument must be a POINT");
2215  PG_RETURN_NULL();
2216  }
2217 
2218  if (gserialized_is_empty(pglwg2))
2219  {
2220  PG_RETURN_POINTER(pglwg1);
2221  }
2222 
2223  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2224 
2225  if (PG_NARGS() <= 2)
2226  {
2227  uwhere = line->points->npoints;
2228  }
2229  else
2230  {
2231  int32 where = PG_GETARG_INT32(2);
2232  if (where == -1)
2233  {
2234  uwhere = line->points->npoints;
2235  }
2236  else if (where < 0 || where > (int32)line->points->npoints)
2237  {
2238  elog(ERROR, "%s: Invalid offset", __func__);
2239  PG_RETURN_NULL();
2240  }
2241  else
2242  {
2243  uwhere = where;
2244  }
2245  }
2246 
2247  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2249  lwline_free(line);
2250 
2251  if (lwline_add_lwpoint(linecopy, point, uwhere) == LW_FAILURE)
2252  {
2253  elog(ERROR, "Point insert failed");
2254  PG_RETURN_NULL();
2255  }
2256 
2257  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2258 
2259  /* Release memory */
2260  PG_FREE_IF_COPY(pglwg1, 0);
2261  PG_FREE_IF_COPY(pglwg2, 1);
2262  lwpoint_free(point);
2263 
2264  PG_RETURN_POINTER(result);
2265 }
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:162
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
#define LW_FAILURE
Definition: liblwgeom.h:110
#define LINETYPE
Definition: liblwgeom.h:117
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:512
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
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:497
uint32_t npoints
Definition: liblwgeom.h:441

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: