File: lwgeom_dump.c

package info (click to toggle)
postgis 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,296 kB
  • sloc: sql: 77,621; ansic: 59,025; xml: 18,553; sh: 11,043; java: 6,061; perl: 2,133; makefile: 981; yacc: 299; python: 192
file content (302 lines) | stat: -rw-r--r-- 7,003 bytes parent folder | download | duplicates (2)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**********************************************************************
 * $Id: lwgeom_dump.c 4635 2009-10-09 19:23:05Z pramsey $
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.refractions.net
 * Copyright 2001-2009 Refractions Research Inc.
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public Licence. See the COPYING file.
 *
 **********************************************************************/
#include "postgres.h"
#include "fmgr.h"
#include "utils/elog.h"
#include "utils/array.h"
#include "utils/geo_decls.h"
#include "funcapi.h"

#include "liblwgeom.h"
#include "lwgeom_pg.h"
#include "profile.h"

#include "../postgis_config.h"

#include <math.h>
#include <float.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>


Datum LWGEOM_dump(PG_FUNCTION_ARGS);
Datum LWGEOM_dump_rings(PG_FUNCTION_ARGS);

typedef struct GEOMDUMPNODE_T
{
	int idx;
	LWCOLLECTION *geom;
}
GEOMDUMPNODE;

#define MAXDEPTH 32
typedef struct GEOMDUMPSTATE
{
	int stacklen;
	GEOMDUMPNODE *stack[MAXDEPTH];
	LWGEOM *root;
	LWGEOM *geom;
}
GEOMDUMPSTATE;

#define PUSH(x,y) ((x)->stack[(x)->stacklen++]=(y))
#define LAST(x) ((x)->stack[(x)->stacklen-1])
#define POP(x) (--((x)->stacklen))


PG_FUNCTION_INFO_V1(LWGEOM_dump);
Datum LWGEOM_dump(PG_FUNCTION_ARGS)
{
	PG_LWGEOM *pglwgeom;
	LWCOLLECTION *lwcoll;
	LWGEOM *lwgeom;
	FuncCallContext *funcctx;
	GEOMDUMPSTATE *state;
	GEOMDUMPNODE *node;
	TupleDesc tupdesc;
	HeapTuple tuple;
	AttInMetadata *attinmeta;
	MemoryContext oldcontext, newcontext;
	Datum result;
	char address[256];
	char *ptr;
	unsigned int i;
	char *values[2];

	if (SRF_IS_FIRSTCALL())
	{
		funcctx = SRF_FIRSTCALL_INIT();
		newcontext = funcctx->multi_call_memory_ctx;

		oldcontext = MemoryContextSwitchTo(newcontext);

		pglwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
		lwgeom = lwgeom_deserialize(SERIALIZED_FORM(pglwgeom));

		/* Create function state */
		state = lwalloc(sizeof(GEOMDUMPSTATE));
		state->root = lwgeom;
		state->stacklen=0;

		if ( lwgeom_is_collection(TYPE_GETTYPE(lwgeom->type)) )
		{
			/*
			 * Push a GEOMDUMPNODE on the state stack
			 */
			node = lwalloc(sizeof(GEOMDUMPNODE));
			node->idx=0;
			node->geom = (LWCOLLECTION *)lwgeom;
			PUSH(state, node);
		}

		funcctx->user_fctx = state;

		/*
		 * Build a tuple description for an
		 * geometry_dump tuple
		 */
		tupdesc = RelationNameGetTupleDesc("geometry_dump");

		/*
		 * generate attribute metadata needed later to produce
		 * tuples from raw C strings
		 */
		attinmeta = TupleDescGetAttInMetadata(tupdesc);
		funcctx->attinmeta = attinmeta;

		MemoryContextSwitchTo(oldcontext);
	}

	/* stuff done on every call of the function */
	funcctx = SRF_PERCALL_SETUP();
	newcontext = funcctx->multi_call_memory_ctx;

	/* get state */
	state = funcctx->user_fctx;

	/* Handled simple geometries */
	if ( ! state->root ) SRF_RETURN_DONE(funcctx);
	if ( ! lwgeom_is_collection(TYPE_GETTYPE(state->root->type)) )
	{
		values[0] = "{}";
		values[1] = lwgeom_to_hexwkb(state->root, PARSER_CHECK_NONE, -1);
		tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
		result = HeapTupleGetDatum(tuple);

		state->root = NULL;
		SRF_RETURN_NEXT(funcctx, result);
	}

	while (1)
	{
		node = LAST(state);
		lwcoll = node->geom;

		if ( node->idx < lwcoll->ngeoms )
		{
			lwgeom = lwcoll->geoms[node->idx];
			if ( ! lwgeom_is_collection(TYPE_GETTYPE(lwgeom->type)) )
			{
				/* write address of current geom */
				ptr=address;
				*ptr++='{';
				for (i=0; i<state->stacklen; i++)
				{
					if ( i ) ptr += sprintf(ptr, ",");
					ptr += sprintf(ptr, "%d", state->stack[i]->idx+1);
				}
				*ptr++='}';
				*ptr='\0';

				break;
			}

			/*
			 * It's a collection, increment index
			 * of current node, push a new one on the
			 * stack
			 */

			oldcontext = MemoryContextSwitchTo(newcontext);

			node = lwalloc(sizeof(GEOMDUMPNODE));
			node->idx=0;
			node->geom = (LWCOLLECTION *)lwgeom;
			PUSH(state, node);

			MemoryContextSwitchTo(oldcontext);

			continue;
		}

		if ( ! POP(state) ) SRF_RETURN_DONE(funcctx);
		LAST(state)->idx++;
	}

	lwgeom->SRID = state->root->SRID;

	values[0] = address;
	values[1] = lwgeom_to_hexwkb(lwgeom, PARSER_CHECK_NONE, -1);
	tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
	result = TupleGetDatum(funcctx->slot, tuple);
	node->idx++;
	SRF_RETURN_NEXT(funcctx, result);
}

struct POLYDUMPSTATE
{
	int ringnum;
	LWPOLY *poly;
};

PG_FUNCTION_INFO_V1(LWGEOM_dump_rings);
Datum LWGEOM_dump_rings(PG_FUNCTION_ARGS)
{
	PG_LWGEOM *pglwgeom;
	LWGEOM *lwgeom;
	FuncCallContext *funcctx;
	struct POLYDUMPSTATE *state;
	TupleDesc tupdesc;
	HeapTuple tuple;
	AttInMetadata *attinmeta;
	MemoryContext oldcontext, newcontext;
	Datum result;
	char address[256];
	char *values[2];

	if (SRF_IS_FIRSTCALL())
	{
		funcctx = SRF_FIRSTCALL_INIT();
		newcontext = funcctx->multi_call_memory_ctx;

		oldcontext = MemoryContextSwitchTo(newcontext);

		pglwgeom = (PG_LWGEOM *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
		if ( TYPE_GETTYPE(pglwgeom->type) != POLYGONTYPE )
		{
			lwerror("Input is not a polygon");
		}

		lwgeom = lwgeom_deserialize(SERIALIZED_FORM(pglwgeom));

		/* Create function state */
		state = lwalloc(sizeof(struct POLYDUMPSTATE));
		state->poly = lwgeom_as_lwpoly(lwgeom);
		assert (state->poly);
		state->ringnum=0;

		funcctx->user_fctx = state;

		/*
		 * Build a tuple description for an
		 * geometry_dump tuple
		 */
		tupdesc = RelationNameGetTupleDesc("geometry_dump");

		/*
		 * generate attribute metadata needed later to produce
		 * tuples from raw C strings
		 */
		attinmeta = TupleDescGetAttInMetadata(tupdesc);
		funcctx->attinmeta = attinmeta;

		MemoryContextSwitchTo(oldcontext);
	}

	/* stuff done on every call of the function */
	funcctx = SRF_PERCALL_SETUP();
	newcontext = funcctx->multi_call_memory_ctx;

	/* get state */
	state = funcctx->user_fctx;

	/* Loop trough polygon rings */
	while (state->ringnum < state->poly->nrings )
	{
		LWPOLY* poly = state->poly;
		POINTARRAY *ring;
		LWGEOM* ringgeom;

		/* Switch to an appropriate memory context for POINTARRAY
		 * cloning and hexwkb allocation */
		oldcontext = MemoryContextSwitchTo(newcontext);

		/* We need a copy of input ring here */
		ring = ptarray_clone(poly->rings[state->ringnum]);

		/* Construct another polygon with shell only */
		ringgeom = (LWGEOM*)lwpoly_construct(
		               poly->SRID,
		               NULL, /* TODO: could use input bounding box here */
		               1, /* one ring */
		               &ring);

		/* Write path as ``{ <ringnum> }'' */
		sprintf(address, "{%d}", state->ringnum);

		values[0] = address;
		values[1] = lwgeom_to_hexwkb(ringgeom, PARSER_CHECK_NONE, -1);

		MemoryContextSwitchTo(oldcontext);

		tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
		result = HeapTupleGetDatum(tuple);
		++state->ringnum;
		SRF_RETURN_NEXT(funcctx, result);
	}

	SRF_RETURN_DONE(funcctx);

}