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

◆ LWGEOM_addpoint()

Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2180 of file lwgeom_functions_basic.c.

2181{
2182 GSERIALIZED *pglwg1, *pglwg2, *result;
2183 LWPOINT *point;
2184 LWLINE *line, *linecopy;
2185 uint32_t uwhere = 0;
2186
2187 POSTGIS_DEBUGF(2, "%s called.", __func__);
2188
2189 pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2190 pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2191
2192 if (gserialized_get_type(pglwg1) != LINETYPE)
2193 {
2194 elog(ERROR, "First argument must be a LINESTRING");
2195 PG_RETURN_NULL();
2196 }
2197
2198 if (gserialized_get_type(pglwg2) != POINTTYPE)
2199 {
2200 elog(ERROR, "Second argument must be a POINT");
2201 PG_RETURN_NULL();
2202 }
2203
2205
2206 if (PG_NARGS() <= 2)
2207 {
2208 uwhere = line->points->npoints;
2209 }
2210 else
2211 {
2212 int32 where = PG_GETARG_INT32(2);
2213 if (where == -1)
2214 {
2215 uwhere = line->points->npoints;
2216 }
2217 else if (where < 0 || where > (int32)line->points->npoints)
2218 {
2219 elog(ERROR, "%s: Invalid offset", __func__);
2220 PG_RETURN_NULL();
2221 }
2222 else
2223 {
2224 uwhere = where;
2225 }
2226 }
2227
2230 lwline_free(line);
2231
2232 if (lwline_add_lwpoint(linecopy, point, uwhere) == LW_FAILURE)
2233 {
2234 elog(ERROR, "Point insert failed");
2235 PG_RETURN_NULL();
2236 }
2237
2238 result = geometry_serialize(lwline_as_lwgeom(linecopy));
2239
2240 /* Release memory */
2241 PG_FREE_IF_COPY(pglwg1, 0);
2242 PG_FREE_IF_COPY(pglwg2, 1);
2243 lwpoint_free(point);
2244
2245 PG_RETURN_POINTER(result);
2246}
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition gserialized.c:89
void lwpoint_free(LWPOINT *pt)
Definition lwpoint.c:213
#define LW_FAILURE
Definition liblwgeom.h:110
#define LINETYPE
Definition liblwgeom.h:117
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition liblwgeom.h:116
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition lwgeom.c:321
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition lwgeom.c:161
void lwline_free(LWLINE *line)
Definition lwline.c:67
int lwline_add_lwpoint(LWLINE *line, LWPOINT *point, uint32_t where)
Add a LWPOINT to an LWLINE.
Definition lwline.c:327
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition lwgeom.c:511
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition lwinline.h:121
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition shpopen.c:273
POINTARRAY * points
Definition liblwgeom.h:469
uint32_t npoints
Definition liblwgeom.h:413

References geometry_serialize(), gserialized_get_type(), LINETYPE, LW_FAILURE, lwgeom_as_lwline(), lwgeom_as_lwpoint(), lwgeom_clone_deep(), lwgeom_from_gserialized(), lwline_add_lwpoint(), lwline_as_lwgeom(), lwline_free(), lwpoint_free(), POINTARRAY::npoints, LWLINE::points, and POINTTYPE.

Here is the call graph for this function: