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

Definition at line 2188 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.

2189 {
2190  GSERIALIZED *pglwg1, *pglwg2, *result;
2191  LWPOINT *point;
2192  LWLINE *line, *linecopy;
2193  int where = -1;
2194 
2195  POSTGIS_DEBUGF(2, "%s called.", __func__);
2196 
2197  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2198  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2199 
2200  if ( PG_NARGS() > 2 )
2201  {
2202  where = PG_GETARG_INT32(2);
2203  }
2204 
2205  if ( gserialized_get_type(pglwg1) != LINETYPE )
2206  {
2207  elog(ERROR, "First argument must be a LINESTRING");
2208  PG_RETURN_NULL();
2209  }
2210 
2211  if ( gserialized_get_type(pglwg2) != POINTTYPE )
2212  {
2213  elog(ERROR, "Second argument must be a POINT");
2214  PG_RETURN_NULL();
2215  }
2216 
2217  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2218 
2219  if ( where == -1 ) where = line->points->npoints;
2220  else if ( where < 0 || where > line->points->npoints )
2221  {
2222  elog(ERROR, "Invalid offset");
2223  PG_RETURN_NULL();
2224  }
2225 
2226  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2228  lwline_free(line);
2229 
2230  if ( lwline_add_lwpoint(linecopy, point, where) == LW_FAILURE )
2231  {
2232  elog(ERROR, "Point insert failed");
2233  PG_RETURN_NULL();
2234  }
2235 
2236  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2237 
2238  /* Release memory */
2239  PG_FREE_IF_COPY(pglwg1, 0);
2240  PG_FREE_IF_COPY(pglwg2, 1);
2241  lwpoint_free(point);
2242 
2243  PG_RETURN_POINTER(result);
2244 
2245 }
#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:69
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:370
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:195
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:446
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:93
#define LW_FAILURE
Definition: liblwgeom.h:78
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:262
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:102
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
int lwline_add_lwpoint(LWLINE *line, LWPOINT *point, int where)
Add a LWPOINT to an LWLINE.
Definition: lwline.c:342
POINTARRAY * points
Definition: liblwgeom.h:421

Here is the call graph for this function: