PostGIS  2.5.7dev-r@@SVN_REVISION@@
cu_out_svg.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2010 Olivier Courtin <olivier.courtin@oslandia.com>
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "CUnit/Basic.h"
16 
17 #include "liblwgeom_internal.h"
18 #include "cu_tester.h"
19 
20 static void do_svg_test(char * in, char * out, int precision, int relative)
21 {
22  LWGEOM *g;
23  char * h;
24 
26  h = lwgeom_to_svg(g, precision, relative);
27 
28  if (strcmp(h, out))
29  fprintf(stderr, "\nIn: %s\nOut: %s\nTheo: %s\n", in, h, out);
30 
31  CU_ASSERT_STRING_EQUAL(h, out);
32 
33  lwgeom_free(g);
34  lwfree(h);
35 }
36 
37 
38 static void do_svg_unsupported(char * in, char * out)
39 {
40  LWGEOM *g;
41  char *h;
42 
44  h = lwgeom_to_svg(g, 0, 0);
45 
46  if (strcmp(cu_error_msg, out))
47  fprintf(stderr, "\nIn: %s\nOut: %s\nTheo: %s\n",
48  in, cu_error_msg, out);
49 
50  CU_ASSERT_STRING_EQUAL(out, cu_error_msg);
52 
53  lwfree(h);
54  lwgeom_free(g);
55 }
56 
57 
58 static void out_svg_test_precision(void)
59 {
60  /* 0 precision, i.e a round - with Circle point */
62  "POINT(1.1111111111111 1.1111111111111)",
63  "cx=\"1\" cy=\"-1\"",
64  0, 0);
65 
66  /* 0 precision, i.e a round - with Point */
68  "POINT(1.1111111111111 1.1111111111111)",
69  "x=\"1\" y=\"-1\"",
70  0, 1);
71 
72  /* 0 precision, i.e a round - with PointArray */
74  "LINESTRING(1.1111111111111 1.1111111111111,1.1111111111111 1.1111111111111)",
75  "M 1 -1 L 1 -1",
76  0, 0);
77 
78  /* 0 precision, i.e a round - with relative PointArray */
80  "LINESTRING(1.1111111111111 1.1111111111111,1.1111111111111 1.1111111111111)",
81  "M 1 -1 l 0 0",
82  0, 1);
83 
84 
85  /* 9 digits precision - with Circle point */
87  "POINT(1.2345678901234 1.2345678901234)",
88  "cx=\"1.23456789\" cy=\"-1.23456789\"",
89  9, 0);
90 
91  /* 9 digits precision - with Point */
93  "POINT(1.2345678901234 1.2345678901234)",
94  "x=\"1.23456789\" y=\"-1.23456789\"",
95  9, 1);
96 
97  /* 9 digits precision - with PointArray */
99  "LINESTRING(1.2345678901234 1.2345678901234,2.3456789012345 2.3456789012345)",
100  "M 1.23456789 -1.23456789 L 2.345678901 -2.345678901",
101  9, 0);
102 
103  /* 9 digits precision - with relative PointArray */
104  do_svg_test(
105  "LINESTRING(1.2345678901234 1.2345678901234,2.3456789012345 2.3456789012345)",
106  "M 1.23456789 -1.23456789 l 1.111111011 -1.111111011",
107  9, 1);
108 
109 
110  /* huge data - with Circle point */
111  do_svg_test(
112  "POINT(1E300 -1E300)",
113  "cx=\"1e+300\" cy=\"1e+300\"",
114  0, 0);
115 
116  /* huge data - with Point */
117  do_svg_test(
118  "POINT(1E300 -1E300)",
119  "x=\"1e+300\" y=\"1e+300\"",
120  0, 1);
121 
122  /* huge data - with PointArray */
123  do_svg_test(
124  "LINESTRING(1E300 -1E300,1E301 -1E301)",
125  "M 1e+300 1e+300 L 1e+301 1e+301",
126  0, 0);
127 
128  /* huge data - with relative PointArray */
129  do_svg_test(
130  "LINESTRING(1E300 -1E300,1E301 -1E301)",
131  "M 1e+300 1e+300 l 9e+300 9e+300",
132  0, 1);
133 }
134 
135 
136 static void out_svg_test_dims(void)
137 {
138  /* 4D - with Circle point */
139  do_svg_test(
140  "POINT(0 1 2 3)",
141  "cx=\"0\" cy=\"-1\"",
142  0, 0);
143 
144  /* 4D - with Point */
145  do_svg_test(
146  "POINT(0 1 2 3)",
147  "x=\"0\" y=\"-1\"",
148  0, 1);
149 
150  /* 4D - with PointArray */
151  do_svg_test(
152  "LINESTRING(0 1 2 3,4 5 6 7)",
153  "M 0 -1 L 4 -5",
154  0, 0);
155 
156  /* 4D - with relative PointArray */
157  do_svg_test(
158  "LINESTRING(0 1 2 3,4 5 6 7)",
159  "M 0 -1 l 4 -4",
160  0, 1);
161 }
162 
163 
164 static void out_svg_test_geoms(void)
165 {
166  /* Linestring */
167  do_svg_test(
168  "LINESTRING(0 1,2 3,4 5)",
169  "M 0 -1 L 2 -3 4 -5",
170  0, 0);
171 
172  /* Polygon */
173  do_svg_test(
174  "POLYGON((0 1,2 3,4 5,0 1))",
175  "M 0 -1 L 2 -3 4 -5 Z",
176  0, 0);
177 
178  /* Polygon - with internal ring */
179  do_svg_test(
180  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
181  "M 0 -1 L 2 -3 4 -5 Z M 6 -7 L 8 -9 10 -11 Z",
182  0, 0);
183 
184  /* MultiPoint */
185  do_svg_test(
186  "MULTIPOINT(0 1,2 3)",
187  "cx=\"0\" cy=\"-1\",cx=\"2\" cy=\"-3\"",
188  0, 0);
189 
190  /* MultiLine */
191  do_svg_test(
192  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
193  "M 0 -1 L 2 -3 4 -5 M 6 -7 L 8 -9 10 -11",
194  0, 0);
195 
196  /* MultiPolygon */
197  do_svg_test(
198  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
199  "M 0 -1 L 2 -3 4 -5 Z M 6 -7 L 8 -9 10 -11 Z",
200  0, 0);
201 
202  /* GeometryCollection */
203  do_svg_test(
204  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
205  "cx=\"0\" cy=\"-1\";M 2 -3 L 4 -5",
206  0, 0);
207 
208  /* Empty GeometryCollection */
209  do_svg_test(
210  "GEOMETRYCOLLECTION EMPTY",
211  "",
212  0, 0);
213 
214  /* Nested GeometryCollection */
216  "GEOMETRYCOLLECTION(POINT(0 1),GEOMETRYCOLLECTION(LINESTRING(2 3,4 5)))",
217  "assvg_geom_buf: 'GeometryCollection' geometry type not supported.");
218 
219  /* CircularString */
221  "CIRCULARSTRING(-2 0,0 2,2 0,0 2,2 4)",
222  "lwgeom_to_svg: 'CircularString' geometry type not supported");
223 
224  /* CompoundCurve */
226  "COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))",
227  "lwgeom_to_svg: 'CompoundCurve' geometry type not supported");
228 
229  /* CurvePolygon */
231  "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))",
232  "lwgeom_to_svg: 'CurvePolygon' geometry type not supported");
233 
234  /* MultiCurve */
236  "MULTICURVE((5 5,3 5,3 3,0 3),CIRCULARSTRING(0 0,2 1,2 2))",
237  "lwgeom_to_svg: 'MultiCurve' geometry type not supported");
238 
239  /* MultiSurface */
241  "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)))",
242  "lwgeom_to_svg: 'MultiSurface' geometry type not supported");
243 }
244 
245 static void out_svg_test_relative(void)
246 {
247  /* Linestring */
248  do_svg_test(
249  "LINESTRING(0 1,2 3,4 5)",
250  "M 0 -1 l 2 -2 2 -2",
251  0, 1);
252 
253  /* Polygon */
254  do_svg_test(
255  "POLYGON((0 1,2 3,4 5,0 1))",
256  "M 0 -1 l 2 -2 2 -2 z",
257  0, 1);
258 
259  /* Polygon - with internal ring */
260  do_svg_test(
261  "POLYGON((0 1,2 3,4 5,0 1),(6 7,8 9,10 11,6 7))",
262  "M 0 -1 l 2 -2 2 -2 z M 6 -7 l 2 -2 2 -2 z",
263  0, 1);
264 
265  /* MultiPoint */
266  do_svg_test(
267  "MULTIPOINT(0 1,2 3)",
268  "x=\"0\" y=\"-1\",x=\"2\" y=\"-3\"",
269  0, 1);
270 
271  /* MultiLine */
272  do_svg_test(
273  "MULTILINESTRING((0 1,2 3,4 5),(6 7,8 9,10 11))",
274  "M 0 -1 l 2 -2 2 -2 M 6 -7 l 2 -2 2 -2",
275  0, 1);
276 
277  /* MultiPolygon */
278  do_svg_test(
279  "MULTIPOLYGON(((0 1,2 3,4 5,0 1)),((6 7,8 9,10 11,6 7)))",
280  "M 0 -1 l 2 -2 2 -2 z M 6 -7 l 2 -2 2 -2 z",
281  0, 1);
282 
283  /* GeometryCollection */
284  do_svg_test(
285  "GEOMETRYCOLLECTION(POINT(0 1),LINESTRING(2 3,4 5))",
286  "x=\"0\" y=\"-1\";M 2 -3 l 2 -2",
287  0, 1);
288 }
289 
290 static void out_svg_test_srid(void)
291 {
292  /* SRID - with Circle point */
293  do_svg_test(
294  "SRID=4326;POINT(0 1)",
295  "cx=\"0\" cy=\"-1\"",
296  0, 0);
297 
298  /* SRID - with Point */
299  do_svg_test(
300  "SRID=4326;POINT(0 1)",
301  "x=\"0\" y=\"-1\"",
302  0, 1);
303 
304  /* SRID - with PointArray */
305  do_svg_test(
306  "SRID=4326;LINESTRING(0 1,2 3)",
307  "M 0 -1 L 2 -3",
308  0, 0);
309 
310  /* SRID - with relative PointArray */
311  do_svg_test(
312  "SRID=4326;LINESTRING(0 1,2 3)",
313  "M 0 -1 l 2 -2",
314  0, 1);
315 }
316 
317 /*
318 ** Used by test harness to register the tests in this file.
319 */
320 void out_svg_suite_setup(void);
322 {
323  CU_pSuite suite = CU_add_suite("svg_output", NULL, NULL);
329 }
static uint8_t precision
Definition: cu_in_twkb.c:25
static void out_svg_test_srid(void)
Definition: cu_out_svg.c:290
static void out_svg_test_geoms(void)
Definition: cu_out_svg.c:164
static void out_svg_test_relative(void)
Definition: cu_out_svg.c:245
static void do_svg_unsupported(char *in, char *out)
Definition: cu_out_svg.c:38
void out_svg_suite_setup(void)
Definition: cu_out_svg.c:321
static void out_svg_test_dims(void)
Definition: cu_out_svg.c:136
static void do_svg_test(char *in, char *out, int precision, int relative)
Definition: cu_out_svg.c:20
static void out_svg_test_precision(void)
Definition: cu_out_svg.c:58
void cu_error_msg_reset()
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
#define PG_ADD_TEST(suite, testfunc)
char * lwgeom_to_svg(const LWGEOM *geom, int precision, int relative)
Takes a GEOMETRY and returns a SVG representation.
Definition: lwout_svg.c:56
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904