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