PostGIS  2.5.7dev-r@@SVN_REVISION@@
lwiterator.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright 2015 Daniel Baston <dbaston@gmail.com>
22  *
23  **********************************************************************/
24 
25 
26 #include "liblwgeom.h"
27 #include "lwgeom_log.h"
28 
29 struct LISTNODE
30 {
31  struct LISTNODE* next;
32  void* item;
33 };
34 typedef struct LISTNODE LISTNODE;
35 
36 /* The LWPOINTITERATOR consists of two stacks of items to process: a stack
37  * of geometries, and a stack of POINTARRAYs extracted from those geometries.
38  * The index "i" refers to the "next" point, which is found at the top of the
39  * pointarrays stack.
40  *
41  * When the pointarrays stack is depleted, we pull a geometry from the geometry
42  * stack to replenish it.
43  */
45 {
50 };
51 
52 static LISTNODE*
53 prepend_node(void* g, LISTNODE* front)
54 {
55  LISTNODE* n = lwalloc(sizeof(LISTNODE));
56  n->item = g;
57  n->next = front;
58 
59  return n;
60 }
61 
62 static LISTNODE*
64 {
65  LISTNODE* next = i->next;
66  lwfree(i);
67  return next;
68 }
69 
70 static int
72 {
73  if (lwgeom_is_empty(g))
74  return LW_FAILURE;
75 
76  s->geoms = prepend_node(g, s->geoms);
77  return LW_SUCCESS;
78 }
79 
83 static LISTNODE*
85 {
86  switch(lwgeom_get_type(g))
87  {
88  case POINTTYPE:
89  return prepend_node(lwgeom_as_lwpoint(g)->point, NULL);
90  case LINETYPE:
91  return prepend_node(lwgeom_as_lwline(g)->points, NULL);
92  case TRIANGLETYPE:
93  return prepend_node(lwgeom_as_lwtriangle(g)->points, NULL);
94  case CIRCSTRINGTYPE:
95  return prepend_node(lwgeom_as_lwcircstring(g)->points, NULL);
96  case POLYGONTYPE:
97  {
98  LISTNODE* n = NULL;
99 
100  LWPOLY* p = lwgeom_as_lwpoly(g);
101  int i;
102  for (i = p->nrings - 1; i >= 0; i--)
103  {
104  n = prepend_node(p->rings[i], n);
105  }
106 
107  return n;
108  }
109  default:
110  lwerror("Unsupported geometry type for lwpointiterator");
111  }
112 
113  return NULL;
114 }
115 
119 static void
121 {
122  int i;
123  LWCOLLECTION* c;
124 
125  if (!s->geoms)
126  {
127  return;
128  }
129 
130  c = (LWCOLLECTION*) s->geoms->item;
131  s->geoms = pop_node(s->geoms);
132 
133  for (i = c->ngeoms - 1; i >= 0; i--)
134  {
135  LWGEOM* g = lwcollection_getsubgeom(c, i);
136 
138  }
139 }
140 
144 static void
146 {
147  while(s->geoms && lwgeom_is_collection(s->geoms->item))
148  {
150  }
151 }
152 
153 static int
155 {
156  s->i += 1;
157 
158  /* We've reached the end of our current POINTARRAY. Try to see if there
159  * are any more POINTARRAYS on the stack. */
160  if (s->pointarrays && s->i >= ((POINTARRAY*) s->pointarrays->item)->npoints)
161  {
162  s->pointarrays = pop_node(s->pointarrays);
163  s->i = 0;
164  }
165 
166  /* We don't have a current POINTARRAY. Pull a geometry from the stack, and
167  * decompose it into its POINTARRARYs. */
168  if (!s->pointarrays)
169  {
170  LWGEOM* g;
172 
173  if (!s->geoms)
174  {
175  return LW_FAILURE;
176  }
177 
178  s->i = 0;
179  g = s->geoms->item;
180  s->pointarrays = extract_pointarrays_from_lwgeom(g);
181 
182  s->geoms = pop_node(s->geoms);
183  }
184 
185  if (!s->pointarrays)
186  {
187  return LW_FAILURE;
188  }
189  return LW_SUCCESS;
190 }
191 
192 /* Public API implementation */
193 
194 int
196 {
198  return LW_FAILURE;
199 
200  return getPoint4d_p(s->pointarrays->item, s->i, p);
201 }
202 
203 int
205 {
206  if (s->pointarrays && s->i < ((POINTARRAY*) s->pointarrays->item)->npoints)
207  return LW_TRUE;
208  return LW_FALSE;
209 }
210 
211 int
213 {
215  return LW_FAILURE;
216 
217  /* If p is NULL, just advance without reading */
218  if (p && !lwpointiterator_peek(s, p))
219  return LW_FAILURE;
220 
222  return LW_SUCCESS;
223 }
224 
225 int
227 {
229  return LW_FAILURE;
230 
231  if (!s->allow_modification)
232  {
233  lwerror("Cannot write to read-only iterator");
234  return LW_FAILURE;
235  }
236 
237  ptarray_set_point4d(s->pointarrays->item, s->i, p);
238 
240  return LW_SUCCESS;
241 }
242 
245 {
248 
249  return it;
250 }
251 
254 {
255  LWPOINTITERATOR* it = lwalloc(sizeof(LWPOINTITERATOR));
256 
257  it->geoms = NULL;
258  it->pointarrays = NULL;
259  it->i = 0;
261 
262  add_lwgeom_to_stack(it, g);
264 
265  return it;
266 }
267 
268 void
270 {
271  while (s->geoms != NULL)
272  {
273  s->geoms = pop_node(s->geoms);
274  }
275 
276  while (s->pointarrays != NULL)
277  {
278  s->pointarrays = pop_node(s->pointarrays);
279  }
280 
281  lwfree(s);
282 }
char * s
Definition: cu_in_wkt.c:23
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:170
#define LW_FALSE
Definition: liblwgeom.h:77
#define LW_FAILURE
Definition: liblwgeom.h:79
#define LINETYPE
Definition: liblwgeom.h:86
LWGEOM * lwcollection_getsubgeom(LWCOLLECTION *col, int gnum)
Definition: lwcollection.c:113
#define LW_SUCCESS
Definition: liblwgeom.h:80
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:923
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
LWTRIANGLE * lwgeom_as_lwtriangle(const LWGEOM *lwgeom)
Definition: lwgeom.c:215
void lwfree(void *mem)
Definition: lwutil.c:244
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1085
#define POLYGONTYPE
Definition: liblwgeom.h:87
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:92
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:1393
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:123
#define TRIANGLETYPE
Definition: liblwgeom.h:98
void * lwalloc(size_t size)
Definition: lwutil.c:229
LWCIRCSTRING * lwgeom_as_lwcircstring(const LWGEOM *lwgeom)
Definition: lwgeom.c:179
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:206
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:435
This library is the generic geometry handling section of PostGIS.
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static void unroll_collection(LWPOINTITERATOR *s)
Remove an LWCOLLECTION from the iterator stack, and add the components of the LWCOLLECTIONs to the st...
Definition: lwiterator.c:120
static int add_lwgeom_to_stack(LWPOINTITERATOR *s, LWGEOM *g)
Definition: lwiterator.c:71
LWPOINTITERATOR * lwpointiterator_create(const LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM*.
Definition: lwiterator.c:244
int lwpointiterator_next(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assign the next point in the iterator to p, and advances the iterator to the next point.
Definition: lwiterator.c:212
static int lwpointiterator_advance(LWPOINTITERATOR *s)
Definition: lwiterator.c:154
int lwpointiterator_peek(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assigns the next point in the iterator to p.
Definition: lwiterator.c:195
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
Definition: lwiterator.c:269
int lwpointiterator_modify_next(LWPOINTITERATOR *s, const POINT4D *p)
Attempts to replace the next point int the iterator with p, and advances the iterator to the next poi...
Definition: lwiterator.c:226
LWPOINTITERATOR * lwpointiterator_create_rw(LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM* Supports modification of coordinates during iterat...
Definition: lwiterator.c:253
static LISTNODE * extract_pointarrays_from_lwgeom(LWGEOM *g)
Return a pointer to the first of one or more LISTNODEs holding the POINTARRAYs of a geometry.
Definition: lwiterator.c:84
int lwpointiterator_has_next(LWPOINTITERATOR *s)
Returns LW_TRUE if there is another point available in the iterator.
Definition: lwiterator.c:204
static void unroll_collections(LWPOINTITERATOR *s)
Unroll LWCOLLECTIONs from the top of the stack, as necessary, until the element at the top of the sta...
Definition: lwiterator.c:145
static LISTNODE * pop_node(LISTNODE *i)
Definition: lwiterator.c:63
static LISTNODE * prepend_node(void *g, LISTNODE *front)
Definition: lwiterator.c:53
struct LISTNODE * next
Definition: lwiterator.c:31
void * item
Definition: lwiterator.c:32
uint32_t ngeoms
Definition: liblwgeom.h:510
LISTNODE * pointarrays
Definition: lwiterator.c:47
LISTNODE * geoms
Definition: lwiterator.c:46
uint32_t i
Definition: lwiterator.c:48
char allow_modification
Definition: lwiterator.c:49
POINTARRAY ** rings
Definition: liblwgeom.h:460
uint32_t nrings
Definition: liblwgeom.h:458
unsigned int uint32_t
Definition: uthash.h:78