PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2161 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(), lwline_add_lwpoint(), lwline_as_lwgeom(), lwline_free(), lwpoint_free(), POINTARRAY::npoints, LWLINE::points, and POINTTYPE.

2162 {
2163  GSERIALIZED *pglwg1, *pglwg2, *result;
2164  LWPOINT *point;
2165  LWLINE *line, *linecopy;
2166  int32 where = -1;
2167 
2168  POSTGIS_DEBUGF(2, "%s called.", __func__);
2169 
2170  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2171  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2172 
2173 
2174  if ( gserialized_get_type(pglwg1) != LINETYPE )
2175  {
2176  elog(ERROR, "First argument must be a LINESTRING");
2177  PG_RETURN_NULL();
2178  }
2179 
2180  if ( gserialized_get_type(pglwg2) != POINTTYPE )
2181  {
2182  elog(ERROR, "Second argument must be a POINT");
2183  PG_RETURN_NULL();
2184  }
2185 
2186  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2187 
2188  if ( PG_NARGS() > 2 )
2189  {
2190  where = PG_GETARG_INT32(2);
2191  }
2192  else
2193  {
2194  where = line->points->npoints;
2195  }
2196 
2197  if ( where < 0 || where > (int32) line->points->npoints )
2198  {
2199  elog(ERROR, "Invalid offset");
2200  PG_RETURN_NULL();
2201  }
2202 
2203  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2205  lwline_free(line);
2206 
2207  if ( lwline_add_lwpoint(linecopy, point, (uint32_t) where) == LW_FAILURE )
2208  {
2209  elog(ERROR, "Point insert failed");
2210  PG_RETURN_NULL();
2211  }
2212 
2213  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2214 
2215  /* Release memory */
2216  PG_FREE_IF_COPY(pglwg1, 0);
2217  PG_FREE_IF_COPY(pglwg2, 1);
2218  lwpoint_free(point);
2219 
2220  PG_RETURN_POINTER(result);
2221 
2222 }
#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:513
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:160
#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:329
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:169
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
POINTARRAY * points
Definition: liblwgeom.h:421
uint32_t npoints
Definition: liblwgeom.h:370

Here is the call graph for this function: