File: lwiterator.c

package info (click to toggle)
postgis 2.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,660 kB
  • ctags: 10,181
  • sloc: ansic: 132,858; sql: 131,148; xml: 46,460; sh: 4,832; perl: 4,476; makefile: 2,749; python: 1,198; yacc: 442; lex: 131
file content (282 lines) | stat: -rw-r--r-- 5,978 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 *
 * PostGIS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * PostGIS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PostGIS.  If not, see <http://www.gnu.org/licenses/>.
 *
 **********************************************************************
 *
 * Copyright 2015 Daniel Baston <dbaston@gmail.com>
 *
 **********************************************************************/


#include "liblwgeom.h"
#include "lwgeom_log.h"

struct LISTNODE
{
	struct LISTNODE* next;
	void* item;
};
typedef struct LISTNODE LISTNODE;

/* The LWPOINTITERATOR consists of two stacks of items to process: a stack
 * of geometries, and a stack of POINTARRAYs extracted from those geometries.
 * The index "i" refers to the "next" point, which is found at the top of the
 * pointarrays stack.
 *
 * When the pointarrays stack is depleted, we pull a geometry from the geometry
 * stack to replenish it.
 */
struct LWPOINTITERATOR
{
	LISTNODE* geoms;
	LISTNODE* pointarrays;
	uint32_t i;
	char allow_modification;
};

static LISTNODE*
prepend_node(void* g, LISTNODE* front)
{
	LISTNODE* n = lwalloc(sizeof(LISTNODE));
	n->item = g;
	n->next = front;

	return n;
}

static LISTNODE*
pop_node(LISTNODE* i)
{
	LISTNODE* next = i->next;
	lwfree(i);
	return next;
}

static int
add_lwgeom_to_stack(LWPOINTITERATOR* s, LWGEOM* g)
{
	if (lwgeom_is_empty(g))
		return LW_FAILURE;

	s->geoms = prepend_node(g, s->geoms);
	return LW_SUCCESS;
}

/** Return a pointer to the first of one or more LISTNODEs holding the POINTARRAYs
 *  of a geometry.  Will not handle GeometryCollections.
 */
static LISTNODE*
extract_pointarrays_from_lwgeom(LWGEOM* g)
{
	switch(lwgeom_get_type(g))
	{
	case POINTTYPE:
		return prepend_node(lwgeom_as_lwpoint(g)->point, NULL);
	case LINETYPE:
		return prepend_node(lwgeom_as_lwline(g)->points, NULL);
	case TRIANGLETYPE:
		return prepend_node(lwgeom_as_lwtriangle(g)->points, NULL);
	case CIRCSTRINGTYPE:
		return prepend_node(lwgeom_as_lwcircstring(g)->points, NULL);
	case POLYGONTYPE:
	{
		LISTNODE* n = NULL;

		LWPOLY* p = lwgeom_as_lwpoly(g);
		int i;
		for (i = p->nrings - 1; i >= 0; i--)
		{
			n = prepend_node(p->rings[i], n);
		}

		return n;
	}
	default:
		lwerror("Unsupported geometry type for lwpointiterator");
	}

	return NULL;
}

/** Remove an LWCOLLECTION from the iterator stack, and add the components of the
 *  LWCOLLECTIONs to the stack.
 */
static void
unroll_collection(LWPOINTITERATOR* s)
{
	int i;
	LWCOLLECTION* c;

	if (!s->geoms)
	{
		return;
	}

	c = (LWCOLLECTION*) s->geoms->item;
	s->geoms = pop_node(s->geoms);

	for (i = c->ngeoms - 1; i >= 0; i--)
	{
		LWGEOM* g = lwcollection_getsubgeom(c, i);

		add_lwgeom_to_stack(s, g);
	}
}

/** Unroll LWCOLLECTIONs from the top of the stack, as necessary, until the element at the
 *  top of the stack is not a LWCOLLECTION.
 */
static void
unroll_collections(LWPOINTITERATOR* s)
{
	while(s->geoms && lwgeom_is_collection(s->geoms->item))
	{
		unroll_collection(s);
	}
}

static int
lwpointiterator_advance(LWPOINTITERATOR* s)
{
	s->i += 1;

	/* We've reached the end of our current POINTARRAY.  Try to see if there
	 * are any more POINTARRAYS on the stack. */
	if (s->pointarrays && s->i >= ((POINTARRAY*) s->pointarrays->item)->npoints)
	{
		s->pointarrays = pop_node(s->pointarrays);
		s->i = 0;
	}

	/* We don't have a current POINTARRAY.  Pull a geometry from the stack, and
	 * decompose it into its POINTARRARYs. */
	if (!s->pointarrays)
	{
		LWGEOM* g;
		unroll_collections(s);

		if (!s->geoms)
		{
			return LW_FAILURE;
		}

		s->i = 0;
		g = s->geoms->item;
		s->pointarrays = extract_pointarrays_from_lwgeom(g);

		s->geoms = pop_node(s->geoms);
	}

	if (!s->pointarrays)
	{
		return LW_FAILURE;
	}
	return LW_SUCCESS;
}

/* Public API implementation */

int
lwpointiterator_peek(LWPOINTITERATOR* s, POINT4D* p)
{
	if (!lwpointiterator_has_next(s))
		return LW_FAILURE;

	return getPoint4d_p(s->pointarrays->item, s->i, p);
}

int
lwpointiterator_has_next(LWPOINTITERATOR* s)
{
	if (s->pointarrays && s->i < ((POINTARRAY*) s->pointarrays->item)->npoints)
		return LW_TRUE;
	return LW_FALSE;
}

int
lwpointiterator_next(LWPOINTITERATOR* s, POINT4D* p)
{
	if (!lwpointiterator_has_next(s))
		return LW_FAILURE;

	/* If p is NULL, just advance without reading */
	if (p && !lwpointiterator_peek(s, p))
		return LW_FAILURE;

	lwpointiterator_advance(s);
	return LW_SUCCESS;
}

int
lwpointiterator_modify_next(LWPOINTITERATOR* s, const POINT4D* p)
{
	if (!lwpointiterator_has_next(s))
		return LW_FAILURE;

	if (!s->allow_modification)
	{
		lwerror("Cannot write to read-only iterator");
		return LW_FAILURE;
	}

	ptarray_set_point4d(s->pointarrays->item, s->i, p);

	lwpointiterator_advance(s);
	return LW_SUCCESS;
}

LWPOINTITERATOR*
lwpointiterator_create(const LWGEOM* g)
{
	LWPOINTITERATOR* it = lwpointiterator_create_rw((LWGEOM*) g);
	it->allow_modification = LW_FALSE;

	return it;
}

LWPOINTITERATOR*
lwpointiterator_create_rw(LWGEOM* g)
{
	LWPOINTITERATOR* it = lwalloc(sizeof(LWPOINTITERATOR));

	it->geoms = NULL;
	it->pointarrays = NULL;
	it->i = 0;
	it->allow_modification = LW_TRUE;

	add_lwgeom_to_stack(it, g);
	lwpointiterator_advance(it);

	return it;
}

void
lwpointiterator_destroy(LWPOINTITERATOR* s)
{
	while (s->geoms != NULL)
	{
		s->geoms = pop_node(s->geoms);
	}

	while (s->pointarrays != NULL)
	{
		s->pointarrays = pop_node(s->pointarrays);
	}

	lwfree(s);
}