PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_out_wkb.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cu_out_wkb.c 13242 2015-02-19 00:22:22Z pramsey $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2010 Paul Ramsey <pramsey@cleverelephant.ca>
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 /*
22 ** Global variable to hold hex WKB strings
23 */
24 char *s;
25 
26 /*
27 ** The suite initialization function.
28 ** Create any re-used objects.
29 */
30 static int init_wkb_out_suite(void)
31 {
32  s = NULL;
33  return 0;
34 }
35 
36 /*
37 ** The suite cleanup function.
38 ** Frees any global objects.
39 */
40 static int clean_wkb_out_suite(void)
41 {
42  if (s) free(s);
43  s = NULL;
44  return 0;
45 }
46 
47 /*
48 ** Creating an input from a hexwkb
49 */
50 static void cu_wkb_from_hexwkb(char *hexwkb)
51 {
53  if ( s ) free(s);
54  s = (char*)lwgeom_to_wkb(g, WKB_HEX | WKB_XDR | WKB_EXTENDED, 0);
55  lwgeom_free(g);
56 }
57 
58 /*
59 ** Creating an input WKB from a wkb string
60 */
61 static void cu_wkb(char *wkt)
62 {
64  if ( s ) free(s);
65  s = (char*)lwgeom_to_wkb(g, WKB_HEX | WKB_XDR | WKB_EXTENDED, NULL);
66  lwgeom_free(g);
67 }
68 
69 
70 static void test_wkb_out_point(void)
71 {
72  cu_wkb("POINT(0 0 0 0)");
73  CU_ASSERT_STRING_EQUAL(s,"00C00000010000000000000000000000000000000000000000000000000000000000000000");
74 
75  cu_wkb("SRID=4;POINTM(1 1 1)");
76  CU_ASSERT_STRING_EQUAL(s,"0060000001000000043FF00000000000003FF00000000000003FF0000000000000");
77 }
78 
79 static void test_wkb_out_linestring(void)
80 {
81  cu_wkb("LINESTRING(0 0,1 1)");
82  CU_ASSERT_STRING_EQUAL(s,"000000000200000002000000000000000000000000000000003FF00000000000003FF0000000000000");
83 
84  cu_wkb("LINESTRING(0 0 1,1 1 2,2 2 3)");
85  CU_ASSERT_STRING_EQUAL(s,"008000000200000003000000000000000000000000000000003FF00000000000003FF00000000000003FF00000000000004000000000000000400000000000000040000000000000004008000000000000");
86 
87  cu_wkb("LINESTRING EMPTY");
88  CU_ASSERT_STRING_EQUAL(s,"000000000200000000");
89 }
90 
91 static void test_wkb_out_polygon(void)
92 {
93  cu_wkb("SRID=4;POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0))");
94  CU_ASSERT_STRING_EQUAL(s,"00A000000300000004000000010000000500000000000000000000000000000000000000000000000000000000000000003FF000000000000000000000000000003FF00000000000003FF000000000000000000000000000003FF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
95 
96  cu_wkb("SRID=14;POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
97  CU_ASSERT_STRING_EQUAL(s,"00E00000030000000E00000001000000050000000000000000000000000000000000000000000000003FF000000000000000000000000000003FF0000000000000000000000000000040000000000000003FF00000000000003FF0000000000000000000000000000040080000000000003FF00000000000000000000000000000000000000000000040100000000000000000000000000000000000000000000000000000000000004014000000000000");
98 
99  cu_wkb("SRID=4;POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
100  CU_ASSERT_STRING_EQUAL(s,"00E00000030000000400000001000000050000000000000000000000000000000000000000000000003FF000000000000000000000000000003FF0000000000000000000000000000040000000000000003FF00000000000003FF0000000000000000000000000000040080000000000003FF00000000000000000000000000000000000000000000040100000000000000000000000000000000000000000000000000000000000004014000000000000");
101 
102  cu_wkb("POLYGON EMPTY");
103  CU_ASSERT_STRING_EQUAL(s,"000000000300000000");
104 
105  /*
106  * POLYGON with EMPTY shell
107  * See http://http://trac.osgeo.org/postgis/ticket/937
108  */
109  cu_wkb_from_hexwkb("01030000000100000000000000");
110  CU_ASSERT_STRING_EQUAL(s,"000000000300000000");
111 }
112 
113 static void test_wkb_out_multipoint(void)
114 {
115  cu_wkb("SRID=4;MULTIPOINT(0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)");
116  CU_ASSERT_STRING_EQUAL(s,"00A000000400000004000000050080000001000000000000000000000000000000000000000000000000008000000100000000000000003FF0000000000000000000000000000000800000013FF00000000000003FF0000000000000000000000000000000800000013FF0000000000000000000000000000000000000000000000080000001000000000000000000000000000000000000000000000000");
117 
118  cu_wkb("MULTIPOINT(0 0 0, 0.26794919243112270647255365849413 1 3)");
119  //printf("WKB: %s",s);
120  CU_ASSERT_STRING_EQUAL(s,"008000000400000002008000000100000000000000000000000000000000000000000000000000800000013FD126145E9ECD563FF00000000000004008000000000000");
121 }
122 
123 static void test_wkb_out_multilinestring(void) {}
124 
125 static void test_wkb_out_multipolygon(void)
126 {
127  cu_wkb("SRID=14;MULTIPOLYGON(((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),((-1 -1 0,-1 2 0,2 2 0,2 -1 0,-1 -1 0),(0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)))");
128  CU_ASSERT_STRING_EQUAL(s,"00A00000060000000E000000020080000003000000010000000500000000000000000000000000000000000000000000000000000000000000003FF000000000000000000000000000003FF00000000000003FF000000000000000000000000000003FF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000030000000200000005BFF0000000000000BFF00000000000000000000000000000BFF0000000000000400000000000000000000000000000004000000000000000400000000000000000000000000000004000000000000000BFF00000000000000000000000000000BFF0000000000000BFF000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000003FF000000000000000000000000000003FF00000000000003FF000000000000000000000000000003FF000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
129 }
130 
131 static void test_wkb_out_collection(void)
132 {
133  cu_wkb("SRID=14;GEOMETRYCOLLECTION(POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1))");
134  CU_ASSERT_STRING_EQUAL(s,"00A00000070000000E000000020080000003000000010000000500000000000000000000000000000000000000000000000000000000000000003FF000000000000000000000000000003FF00000000000003FF000000000000000000000000000003FF00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000013FF00000000000003FF00000000000003FF0000000000000");
135 
136  cu_wkb("GEOMETRYCOLLECTION EMPTY");
137  CU_ASSERT_STRING_EQUAL(s,"000000000700000000");
138 }
139 
140 static void test_wkb_out_circularstring(void)
141 {
142  cu_wkb("CIRCULARSTRING(0 -2,-2 0,0 2,2 0,0 -2)");
143  CU_ASSERT_STRING_EQUAL(s,"0000000008000000050000000000000000C000000000000000C000000000000000000000000000000000000000000000004000000000000000400000000000000000000000000000000000000000000000C000000000000000");
144 
145  cu_wkb("CIRCULARSTRING(-5 0 0 4, 0 5 1 3, 5 0 2 2, 10 -5 3 1, 15 0 4 0)");
146  CU_ASSERT_STRING_EQUAL(s,"00C000000800000005C014000000000000000000000000000000000000000000004010000000000000000000000000000040140000000000003FF0000000000000400800000000000040140000000000000000000000000000400000000000000040000000000000004024000000000000C01400000000000040080000000000003FF0000000000000402E000000000000000000000000000040100000000000000000000000000000");
147 
148  cu_wkb("SRID=43;CIRCULARSTRING(-5 0 0 4, 0 5 1 3, 5 0 2 2, 10 -5 3 1, 15 0 4 0)");
149  CU_ASSERT_STRING_EQUAL(s,"00E00000080000002B00000005C014000000000000000000000000000000000000000000004010000000000000000000000000000040140000000000003FF0000000000000400800000000000040140000000000000000000000000000400000000000000040000000000000004024000000000000C01400000000000040080000000000003FF0000000000000402E000000000000000000000000000040100000000000000000000000000000");
150 }
151 
152 static void test_wkb_out_compoundcurve(void)
153 {
154  cu_wkb("COMPOUNDCURVE(CIRCULARSTRING(0 0 0, 0.26794919243112270647255365849413 1 3, 0.5857864376269049511983112757903 1.4142135623730950488016887242097 1),(0.5857864376269049511983112757903 1.4142135623730950488016887242097 1,2 0 0,0 0 0))");
155  CU_ASSERT_STRING_EQUAL(s,"0080000009000000020080000008000000030000000000000000000000000000000000000000000000003FD126145E9ECD563FF000000000000040080000000000003FE2BEC3330188673FF6A09E667F3BCD3FF00000000000000080000002000000033FE2BEC3330188673FF6A09E667F3BCD3FF0000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
156 }
157 
158 static void test_wkb_out_curvpolygon(void)
159 {
160  cu_wkb("CURVEPOLYGON(CIRCULARSTRING(-2 0 0 0,-1 -1 1 2,0 0 2 4,1 -1 3 6,2 0 4 8,0 2 2 4,-2 0 0 0),(-1 0 1 2,0 0.5 2 4,1 0 3 6,0 1 3 4,-1 0 1 2))");
161  CU_ASSERT_STRING_EQUAL(s,"00C000000A0000000200C000000800000007C000000000000000000000000000000000000000000000000000000000000000BFF0000000000000BFF00000000000003FF0000000000000400000000000000000000000000000000000000000000000400000000000000040100000000000003FF0000000000000BFF00000000000004008000000000000401800000000000040000000000000000000000000000000401000000000000040200000000000000000000000000000400000000000000040000000000000004010000000000000C00000000000000000000000000000000000000000000000000000000000000000C000000200000005BFF000000000000000000000000000003FF0000000000000400000000000000000000000000000003FE0000000000000400000000000000040100000000000003FF000000000000000000000000000004008000000000000401800000000000000000000000000003FF000000000000040080000000000004010000000000000BFF000000000000000000000000000003FF00000000000004000000000000000");
162 }
163 
164 static void test_wkb_out_multicurve(void) {}
165 
166 static void test_wkb_out_multisurface(void) {}
167 
169 {
170 // cu_wkb("POLYHEDRALSURFACE(((0 0 0 0,0 0 1 0,0 1 0 2,0 0 0 0)),((0 0 0 0,0 1 0 0,1 0 0 4,0 0 0 0)),((0 0 0 0,1 0 0 0,0 0 1 6,0 0 0 0)),((1 0 0 0,0 1 0 0,0 0 1 0,1 0 0 0)))");
171 // CU_ASSERT_STRING_EQUAL(s, t);
172 // printf("\nnew: %s\nold: %s\n",s,t);
173 }
174 
175 /*
176 ** Used by test harness to register the tests in this file.
177 */
178 void wkb_out_suite_setup(void);
180 {
181  CU_pSuite suite = CU_add_suite("WKB Output", init_wkb_out_suite, clean_wkb_out_suite);
195 }
void wkb_out_suite_setup(void)
Definition: cu_out_wkb.c:179
static void test_wkb_out_circularstring(void)
Definition: cu_out_wkb.c:140
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:844
static void cu_wkb(char *wkt)
Definition: cu_out_wkb.c:61
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1706
static void cu_wkb_from_hexwkb(char *hexwkb)
Definition: cu_out_wkb.c:50
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
Definition: lwout_wkb.c:692
static void test_wkb_out_multipoint(void)
Definition: cu_out_wkb.c:113
static void test_wkb_out_multilinestring(void)
Definition: cu_out_wkb.c:123
#define PG_ADD_TEST(suite, testfunc)
static void test_wkb_out_collection(void)
Definition: cu_out_wkb.c:131
static void test_wkb_out_multicurve(void)
Definition: cu_out_wkb.c:164
static void test_wkb_out_linestring(void)
Definition: cu_out_wkb.c:79
static int clean_wkb_out_suite(void)
Definition: cu_out_wkb.c:40
#define WKB_EXTENDED
Definition: liblwgeom.h:1769
static void test_wkb_out_polygon(void)
Definition: cu_out_wkb.c:91
static void test_wkb_out_multisurface(void)
Definition: cu_out_wkb.c:166
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:753
static int init_wkb_out_suite(void)
Definition: cu_out_wkb.c:30
static void test_wkb_out_point(void)
Definition: cu_out_wkb.c:70
#define WKB_XDR
Definition: liblwgeom.h:1771
static void test_wkb_out_compoundcurve(void)
Definition: cu_out_wkb.c:152
char * s
Definition: cu_out_wkb.c:24
static void test_wkb_out_polyhedralsurface(void)
Definition: cu_out_wkb.c:168
static void test_wkb_out_multipolygon(void)
Definition: cu_out_wkb.c:125
static void test_wkb_out_curvpolygon(void)
Definition: cu_out_wkb.c:158
#define WKB_HEX
Definition: liblwgeom.h:1772