PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ LWGEOM_addpoint()

Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2164 of file lwgeom_functions_basic.c.

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

Referenced by LWGEOM_makepoint3dm().

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