PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ LWGEOM_setpoint_linestring()

Datum LWGEOM_setpoint_linestring ( PG_FUNCTION_ARGS  )

Definition at line 2421 of file lwgeom_functions_basic.c.

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