PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ LWGEOM_setpoint_linestring()

Datum LWGEOM_setpoint_linestring ( PG_FUNCTION_ARGS  )

Definition at line 2417 of file lwgeom_functions_basic.c.

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