PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ LWGEOM_dumppoints()

Datum LWGEOM_dumppoints ( PG_FUNCTION_ARGS  )

Definition at line 75 of file lwgeom_dumppoints.c.

References dumpstate::align, dumpstate::byval, CIRCSTRINGTYPE, COLLECTIONTYPE, COMPOUNDTYPE, CURVEPOLYTYPE, POINTARRAY::flags, FLAGS_GET_M, FLAGS_GET_Z, dumpnode::geom, LWCOLLECTION::geoms, getPoint4d_p(), gserialized_from_lwgeom(), dumpnode::idx, if(), LINETYPE, lwalloc(), lwcircstring_get_lwpoint(), lwgeom_as_lwcircstring(), lwgeom_as_lwline(), lwgeom_as_lwpoint(), lwgeom_as_lwpoly(), lwgeom_as_lwtriangle(), lwgeom_from_gserialized(), lwgeom_is_collection(), lwgeom_is_empty(), lwline_get_lwpoint(), lwpoint_make(), MULTICURVETYPE, MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, MULTISURFACETYPE, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, dumpstate::path, dumpstate::pathlen, LWLINE::points, LWTRIANGLE::points, LWCIRCSTRING::points, POINTTYPE, POLYGONTYPE, POLYHEDRALSURFACETYPE, dumpstate::pt, dumpstate::ring, LWPOLY::rings, dumpstate::root, LWTRIANGLE::srid, LWPOLY::srid, dumpstate::stack, dumpstate::stacklen, TINTYPE, TRIANGLETYPE, LWGEOM::type, and dumpstate::typlen.

75  {
76  FuncCallContext *funcctx;
77  MemoryContext oldcontext, newcontext;
78 
79  GSERIALIZED *pglwgeom;
80  LWCOLLECTION *lwcoll;
81  LWGEOM *lwgeom;
82  struct dumpstate *state;
83  struct dumpnode *node;
84 
85  HeapTuple tuple;
86  Datum pathpt[2]; /* used to construct the composite return value */
87  bool isnull[2] = {0,0}; /* needed to say neither value is null */
88  Datum result; /* the actual composite return value */
89 
90  if (SRF_IS_FIRSTCALL()) {
91  funcctx = SRF_FIRSTCALL_INIT();
92 
93  newcontext = funcctx->multi_call_memory_ctx;
94  oldcontext = MemoryContextSwitchTo(newcontext);
95 
96  /* get a local copy of what we're doing a dump points on */
97  pglwgeom = PG_GETARG_GSERIALIZED_P_COPY(0);
98  lwgeom = lwgeom_from_gserialized(pglwgeom);
99 
100  /* return early if nothing to do */
101  if (!lwgeom || lwgeom_is_empty(lwgeom)) {
102  MemoryContextSwitchTo(oldcontext);
103  funcctx = SRF_PERCALL_SETUP();
104  SRF_RETURN_DONE(funcctx);
105  }
106 
107  /* Create function state */
108  state = lwalloc(sizeof *state);
109  state->root = lwgeom;
110  state->stacklen = 0;
111  state->pathlen = 0;
112  state->pt = 0;
113  state->ring = 0;
114 
115  funcctx->user_fctx = state;
116 
117  /*
118  * Push a struct dumpnode on the state stack
119  */
120 
121  state->stack[0].idx = 0;
122  state->stack[0].geom = lwgeom;
123  state->stacklen++;
124 
125  /*
126  * get tuple description for return type
127  */
128  if (get_call_result_type(fcinfo, 0, &funcctx->tuple_desc) != TYPEFUNC_COMPOSITE) {
129  ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
130  errmsg("set-valued function called in context that cannot accept a set")));
131  }
132 
133  BlessTupleDesc(funcctx->tuple_desc);
134 
135  /* get and cache data for constructing int4 arrays */
136  get_typlenbyvalalign(INT4OID, &state->typlen, &state->byval, &state->align);
137 
138  MemoryContextSwitchTo(oldcontext);
139  }
140 
141  /* stuff done on every call of the function */
142  funcctx = SRF_PERCALL_SETUP();
143  newcontext = funcctx->multi_call_memory_ctx;
144 
145  /* get state */
146  state = funcctx->user_fctx;
147 
148  while (1) {
149  node = &state->stack[state->stacklen-1];
150  lwgeom = node->geom;
151 
152  /* need to return a point from this geometry */
153  if (!lwgeom_is_collection(lwgeom)) {
154  /* either return a point, or pop the stack */
155  /* TODO use a union? would save a tiny amount of stack space.
156  * probably not worth the bother
157  */
158  LWLINE *line;
159  LWCIRCSTRING *circ;
160  LWPOLY *poly;
161  LWTRIANGLE *tri;
162  LWPOINT *lwpoint = NULL;
163  POINT4D pt;
164 
165  /*
166  * net result of switch should be to set lwpoint to the
167  * next point to return, or leave at NULL if there
168  * are no more points in the geometry
169  */
170  switch(lwgeom->type) {
171  case TRIANGLETYPE:
172  tri = lwgeom_as_lwtriangle(lwgeom);
173  if (state->pt == 0) {
174  state->path[state->pathlen++] = Int32GetDatum(state->ring+1);
175  }
176  if (state->pt <= 3) {
177  getPoint4d_p(tri->points, state->pt, &pt);
178  lwpoint = lwpoint_make(tri->srid,
179  FLAGS_GET_Z(tri->points->flags),
180  FLAGS_GET_M(tri->points->flags),
181  &pt);
182  }
183  if (state->pt > 3) {
184  state->pathlen--;
185  }
186  break;
187  case POLYGONTYPE:
188  poly = lwgeom_as_lwpoly(lwgeom);
189  if (state->pt == poly->rings[state->ring]->npoints) {
190  state->pt = 0;
191  state->ring++;
192  state->pathlen--;
193  }
194  if (state->pt == 0 && state->ring < poly->nrings) {
195  /* handle new ring */
196  state->path[state->pathlen] = Int32GetDatum(state->ring+1);
197  state->pathlen++;
198  }
199  if (state->ring == poly->nrings) {
200  } else {
201  /* TODO should be able to directly get the point
202  * into the point array of a fixed lwpoint
203  */
204  /* can't get the point directly from the ptarray because
205  * it might be aligned wrong, so at least one memcpy
206  * seems unavoidable
207  * It might be possible to pass it directly to gserialized
208  * depending how that works, it might effectively be gserialized
209  * though a brief look at the code indicates not
210  */
211  getPoint4d_p(poly->rings[state->ring], state->pt, &pt);
212  lwpoint = lwpoint_make(poly->srid,
213  FLAGS_GET_Z(poly->rings[state->ring]->flags),
214  FLAGS_GET_M(poly->rings[state->ring]->flags),
215  &pt);
216  }
217  break;
218  case POINTTYPE:
219  if (state->pt == 0) lwpoint = lwgeom_as_lwpoint(lwgeom);
220  break;
221  case LINETYPE:
222  line = lwgeom_as_lwline(lwgeom);
223  if (line->points && state->pt <= line->points->npoints) {
224  lwpoint = lwline_get_lwpoint((LWLINE*)lwgeom, state->pt);
225  }
226  break;
227  case CIRCSTRINGTYPE:
228  circ = lwgeom_as_lwcircstring(lwgeom);
229  if (circ->points && state->pt <= circ->points->npoints) {
230  lwpoint = lwcircstring_get_lwpoint((LWCIRCSTRING*)lwgeom, state->pt);
231  }
232  break;
233  default:
234  ereport(ERROR, (errcode(ERRCODE_DATA_EXCEPTION),
235  errmsg("Invalid Geometry type %d passed to ST_DumpPoints()", lwgeom->type)));
236  }
237 
238  /*
239  * At this point, lwpoint is either NULL, in which case
240  * we need to pop the geometry stack and get the next
241  * geometry, if amy, or lwpoint is set and we construct
242  * a record type with the integer array of geometry
243  * indexes and the point number, and the actual point
244  * geometry itself
245  */
246 
247  if (!lwpoint) {
248  /* no point, so pop the geom and look for more */
249  if (--state->stacklen == 0) SRF_RETURN_DONE(funcctx);
250  state->pathlen--;
251  continue;
252  } else {
253  /* write address of current geom/pt */
254  state->pt++;
255 
256  state->path[state->pathlen] = Int32GetDatum(state->pt);
257  pathpt[0] = PointerGetDatum(construct_array(state->path, state->pathlen+1,
258  INT4OID, state->typlen, state->byval, state->align));
259 
260  pathpt[1] = PointerGetDatum(gserialized_from_lwgeom((LWGEOM*)lwpoint,0));
261 
262  tuple = heap_form_tuple(funcctx->tuple_desc, pathpt, isnull);
263  result = HeapTupleGetDatum(tuple);
264  SRF_RETURN_NEXT(funcctx, result);
265  }
266  }
267 
268  lwcoll = (LWCOLLECTION*)node->geom;
269 
270  /* if a collection and we have more geoms */
271  if (node->idx < lwcoll->ngeoms) {
272  /* push the next geom on the path and the stack */
273  lwgeom = lwcoll->geoms[node->idx++];
274  state->path[state->pathlen++] = Int32GetDatum(node->idx);
275 
276  node = &state->stack[state->stacklen++];
277  node->idx = 0;
278  node->geom = lwgeom;
279 
280  state->pt = 0;
281  state->ring = 0;
282 
283  /* loop back to beginning, which will then check whatever node we just pushed */
284  continue;
285  }
286 
287  /* no more geometries in the current collection */
288  if (--state->stacklen == 0) SRF_RETURN_DONE(funcctx);
289  state->pathlen--;
290  state->stack[state->stacklen-1].idx++;
291  }
292 }
#define LINETYPE
Definition: liblwgeom.h:85
POINTARRAY * points
Definition: liblwgeom.h:432
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1004
Datum path[34]
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:370
#define POLYGONTYPE
Definition: liblwgeom.h:86
#define TRIANGLETYPE
Definition: liblwgeom.h:97
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:138
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:93
LWGEOM * geom
uint8_t flags
Definition: liblwgeom.h:368
LWGEOM ** geoms
Definition: liblwgeom.h:508
LWPOINT * lwpoint_make(int srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:188
POINTARRAY ** rings
Definition: liblwgeom.h:456
int nrings
Definition: liblwgeom.h:454
#define FLAGS_GET_Z(flags)
Macros for manipulating the &#39;flags&#39; byte.
Definition: liblwgeom.h:139
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:102
LWCIRCSTRING * lwgeom_as_lwcircstring(const LWGEOM *lwgeom)
Definition: lwgeom.c:111
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Definition: lwgeom.c:147
int32_t srid
Definition: liblwgeom.h:453
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
LWPOINT * lwline_get_lwpoint(const LWLINE *line, int where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:324
uint8_t type
Definition: liblwgeom.h:395
POINTARRAY * points
Definition: liblwgeom.h:443
struct dumpnode stack[MAXDEPTH]
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:91
void * lwalloc(size_t size)
Definition: lwutil.c:227
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1310
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
Definition: g_serialized.c:933
LWPOINT * lwcircstring_get_lwpoint(const LWCIRCSTRING *circ, int where)
Definition: lwcircstring.c:300
if(!(yy_init))
int32_t srid
Definition: liblwgeom.h:431
LWGEOM * root
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:244
POINTARRAY * points
Definition: liblwgeom.h:421
Here is the call graph for this function: