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

◆ LWGEOM_setpoint_linestring()

Datum LWGEOM_setpoint_linestring ( PG_FUNCTION_ARGS  )

Definition at line 2292 of file lwgeom_functions_basic.c.

2293{
2294 GSERIALIZED *pglwg1, *pglwg2, *result;
2295 LWGEOM *lwg;
2296 LWLINE *line;
2297 LWPOINT *lwpoint;
2298 POINT4D newpoint;
2299 int64_t which;
2300
2301 POSTGIS_DEBUG(2, "LWGEOM_setpoint_linestring called.");
2302
2303 /* we copy input as we're going to modify it */
2304 pglwg1 = PG_GETARG_GSERIALIZED_P_COPY(0);
2305
2306 which = PG_GETARG_INT32(1);
2307 pglwg2 = PG_GETARG_GSERIALIZED_P(2);
2308
2309 /* Extract a POINT4D from the point */
2310 lwg = lwgeom_from_gserialized(pglwg2);
2311 lwpoint = lwgeom_as_lwpoint(lwg);
2312 if (!lwpoint)
2313 {
2314 elog(ERROR, "Third argument must be a POINT");
2315 PG_RETURN_NULL();
2316 }
2317 getPoint4d_p(lwpoint->point, 0, &newpoint);
2318 lwpoint_free(lwpoint);
2319 PG_FREE_IF_COPY(pglwg2, 2);
2320
2321 lwg = lwgeom_from_gserialized(pglwg1);
2322 line = lwgeom_as_lwline(lwg);
2323
2324 if (!line)
2325 {
2326 elog(ERROR, "First argument must be a LINESTRING");
2327 PG_RETURN_NULL();
2328 }
2329
2330 if ( line->points->npoints < 1 ) {
2331 elog(ERROR, "Line has no points");
2332 PG_RETURN_NULL();
2333 }
2334
2335 if (which < 0)
2336 {
2337 /* Use backward indexing for negative values */
2338 which += (int64_t)line->points->npoints;
2339 }
2340 if ((uint32_t)which > line->points->npoints - 1)
2341 {
2342 elog(ERROR, "abs(Point index) out of range (-)(%u..%u)", 0, line->points->npoints - 1);
2343 PG_RETURN_NULL();
2344 }
2345
2346 /*
2347 * This will change pointarray of the serialized pglwg1,
2348 */
2349 lwline_setPoint4d(line, (uint32_t)which, &newpoint);
2350 result = geometry_serialize((LWGEOM *)line);
2351
2352 /* Release memory */
2353 lwline_free(line);
2354 pfree(pglwg1); /* we forced copy, POINARRAY is released now */
2355
2356 PG_RETURN_POINTER(result);
2357}
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:161
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:121
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
POINTARRAY * points
Definition liblwgeom.h:469
POINTARRAY * point
Definition liblwgeom.h:457
uint32_t npoints
Definition liblwgeom.h:413

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

Here is the call graph for this function: