File: lwout_geojson.c

package info (click to toggle)
postgis 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,052 kB
  • sloc: ansic: 162,204; sql: 93,950; xml: 53,121; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,434; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (407 lines) | stat: -rw-r--r-- 10,824 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**********************************************************************
 *
 * 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 2001-2003 Refractions Research Inc.
 * Copyright 2009-2010 Olivier Courtin <olivier.courtin@oslandia.com>
 *
 **********************************************************************/


#include "liblwgeom_internal.h"
#include "stringbuffer.h"
#include <string.h>	/* strlen */
#include <assert.h>


typedef struct geojson_opts {
	const char *srs;
	GBOX       *bbox;
	int         precision;
	int         hasz;
	int         isCollectionElement;
} geojson_opts;

enum {
	geojson_tagged,
	geojson_untagged
};

static void asgeojson_geometry(stringbuffer_t *sb, const LWGEOM *geom, const geojson_opts *opts);



static void
coordinate_to_geojson(stringbuffer_t *sb, const POINTARRAY *pa, uint32_t i, const geojson_opts *opts)
{
	if (!FLAGS_GET_Z(pa->flags))
	{
		const POINT2D *pt = getPoint2d_cp(pa, i);
		stringbuffer_append_char(sb, '[');
		stringbuffer_append_double(sb, pt->x, opts->precision);
		stringbuffer_append_char(sb, ',');
		stringbuffer_append_double(sb, pt->y, opts->precision);
		stringbuffer_append_char(sb, ']');
	}
	else
	{
		const POINT3D *pt = getPoint3d_cp(pa, i);
		stringbuffer_append_char(sb, '[');
		stringbuffer_append_double(sb, pt->x, opts->precision);
		stringbuffer_append_char(sb, ',');
		stringbuffer_append_double(sb, pt->y, opts->precision);
		stringbuffer_append_char(sb, ',');
		stringbuffer_append_double(sb, pt->z, opts->precision);
		stringbuffer_append_char(sb, ']');
	}
}

static void
pointArray_to_geojson(stringbuffer_t *sb, const POINTARRAY *pa, const geojson_opts *opts)
{
	if (!pa || pa->npoints == 0)
	{
		stringbuffer_append_len(sb, "[]", 2);
		return;
	}

	stringbuffer_append_char(sb, '[');
	for (uint32_t i = 0; i < pa->npoints; i++)
	{
		if (i) stringbuffer_append_char(sb, ',');
		coordinate_to_geojson(sb, pa, i, opts);
	}
	stringbuffer_append_char(sb, ']');
	return;
}

static void
asgeojson_srs(stringbuffer_t *sb, const geojson_opts *opts)
{
	if (!opts->srs) return;
	stringbuffer_append_len(sb, "\"crs\":{\"type\":\"name\",", 21);
	stringbuffer_aprintf(sb, "\"properties\":{\"name\":\"%s\"}},", opts->srs);
	return;
}


static void
asgeojson_bbox(stringbuffer_t *sb, const geojson_opts *opts)
{
	if (!opts->bbox) return;
	if (!opts->hasz)
		stringbuffer_aprintf(sb, "\"bbox\":[%.*f,%.*f,%.*f,%.*f],",
			opts->precision, opts->bbox->xmin,
			opts->precision, opts->bbox->ymin,
			opts->precision, opts->bbox->xmax,
			opts->precision, opts->bbox->ymax);
	else
		stringbuffer_aprintf(sb, "\"bbox\":[%.*f,%.*f,%.*f,%.*f,%.*f,%.*f],",
			opts->precision, opts->bbox->xmin,
			opts->precision, opts->bbox->ymin,
			opts->precision, opts->bbox->zmin,
			opts->precision, opts->bbox->xmax,
			opts->precision, opts->bbox->ymax,
			opts->precision, opts->bbox->zmax);
	return;
}


/**
 * Point Geometry
 */
static void
asgeojson_point_coords(stringbuffer_t *sb, const LWPOINT *point, const geojson_opts *opts, int tagged)
{
	if (tagged == geojson_tagged) stringbuffer_append_len(sb, "\"coordinates\":", 14);
	if (lwgeom_is_empty((LWGEOM*)point))
		stringbuffer_append_len(sb, "[]", 2);
	else
		coordinate_to_geojson(sb, point->point, 0, opts);
	return;
}

static void
asgeojson_line_coords(stringbuffer_t *sb, const LWLINE *line, const geojson_opts *opts, int tagged)
{
	if (tagged == geojson_tagged) stringbuffer_append_len(sb, "\"coordinates\":", 14);
	if (lwgeom_is_empty((LWGEOM*)line))
		stringbuffer_append_len(sb, "[]", 2);
	else
		pointArray_to_geojson(sb, line->points, opts);
	return;
}

static void
asgeojson_poly_coords(stringbuffer_t *sb, const LWPOLY *poly, const geojson_opts *opts, int tagged)
{
	uint32_t i;
	if (tagged == geojson_tagged) stringbuffer_append_len(sb, "\"coordinates\":", 14);
	if (lwgeom_is_empty((LWGEOM*)poly))
		stringbuffer_append_len(sb, "[]", 2);
	else
	{
		stringbuffer_append_char(sb, '[');
		for (i = 0; i < poly->nrings; i++)
		{
			if (i) stringbuffer_append_char(sb, ',');
			pointArray_to_geojson(sb, poly->rings[i], opts);
		}
		stringbuffer_append_char(sb, ']');
	}
	return;
}

/**
 * Point Geometry
 */
static void
asgeojson_point(stringbuffer_t *sb, const LWPOINT *point, const geojson_opts *opts)
{
	stringbuffer_append_len(sb, "{\"type\":\"Point\",", 16);
	asgeojson_srs(sb, opts);
	asgeojson_bbox(sb, opts);
	asgeojson_point_coords(sb, point, opts, geojson_tagged);
	stringbuffer_append_char(sb, '}');
	return;
}

/**
 * Triangle Geometry
 */
static void
asgeojson_triangle(stringbuffer_t *sb, const LWTRIANGLE *tri, const geojson_opts *opts)
{
	stringbuffer_append_len(sb, "{\"type\":\"Polygon\",", 18);
	asgeojson_srs(sb, opts);
	asgeojson_bbox(sb, opts);
	stringbuffer_append_len(sb, "\"coordinates\":[", 15);
	if (lwtriangle_is_empty(tri))
		stringbuffer_append_len(sb, "[]", 2);
	else
		pointArray_to_geojson(sb, tri->points, opts);
	stringbuffer_append_len(sb, "]}", 2);
	return;
}

/**
 * Line Geometry
 */
static void
asgeojson_line(stringbuffer_t *sb, const LWLINE *line, const geojson_opts *opts)
{
	const char tmpl[] = "{\"type\":\"LineString\",";
	stringbuffer_append_len(sb, tmpl, sizeof(tmpl)-1);
	asgeojson_srs(sb, opts);
	asgeojson_bbox(sb, opts);
	asgeojson_line_coords(sb, line, opts, geojson_tagged);
	stringbuffer_append_char(sb, '}');
	return;
}

/**
 * Polygon Geometry
 */
static void
asgeojson_poly(stringbuffer_t *sb, const LWPOLY *poly, const geojson_opts *opts)
{
	stringbuffer_append_len(sb, "{\"type\":\"Polygon\",", 18);
	asgeojson_srs(sb, opts);
	asgeojson_bbox(sb, opts);
	asgeojson_poly_coords(sb, poly, opts, geojson_tagged);
	stringbuffer_append_char(sb, '}');
	return;
}

/**
 * Multipoint Geometry
 */
static void
asgeojson_multipoint(stringbuffer_t *sb, const LWMPOINT *mpoint, const geojson_opts *opts)
{
	uint32_t i, ngeoms = mpoint->ngeoms;
	stringbuffer_append_len(sb, "{\"type\":\"MultiPoint\",", 21);
	asgeojson_srs(sb, opts);
	asgeojson_bbox(sb, opts);
	stringbuffer_append_len(sb, "\"coordinates\":[", 15);

	if (lwgeom_is_empty((LWGEOM*)mpoint))
		ngeoms = 0;

	for (i=0; i < ngeoms; i++)
	{
		if (i) stringbuffer_append_char(sb, ',');
		asgeojson_point_coords(sb, mpoint->geoms[i], opts, geojson_untagged);
	}
	stringbuffer_append_len(sb, "]}", 2);
	return;
}


/**
 * Multipoint Geometry
 */
static void
asgeojson_multiline(stringbuffer_t *sb, const LWMLINE *mline, const geojson_opts *opts)
{
	uint32_t i, ngeoms = mline->ngeoms;
	stringbuffer_append_len(sb, "{\"type\":\"MultiLineString\",", 26);
	asgeojson_srs(sb, opts);
	asgeojson_bbox(sb, opts);
	stringbuffer_append_len(sb, "\"coordinates\":[", 15);

	if (lwgeom_is_empty((LWGEOM*)mline))
		ngeoms = 0;

	for (i=0; i < ngeoms; i++)
	{
		if (i) stringbuffer_append_char(sb, ',');
		asgeojson_line_coords(sb, mline->geoms[i], opts, geojson_untagged);
	}
	stringbuffer_append_len(sb, "]}", 2);
	return;
}


static void
asgeojson_multipolygon(stringbuffer_t *sb, const LWMPOLY *mpoly, const geojson_opts *opts)
{
	uint32_t i, ngeoms = mpoly->ngeoms;

	stringbuffer_append_len(sb, "{\"type\":\"MultiPolygon\",", 23);
	asgeojson_srs(sb, opts);
	asgeojson_bbox(sb, opts);
	stringbuffer_append_len(sb, "\"coordinates\":[", 15);

	if (lwgeom_is_empty((LWGEOM*)mpoly))
		ngeoms = 0;

	for (i=0; i < ngeoms; i++)
	{
		if (i) stringbuffer_append_char(sb, ',');
		asgeojson_poly_coords(sb, mpoly->geoms[i], opts, geojson_untagged);
	}
	stringbuffer_append_len(sb, "]}", 2);
	return;
}

/**
 * Collection Geometry
 */
static void
asgeojson_collection(stringbuffer_t *sb, const LWCOLLECTION *col, const geojson_opts *opts)
{
	uint32_t i, ngeoms = col->ngeoms;

	/* subgeometries don't get boxes or srs */
	geojson_opts subopts = *opts;
	subopts.bbox = NULL;
	subopts.srs = NULL;
	subopts.isCollectionElement = LW_TRUE;

	stringbuffer_append_len(sb, "{\"type\":\"GeometryCollection\",", 29);
	asgeojson_srs(sb, opts);
	if (col->ngeoms) asgeojson_bbox(sb, opts);
	stringbuffer_append_len(sb, "\"geometries\":[", 14);

	if (lwgeom_is_empty((LWGEOM*)col))
		ngeoms = 0;

	for (i=0; i<ngeoms; i++)
	{
		if (i) stringbuffer_append_char(sb, ',');
		asgeojson_geometry(sb, col->geoms[i], &subopts);
	}

	stringbuffer_append_len(sb, "]}", 2);
	return;
}

static void
asgeojson_geometry(stringbuffer_t *sb, const LWGEOM *geom, const geojson_opts *opts)
{
	switch (geom->type)
	{
	case POINTTYPE:
		asgeojson_point(sb, (LWPOINT*)geom, opts);
		break;
	case LINETYPE:
		asgeojson_line(sb, (LWLINE*)geom, opts);
		break;
	case POLYGONTYPE:
		asgeojson_poly(sb, (LWPOLY*)geom, opts);
		break;
	case MULTIPOINTTYPE:
		asgeojson_multipoint(sb, (LWMPOINT*)geom, opts);
		break;
	case MULTILINETYPE:
		asgeojson_multiline(sb, (LWMLINE*)geom, opts);
		break;
	case MULTIPOLYGONTYPE:
		asgeojson_multipolygon(sb, (LWMPOLY*)geom, opts);
		break;
	case TRIANGLETYPE:
		asgeojson_triangle(sb, (LWTRIANGLE *)geom, opts);
		break;
	case TINTYPE:
	case COLLECTIONTYPE:
		if (opts->isCollectionElement) {
			lwerror("GeoJson: geometry not supported.");
		}
		asgeojson_collection(sb, (LWCOLLECTION*)geom, opts);
		break;
	default:
		lwerror("lwgeom_to_geojson: '%s' geometry type not supported", lwtype_name(geom->type));
	}
}

/**
 * Takes a GEOMETRY and returns a GeoJson representation
 */
lwvarlena_t *
lwgeom_to_geojson(const LWGEOM *geom, const char *srs, int precision, int has_bbox)
{
	GBOX static_bbox = {0};
	geojson_opts opts;
	stringbuffer_t sb;

	memset(&opts, 0, sizeof(opts));
	opts.precision = precision;
	opts.hasz = FLAGS_GET_Z(geom->flags);
	opts.srs = srs;

	if (has_bbox)
	{
		/* Whether these are geography or geometry,
		   the GeoJSON expects a cartesian bounding box */
		lwgeom_calculate_gbox_cartesian(geom, &static_bbox);
		opts.bbox = &static_bbox;
	}

	/* To avoid taking a copy of the output, we make */
	/* space for the VARLENA header before starting to */
	/* serialize the geom */
	stringbuffer_init_varlena(&sb);
	/* Now serialize the geometry */
	asgeojson_geometry(&sb, geom, &opts);
	/* Leave the initially allocated buffer in place */
	/* and write the varlena_t metadata into the slot we */
	/* left at the start */
	return stringbuffer_getvarlena(&sb);
}