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

Definition at line 2268 of file lwgeom_functions_basic.c.

References geometry_serialize(), getPoint4d_p(), lwgeom_as_lwline(), lwgeom_as_lwpoint(), lwgeom_from_gserialized(), lwline_free(), lwline_setPoint4d(), lwpoint_free(), POINTARRAY::npoints, LWPOINT::point, and LWLINE::points.

2269 {
2270  GSERIALIZED *pglwg1, *pglwg2, *result;
2271  LWGEOM *lwg;
2272  LWLINE *line;
2273  LWPOINT *lwpoint;
2274  POINT4D newpoint;
2275  int32 which;
2276 
2277  POSTGIS_DEBUG(2, "LWGEOM_setpoint_linestring called.");
2278 
2279  /* we copy input as we're going to modify it */
2280  pglwg1 = PG_GETARG_GSERIALIZED_P_COPY(0);
2281 
2282  which = PG_GETARG_INT32(1);
2283  pglwg2 = PG_GETARG_GSERIALIZED_P(2);
2284 
2285 
2286  /* Extract a POINT4D from the point */
2287  lwg = lwgeom_from_gserialized(pglwg2);
2288  lwpoint = lwgeom_as_lwpoint(lwg);
2289  if ( ! lwpoint )
2290  {
2291  elog(ERROR, "Third argument must be a POINT");
2292  PG_RETURN_NULL();
2293  }
2294  getPoint4d_p(lwpoint->point, 0, &newpoint);
2295  lwpoint_free(lwpoint);
2296  PG_FREE_IF_COPY(pglwg2, 2);
2297 
2298  lwg = lwgeom_from_gserialized(pglwg1);
2299  line = lwgeom_as_lwline(lwg);
2300  if ( ! line )
2301  {
2302  elog(ERROR, "First argument must be a LINESTRING");
2303  PG_RETURN_NULL();
2304  }
2305  if(which < 0){
2306  /* Use backward indexing for negative values */
2307  which = which + line->points->npoints ;
2308  }
2309  if ( (uint32_t)which + 1 > line->points->npoints )
2310  {
2311  elog(ERROR, "abs(Point index) out of range (-)(%d..%d)", 0, line->points->npoints-1);
2312  PG_RETURN_NULL();
2313  }
2314 
2315  /*
2316  * This will change pointarray of the serialized pglwg1,
2317  */
2318  lwline_setPoint4d(line, (uint32_t)which, &newpoint);
2319  result = geometry_serialize((LWGEOM *)line);
2320 
2321  /* Release memory */
2322  lwline_free(line);
2323  pfree(pglwg1); /* we forced copy, POINARRAY is released now */
2324 
2325  PG_RETURN_POINTER(result);
2326 }
unsigned int int32
Definition: shpopen.c:273
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
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:160
POINTARRAY * point
Definition: liblwgeom.h:410
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
Definition: lwline.c:373
unsigned int uint32_t
Definition: uthash.h:78
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:169
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:113
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
POINTARRAY * points
Definition: liblwgeom.h:421
uint32_t npoints
Definition: liblwgeom.h:370

Here is the call graph for this function: