PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ LWGEOM_setpoint_linestring()

Datum LWGEOM_setpoint_linestring ( PG_FUNCTION_ARGS  )

Definition at line 2311 of file lwgeom_functions_basic.c.

2312 {
2313  GSERIALIZED *pglwg1, *pglwg2, *result;
2314  LWGEOM *lwg;
2315  LWLINE *line;
2316  LWPOINT *lwpoint;
2317  POINT4D newpoint;
2318  int64_t which;
2319 
2320  POSTGIS_DEBUG(2, "LWGEOM_setpoint_linestring called.");
2321 
2322  /* we copy input as we're going to modify it */
2323  pglwg1 = PG_GETARG_GSERIALIZED_P_COPY(0);
2324 
2325  which = PG_GETARG_INT32(1);
2326  pglwg2 = PG_GETARG_GSERIALIZED_P(2);
2327 
2328  /* Extract a POINT4D from the point */
2329  lwg = lwgeom_from_gserialized(pglwg2);
2330  lwpoint = lwgeom_as_lwpoint(lwg);
2331  if (!lwpoint)
2332  {
2333  elog(ERROR, "Third argument must be a POINT");
2334  PG_RETURN_NULL();
2335  }
2336  getPoint4d_p(lwpoint->point, 0, &newpoint);
2337  lwpoint_free(lwpoint);
2338  PG_FREE_IF_COPY(pglwg2, 2);
2339 
2340  lwg = lwgeom_from_gserialized(pglwg1);
2341  line = lwgeom_as_lwline(lwg);
2342 
2343  if (!line)
2344  {
2345  elog(ERROR, "First argument must be a LINESTRING");
2346  PG_RETURN_NULL();
2347  }
2348 
2349  if ( line->points->npoints < 1 ) {
2350  elog(ERROR, "Line has no points");
2351  PG_RETURN_NULL();
2352  }
2353 
2354  if (which < 0)
2355  {
2356  /* Use backward indexing for negative values */
2357  which += (int64_t)line->points->npoints;
2358  }
2359  if ((uint32_t)which > line->points->npoints - 1)
2360  {
2361  elog(ERROR, "abs(Point index) out of range (-)(%u..%u)", 0, line->points->npoints - 1);
2362  PG_RETURN_NULL();
2363  }
2364 
2365  /*
2366  * This will change pointarray of the serialized pglwg1,
2367  */
2368  lwline_setPoint4d(line, (uint32_t)which, &newpoint);
2369  result = geometry_serialize((LWGEOM *)line);
2370 
2371  /* Release memory */
2372  lwline_free(line);
2373  pfree(pglwg1); /* we forced copy, POINARRAY is released now */
2374 
2375  PG_RETURN_POINTER(result);
2376 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:162
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:126
void lwline_setPoint4d(LWLINE *line, uint32_t which, POINT4D *newpoint)
Definition: lwline.c:364
void lwline_free(LWLINE *line)
Definition: lwline.c:67
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
POINTARRAY * points
Definition: liblwgeom.h:497
POINTARRAY * point
Definition: liblwgeom.h:485
uint32_t npoints
Definition: liblwgeom.h:441

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

Here is the call graph for this function: