PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ LWGEOM_setpoint_linestring()

Datum LWGEOM_setpoint_linestring ( PG_FUNCTION_ARGS  )

Definition at line 2423 of file lwgeom_functions_basic.c.

2424 {
2425  GSERIALIZED *pglwg1, *pglwg2, *result;
2426  LWGEOM *lwg;
2427  LWLINE *line;
2428  LWPOINT *lwpoint;
2429  POINT4D newpoint;
2430  int64_t which;
2431 
2432  POSTGIS_DEBUG(2, "LWGEOM_setpoint_linestring called.");
2433 
2434  /* we copy input as we're going to modify it */
2435  pglwg1 = PG_GETARG_GSERIALIZED_P_COPY(0);
2436 
2437  which = PG_GETARG_INT32(1);
2438  pglwg2 = PG_GETARG_GSERIALIZED_P(2);
2439 
2440  /* Extract a POINT4D from the point */
2441  lwg = lwgeom_from_gserialized(pglwg2);
2442  lwpoint = lwgeom_as_lwpoint(lwg);
2443  if (!lwpoint)
2444  {
2445  elog(ERROR, "Third argument must be a POINT");
2446  PG_RETURN_NULL();
2447  }
2448  getPoint4d_p(lwpoint->point, 0, &newpoint);
2449  lwpoint_free(lwpoint);
2450  PG_FREE_IF_COPY(pglwg2, 2);
2451 
2452  lwg = lwgeom_from_gserialized(pglwg1);
2453  line = lwgeom_as_lwline(lwg);
2454 
2455  if (!line)
2456  {
2457  elog(ERROR, "First argument must be a LINESTRING");
2458  PG_RETURN_NULL();
2459  }
2460 
2461  if ( line->points->npoints < 1 ) {
2462  elog(ERROR, "Line has no points");
2463  PG_RETURN_NULL();
2464  }
2465 
2466  if (which < 0)
2467  {
2468  /* Use backward indexing for negative values */
2469  which += (int64_t)line->points->npoints;
2470  }
2471  if ((uint32_t)which > line->points->npoints - 1)
2472  {
2473  elog(ERROR, "abs(Point index) out of range (-)(%u..%u)", 0, line->points->npoints - 1);
2474  PG_RETURN_NULL();
2475  }
2476 
2477  /*
2478  * This will change pointarray of the serialized pglwg1,
2479  */
2480  lwline_setPoint4d(line, (uint32_t)which, &newpoint);
2481  result = geometry_serialize((LWGEOM *)line);
2482 
2483  /* Release memory */
2484  lwline_free(line);
2485  pfree(pglwg1); /* we forced copy, POINARRAY is released now */
2486 
2487  PG_RETURN_POINTER(result);
2488 }
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:179
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:498
POINTARRAY * point
Definition: liblwgeom.h:486
uint32_t npoints
Definition: liblwgeom.h:442

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: