PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum LWGEOM_dumppoints ( PG_FUNCTION_ARGS  )

Definition at line 50 of file lwgeom_dumppoints.c.

References dumpstate::align, dumpstate::byval, CIRCSTRINGTYPE, 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(), LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, dumpstate::path, dumpstate::pathlen, LWLINE::points, LWTRIANGLE::points, LWCIRCSTRING::points, POINTTYPE, POLYGONTYPE, dumpstate::pt, result, dumpstate::ring, LWPOLY::rings, dumpstate::root, LWTRIANGLE::srid, LWPOLY::srid, dumpstate::stack, dumpstate::stacklen, TRIANGLETYPE, LWGEOM::type, and dumpstate::typlen.

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

Here is the call graph for this function: