PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ LWGEOM_setpoint_linestring()

Datum LWGEOM_setpoint_linestring ( PG_FUNCTION_ARGS  )

Definition at line 2390 of file lwgeom_functions_basic.c.

2391 {
2392  GSERIALIZED *pglwg1, *pglwg2, *result;
2393  LWGEOM *lwg;
2394  LWLINE *line;
2395  LWPOINT *lwpoint;
2396  POINT4D newpoint;
2397  int64_t which;
2398 
2399  POSTGIS_DEBUG(2, "LWGEOM_setpoint_linestring called.");
2400 
2401  /* we copy input as we're going to modify it */
2402  pglwg1 = PG_GETARG_GSERIALIZED_P_COPY(0);
2403 
2404  which = PG_GETARG_INT32(1);
2405  pglwg2 = PG_GETARG_GSERIALIZED_P(2);
2406 
2407  /* Extract a POINT4D from the point */
2408  lwg = lwgeom_from_gserialized(pglwg2);
2409  lwpoint = lwgeom_as_lwpoint(lwg);
2410  if (!lwpoint)
2411  {
2412  elog(ERROR, "Third argument must be a POINT");
2413  PG_RETURN_NULL();
2414  }
2415  getPoint4d_p(lwpoint->point, 0, &newpoint);
2416  lwpoint_free(lwpoint);
2417  PG_FREE_IF_COPY(pglwg2, 2);
2418 
2419  lwg = lwgeom_from_gserialized(pglwg1);
2420  line = lwgeom_as_lwline(lwg);
2421 
2422  if (!line)
2423  {
2424  elog(ERROR, "First argument must be a LINESTRING");
2425  PG_RETURN_NULL();
2426  }
2427 
2428  if ( line->points->npoints < 1 ) {
2429  elog(ERROR, "Line has no points");
2430  PG_RETURN_NULL();
2431  }
2432 
2433  if (which < 0)
2434  {
2435  /* Use backward indexing for negative values */
2436  which += (int64_t)line->points->npoints;
2437  }
2438  if ((uint32_t)which > line->points->npoints - 1)
2439  {
2440  elog(ERROR, "abs(Point index) out of range (-)(%u..%u)", 0, line->points->npoints - 1);
2441  PG_RETURN_NULL();
2442  }
2443 
2444  /*
2445  * This will change pointarray of the serialized pglwg1,
2446  */
2447  lwline_setPoint4d(line, (uint32_t)which, &newpoint);
2448  result = geometry_serialize((LWGEOM *)line);
2449 
2450  /* Release memory */
2451  lwline_free(line);
2452  pfree(pglwg1); /* we forced copy, POINARRAY is released now */
2453 
2454  PG_RETURN_POINTER(result);
2455 }
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:268
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:125
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:127
POINTARRAY * points
Definition: liblwgeom.h:483
POINTARRAY * point
Definition: liblwgeom.h:471
uint32_t npoints
Definition: liblwgeom.h:427

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: