PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ LWGEOM_addpoint()

Datum LWGEOM_addpoint ( PG_FUNCTION_ARGS  )

Definition at line 2157 of file lwgeom_functions_basic.c.

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

Referenced by LWGEOM_makepoint3dm().

2158 {
2159  GSERIALIZED *pglwg1, *pglwg2, *result;
2160  LWPOINT *point;
2161  LWLINE *line, *linecopy;
2162  int where = -1;
2163 
2164  POSTGIS_DEBUGF(2, "%s called.", __func__);
2165 
2166  pglwg1 = PG_GETARG_GSERIALIZED_P(0);
2167  pglwg2 = PG_GETARG_GSERIALIZED_P(1);
2168 
2169  if ( PG_NARGS() > 2 )
2170  {
2171  where = PG_GETARG_INT32(2);
2172  }
2173 
2174  if ( gserialized_get_type(pglwg1) != LINETYPE )
2175  {
2176  elog(ERROR, "First argument must be a LINESTRING");
2177  PG_RETURN_NULL();
2178  }
2179 
2180  if ( gserialized_get_type(pglwg2) != POINTTYPE )
2181  {
2182  elog(ERROR, "Second argument must be a POINT");
2183  PG_RETURN_NULL();
2184  }
2185 
2186  line = lwgeom_as_lwline(lwgeom_from_gserialized(pglwg1));
2187 
2188  if ( where == -1 ) where = line->points->npoints;
2189  else if ( where < 0 || where > line->points->npoints )
2190  {
2191  elog(ERROR, "Invalid offset");
2192  PG_RETURN_NULL();
2193  }
2194 
2195  point = lwgeom_as_lwpoint(lwgeom_from_gserialized(pglwg2));
2197  lwline_free(line);
2198 
2199  if ( lwline_add_lwpoint(linecopy, point, where) == LW_FAILURE )
2200  {
2201  elog(ERROR, "Point insert failed");
2202  PG_RETURN_NULL();
2203  }
2204 
2205  result = geometry_serialize(lwline_as_lwgeom(linecopy));
2206 
2207  /* Release memory */
2208  PG_FREE_IF_COPY(pglwg1, 0);
2209  PG_FREE_IF_COPY(pglwg2, 1);
2210  lwpoint_free(point);
2211 
2212  PG_RETURN_POINTER(result);
2213 
2214 }
#define LINETYPE
Definition: liblwgeom.h:86
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:86
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:371
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwline_free(LWLINE *line)
Definition: lwline.c:76
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:482
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:129
#define LW_FAILURE
Definition: liblwgeom.h:79
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:298
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
int lwline_add_lwpoint(LWLINE *line, LWPOINT *point, int where)
Add a LWPOINT to an LWLINE.
Definition: lwline.c:342
POINTARRAY * points
Definition: liblwgeom.h:422
Here is the call graph for this function:
Here is the caller graph for this function: