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

Definition at line 2193 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, POINTTYPE, and result.

2194 {
2195  GSERIALIZED *pglwg1, *pglwg2, *result;
2196  LWPOINT *point;
2197  LWLINE *line, *linecopy;
2198  int where = -1;
2199 
2200  POSTGIS_DEBUG(2, "LWGEOM_addpoint called.");
2201 
2202  pglwg1 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
2203  pglwg2 = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
2204 
2205  if ( PG_NARGS() > 2 )
2206  {
2207  where = PG_GETARG_INT32(2);
2208  }
2209 
2210  if ( gserialized_get_type(pglwg1) != LINETYPE )
2211  {
2212  elog(ERROR, "First argument must be a LINESTRING");
2213  PG_RETURN_NULL();
2214  }
2215 
2216  if ( gserialized_get_type(pglwg2) != POINTTYPE )
2217  {
2218  elog(ERROR, "Second argument must be a POINT");
2219  PG_RETURN_NULL();
2220  }
2221 
2222  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2223 
2224  if ( where == -1 ) where = line->points->npoints;
2225  else if ( where < 0 || where > line->points->npoints )
2226  {
2227  elog(ERROR, "Invalid offset");
2228  PG_RETURN_NULL();
2229  }
2230 
2231  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2233  lwline_free(line);
2234 
2235  if ( lwline_add_lwpoint(linecopy, point, where) == LW_FAILURE )
2236  {
2237  elog(ERROR, "Point insert failed");
2238  PG_RETURN_NULL();
2239  }
2240 
2241  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2242 
2243  /* Release memory */
2244  PG_FREE_IF_COPY(pglwg1, 0);
2245  PG_FREE_IF_COPY(pglwg2, 1);
2246  lwpoint_free(point);
2247 
2248  PG_RETURN_POINTER(result);
2249 
2250 }
#define LINETYPE
Definition: liblwgeom.h:61
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:56
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:327
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:180
void lwline_free(LWLINE *line)
Definition: lwline.c:63
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:389
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:80
char ** result
Definition: liblwgeom.h:218
#define LW_FAILURE
Definition: liblwgeom.h:54
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:89
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
int lwline_add_lwpoint(LWLINE *line, LWPOINT *point, int where)
Add a LWPOINT to an LWLINE.
Definition: lwline.c:312
POINTARRAY * points
Definition: liblwgeom.h:378

Here is the call graph for this function: