File: cu_out_svg.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 (329 lines) | stat: -rw-r--r-- 7,975 bytes parent folder | download | duplicates (4)
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
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 * Copyright 2010 Olivier Courtin <olivier.courtin@oslandia.com>
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CUnit/Basic.h"

#include "liblwgeom_internal.h"
#include "cu_tester.h"

static void do_svg_test(char * in, char * out, int precision, int relative)
{
	LWGEOM *g;
	char * h;

	g = lwgeom_from_wkt(in, LW_PARSER_CHECK_NONE);
	h = lwgeom_to_svg(g, precision, relative);

	if (strcmp(h, out))
		fprintf(stderr, "\nIn:   %s\nOut:  %s\nTheo: %s\n", in, h, out);

	CU_ASSERT_STRING_EQUAL(h, out);

	lwgeom_free(g);
	lwfree(h);
}


static void do_svg_unsupported(char * in, char * out)
{
	LWGEOM *g;
	char *h;

	g = lwgeom_from_wkt(in, LW_PARSER_CHECK_NONE);
	h = lwgeom_to_svg(g, 0, 0);

	if (strcmp(cu_error_msg, out))
		fprintf(stderr, "\nIn:   %s\nOut:  %s\nTheo: %s\n",
		        in, cu_error_msg, out);

	CU_ASSERT_STRING_EQUAL(out, cu_error_msg);
	cu_error_msg_reset();

	lwfree(h);
	lwgeom_free(g);
}


static void out_svg_test_precision(void)
{
	/* 0 precision, i.e a round - with Circle point */
	do_svg_test(
	    "POINT(1.1111111111111 1.1111111111111)",
	    "cx=\"1\" cy=\"-1\"",
	    0, 0);

	/* 0 precision, i.e a round - with Point */
	do_svg_test(
	    "POINT(1.1111111111111 1.1111111111111)",
	    "x=\"1\" y=\"-1\"",
	    0, 1);

	/* 0 precision, i.e a round - with PointArray */
	do_svg_test(
	    "LINESTRING(1.1111111111111 1.1111111111111,1.1111111111111 1.1111111111111)",
	    "M 1 -1 L 1 -1",
	    0, 0);

	/* 0 precision, i.e a round - with relative PointArray */
	do_svg_test(
	    "LINESTRING(1.1111111111111 1.1111111111111,1.1111111111111 1.1111111111111)",
	    "M 1 -1 l 0 0",
	    0, 1);


	/* 9 digits precision - with Circle point */
	do_svg_test(
	    "POINT(1.2345678901234 1.2345678901234)",
	    "cx=\"1.23456789\" cy=\"-1.23456789\"",
	    9, 0);

	/* 9 digits precision - with Point */
	do_svg_test(
	    "POINT(1.2345678901234 1.2345678901234)",
	    "x=\"1.23456789\" y=\"-1.23456789\"",
	    9, 1);

	/* 9 digits precision - with PointArray */
	do_svg_test(
	    "LINESTRING(1.2345678901234 1.2345678901234,2.3456789012345 2.3456789012345)",
	    "M 1.23456789 -1.23456789 L 2.345678901 -2.345678901",
	    9, 0);

	/* 9 digits precision - with relative PointArray */
	do_svg_test(
	    "LINESTRING(1.2345678901234 1.2345678901234,2.3456789012345 2.3456789012345)",
	    "M 1.23456789 -1.23456789 l 1.111111011 -1.111111011",
	    9, 1);


	/* huge data - with Circle point */
	do_svg_test(
	    "POINT(1E300 -1E300)",
	    "cx=\"1e+300\" cy=\"1e+300\"",
	    0, 0);

	/* huge data - with Point */
	do_svg_test(
	    "POINT(1E300 -1E300)",
	    "x=\"1e+300\" y=\"1e+300\"",
	    0, 1);

	/* huge data - with PointArray */
	do_svg_test(
	    "LINESTRING(1E300 -1E300,1E301 -1E301)",
	    "M 1e+300 1e+300 L 1e+301 1e+301",
	    0, 0);

	/* huge data - with relative PointArray */
	do_svg_test(
	    "LINESTRING(1E300 -1E300,1E301 -1E301)",
	    "M 1e+300 1e+300 l 9e+300 9e+300",
	    0, 1);
}


static void out_svg_test_dims(void)
{
	/* 4D - with Circle point */
	do_svg_test(
	    "POINT(0 1 2 3)",
	    "cx=\"0\" cy=\"-1\"",
	    0, 0);

	/* 4D - with Point */
	do_svg_test(
	    "POINT(0 1 2 3)",
	    "x=\"0\" y=\"-1\"",
	    0, 1);

	/* 4D - with PointArray */
	do_svg_test(
	    "LINESTRING(0 1 2 3,4 5 6 7)",
	    "M 0 -1 L 4 -5",
	    0, 0);

	/* 4D - with relative PointArray */
	do_svg_test(
	    "LINESTRING(0 1 2 3,4 5 6 7)",
	    "M 0 -1 l 4 -4",
	    0, 1);
}


static void out_svg_test_geoms(void)
{
	/* Linestring */
	do_svg_test(
	    "LINESTRING(0 1,2 3,4 5)",
	    "M 0 -1 L 2 -3 4 -5",
	    0, 0);

	/* Polygon */
	do_svg_test(
	    "POLYGON((0 1,2 3,4 5,0 1))",
	    "M 0 -1 L 2 -3 4 -5 Z",
	    0, 0);

	/* Polygon - with internal ring */
	do_svg_test(
	    "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
	    "M 0 -1 L 2 -3 4 -5 Z M 6 -7 L 8 -9 10 -11 Z",
	    0, 0);

	/* MultiPoint */
	do_svg_test(
	    "MULTIPOINT(0 1,2 3)",
	    "cx=\"0\" cy=\"-1\",cx=\"2\" cy=\"-3\"",
	    0, 0);

	/* MultiLine */
	do_svg_test(
	    "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
	    "M 0 -1 L 2 -3 4 -5 M 6 -7 L 8 -9 10 -11",
	    0, 0);

	/* MultiPolygon */
	do_svg_test(
	    "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
	    "M 0 -1 L 2 -3 4 -5 Z M 6 -7 L 8 -9 10 -11 Z",
	    0, 0);

	/* GeometryCollection */
	do_svg_test(
	    "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
	    "cx=\"0\" cy=\"-1\";M 2 -3 L 4 -5",
	    0, 0);

	/* Empty GeometryCollection */
	do_svg_test(
	    "GEOMETRYCOLLECTION EMPTY",
	    "",
	    0, 0);

	/* Nested GeometryCollection */
	do_svg_unsupported(
	    "GEOMETRYCOLLECTION(POINT(0 1),GEOMETRYCOLLECTION(LINESTRING(2 3,4 5)))",
	    "assvg_geom_buf: 'GeometryCollection' geometry type not supported.");

	/* CircularString */
	do_svg_unsupported(
	    "CIRCULARSTRING(-2 0,0 2,2 0,0 2,2 4)",
	    "lwgeom_to_svg: 'CircularString' geometry type not supported");

	/* CompoundCurve */
	do_svg_unsupported(
	    "COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))",
	    "lwgeom_to_svg: 'CompoundCurve' geometry type not supported");

	/* CurvePolygon */
	do_svg_unsupported(
	    "CURVEPOLYGON(CIRCULARSTRING(-2 0,-1 -1,0 0,1 -1,2 0,0 2,-2 0),(-1 0,0 0.5,1 0,0 1,-1 0))",
	    "lwgeom_to_svg: 'CurvePolygon' geometry type not supported");

	/* MultiCurve */
	do_svg_unsupported(
	    "MULTICURVE((5 5,3 5,3 3,0 3),CIRCULARSTRING(0 0,2 1,2 2))",
	    "lwgeom_to_svg: 'MultiCurve' geometry type not supported");

	/* MultiSurface */
	do_svg_unsupported(
	    "MULTISURFACE(CURVEPOLYGON(CIRCULARSTRING(-2 0,-1 -1,0 0,1 -1,2 0,0 2,-2 0),(-1 0,0 0.5,1 0,0 1,-1 0)),((7 8,10 10,6 14,4 11,7 8)))",
	    "lwgeom_to_svg: 'MultiSurface' geometry type not supported");
}

static void out_svg_test_relative(void)
{
	/* Linestring */
	do_svg_test(
	    "LINESTRING(0 1,2 3,4 5)",
	    "M 0 -1 l 2 -2 2 -2",
	    0, 1);

	/* Polygon */
	do_svg_test(
	    "POLYGON((0 1,2 3,4 5,0 1))",
	    "M 0 -1 l 2 -2 2 -2 z",
	    0, 1);

	/* Polygon - with internal ring */
	do_svg_test(
	    "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
	    "M 0 -1 l 2 -2 2 -2 z M 6 -7 l 2 -2 2 -2 z",
	    0, 1);

	/* MultiPoint */
	do_svg_test(
	    "MULTIPOINT(0 1,2 3)",
	    "x=\"0\" y=\"-1\",x=\"2\" y=\"-3\"",
	    0, 1);

	/* MultiLine */
	do_svg_test(
	    "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
	    "M 0 -1 l 2 -2 2 -2 M 6 -7 l 2 -2 2 -2",
	    0, 1);

	/* MultiPolygon */
	do_svg_test(
	    "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
	    "M 0 -1 l 2 -2 2 -2 z M 6 -7 l 2 -2 2 -2 z",
	    0, 1);

	/* GeometryCollection */
	do_svg_test(
	    "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
	    "x=\"0\" y=\"-1\";M 2 -3 l 2 -2",
	    0, 1);
}

static void out_svg_test_srid(void)
{
	/* SRID - with Circle point */
	do_svg_test(
	    "SRID=4326;POINT(0 1)",
	    "cx=\"0\" cy=\"-1\"",
	    0, 0);

	/* SRID - with Point */
	do_svg_test(
	    "SRID=4326;POINT(0 1)",
	    "x=\"0\" y=\"-1\"",
	    0, 1);

	/* SRID - with PointArray */
	do_svg_test(
	    "SRID=4326;LINESTRING(0 1,2 3)",
	    "M 0 -1 L 2 -3",
	    0, 0);

	/* SRID - with relative PointArray */
	do_svg_test(
	    "SRID=4326;LINESTRING(0 1,2 3)",
	    "M 0 -1 l 2 -2",
	    0, 1);
}

/*
** Used by test harness to register the tests in this file.
*/
void out_svg_suite_setup(void);
void out_svg_suite_setup(void)
{
	CU_pSuite suite = CU_add_suite("svg_output", NULL, NULL);
	PG_ADD_TEST(suite, out_svg_test_precision);
	PG_ADD_TEST(suite, out_svg_test_dims);
	PG_ADD_TEST(suite, out_svg_test_relative);
	PG_ADD_TEST(suite, out_svg_test_geoms);
	PG_ADD_TEST(suite, out_svg_test_srid);
}