PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ LWGEOM_addpoint()

Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2164 of file lwgeom_functions_basic.c.

2165 {
2166  GSERIALIZED *pglwg1, *pglwg2, *result;
2167  LWPOINT *point;
2168  LWLINE *line, *linecopy;
2169  uint32_t uwhere = 0;
2170 
2171  POSTGIS_DEBUGF(2, "%s called.", __func__);
2172 
2173  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2174  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2175 
2176  if (gserialized_get_type(pglwg1) != LINETYPE)
2177  {
2178  elog(ERROR, "First argument must be a LINESTRING");
2179  PG_RETURN_NULL();
2180  }
2181 
2182  if (gserialized_get_type(pglwg2) != POINTTYPE)
2183  {
2184  elog(ERROR, "Second argument must be a POINT");
2185  PG_RETURN_NULL();
2186  }
2187 
2188  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2189 
2190  if (PG_NARGS() <= 2)
2191  {
2192  uwhere = line->points->npoints;
2193  }
2194  else
2195  {
2196  int32 where = PG_GETARG_INT32(2);
2197  if (where == -1)
2198  {
2199  uwhere = line->points->npoints;
2200  }
2201  else if (where < 0 || where > (int32)line->points->npoints)
2202  {
2203  elog(ERROR, "%s: Invalid offset", __func__);
2204  PG_RETURN_NULL();
2205  }
2206  else
2207  {
2208  uwhere = where;
2209  }
2210  }
2211 
2212  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2214  lwline_free(line);
2215 
2216  if (lwline_add_lwpoint(linecopy, point, uwhere) == LW_FAILURE)
2217  {
2218  elog(ERROR, "Point insert failed");
2219  PG_RETURN_NULL();
2220  }
2221 
2222  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2223 
2224  /* Release memory */
2225  PG_FREE_IF_COPY(pglwg1, 0);
2226  PG_FREE_IF_COPY(pglwg2, 1);
2227  lwpoint_free(point);
2228 
2229  PG_RETURN_POINTER(result);
2230 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
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
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:170
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:330
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
#define LW_FAILURE
Definition: liblwgeom.h:79
#define LINETYPE
Definition: liblwgeom.h:86
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:520
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
void lwline_free(LWLINE *line)
Definition: lwline.c:76
int lwline_add_lwpoint(LWLINE *line, LWPOINT *point, uint32_t where)
Add a LWPOINT to an LWLINE.
Definition: lwline.c:336
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition: shpopen.c:273
POINTARRAY * points
Definition: liblwgeom.h:425
uint32_t npoints
Definition: liblwgeom.h:374
unsigned int uint32_t
Definition: uthash.h:78

References geometry_serialize(), 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, and POINTTYPE.

Here is the call graph for this function: