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

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

2129 {
2130  GSERIALIZED *pglwg1, *pglwg2, *result;
2131  LWPOINT *point;
2132  LWLINE *line, *linecopy;
2133  int where = -1;
2134 
2135  POSTGIS_DEBUGF(2, "%s called.", __func__);
2136 
2137  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2138  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2139 
2140  if ( PG_NARGS() > 2 )
2141  {
2142  where = PG_GETARG_INT32(2);
2143  }
2144 
2145  if ( gserialized_get_type(pglwg1) != LINETYPE )
2146  {
2147  elog(ERROR, "First argument must be a LINESTRING");
2148  PG_RETURN_NULL();
2149  }
2150 
2151  if ( gserialized_get_type(pglwg2) != POINTTYPE )
2152  {
2153  elog(ERROR, "Second argument must be a POINT");
2154  PG_RETURN_NULL();
2155  }
2156 
2157  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2158 
2159  if ( where == -1 ) where = line->points->npoints;
2160  else if ( where < 0 || where > line->points->npoints )
2161  {
2162  elog(ERROR, "Invalid offset");
2163  PG_RETURN_NULL();
2164  }
2165 
2166  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2168  lwline_free(line);
2169 
2170  if ( lwline_add_lwpoint(linecopy, point, where) == LW_FAILURE )
2171  {
2172  elog(ERROR, "Point insert failed");
2173  PG_RETURN_NULL();
2174  }
2175 
2176  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2177 
2178  /* Release memory */
2179  PG_FREE_IF_COPY(pglwg1, 0);
2180  PG_FREE_IF_COPY(pglwg2, 1);
2181  lwpoint_free(point);
2182 
2183  PG_RETURN_POINTER(result);
2184 
2185 }
#define LINETYPE
Definition: liblwgeom.h:71
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:55
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:355
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:182
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:433
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:80
#define LW_FAILURE
Definition: liblwgeom.h:64
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:70
int lwline_add_lwpoint(LWLINE *line, LWPOINT *point, int where)
Add a LWPOINT to an LWLINE.
Definition: lwline.c:313
POINTARRAY * points
Definition: liblwgeom.h:406

Here is the call graph for this function: