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

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

2292 {
2293  GSERIALIZED *pglwg1, *pglwg2, *result;
2294  LWGEOM *lwg;
2295  LWLINE *line;
2296  LWPOINT *lwpoint;
2297  POINT4D newpoint;
2298  int32 which;
2299 
2300  POSTGIS_DEBUG(2, "LWGEOM_setpoint_linestring called.");
2301 
2302  /* we copy input as we're going to modify it */
2303  pglwg1 = PG_GETARG_GSERIALIZED_P_COPY(0);
2304 
2305  which = PG_GETARG_INT32(1);
2306  pglwg2 = PG_GETARG_GSERIALIZED_P(2);
2307 
2308 
2309  /* Extract a POINT4D from the point */
2310  lwg = lwgeom_from_gserialized(pglwg2);
2311  lwpoint = lwgeom_as_lwpoint(lwg);
2312  if ( ! lwpoint )
2313  {
2314  elog(ERROR, "Third argument must be a POINT");
2315  PG_RETURN_NULL();
2316  }
2317  getPoint4d_p(lwpoint->point, 0, &newpoint);
2318  lwpoint_free(lwpoint);
2319  PG_FREE_IF_COPY(pglwg2, 2);
2320 
2321  lwg = lwgeom_from_gserialized(pglwg1);
2322  line = lwgeom_as_lwline(lwg);
2323  if ( ! line )
2324  {
2325  elog(ERROR, "First argument must be a LINESTRING");
2326  PG_RETURN_NULL();
2327  }
2328  if(which < 0){
2329  /* Use backward indexing for negative values */
2330  which = which + line->points->npoints ;
2331  }
2332  if ( which > line->points->npoints-1 || which < 0 )
2333  {
2334  elog(ERROR, "abs(Point index) out of range (-)(%d..%d)", 0, line->points->npoints-1);
2335  PG_RETURN_NULL();
2336  }
2337 
2338  /*
2339  * This will change pointarray of the serialized pglwg1,
2340  */
2341  lwline_setPoint4d(line, which, &newpoint);
2342  result = geometry_serialize((LWGEOM *)line);
2343 
2344  /* Release memory */
2345  lwline_free(line);
2346  pfree(pglwg1); /* we forced copy, POINARRAY is released now */
2347 
2348  PG_RETURN_POINTER(result);
2349 }
unsigned int int32
Definition: shpopen.c:273
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
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:93
POINTARRAY * point
Definition: liblwgeom.h:410
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
Definition: lwline.c:380
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:102
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:244
POINTARRAY * points
Definition: liblwgeom.h:421

Here is the call graph for this function: