PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_in_wkb.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cu_out_wkb.c 6036 2010-10-03 18:14:35Z 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 WKB strings
23 */
24 char *hex_a;
25 char *hex_b;
26 
27 /*
28 ** The suite initialization function.
29 ** Create any re-used objects.
30 */
31 static int init_wkb_in_suite(void)
32 {
33  hex_a = NULL;
34  hex_b = NULL;
35  return 0;
36 }
37 
38 /*
39 ** The suite cleanup function.
40 ** Frees any global objects.
41 */
42 static int clean_wkb_in_suite(void)
43 {
44  if (hex_a) free(hex_a);
45  if (hex_b) free(hex_b);
46  hex_a = NULL;
47  hex_b = NULL;
48  return 0;
49 }
50 
51 static void cu_wkb_malformed_in(char *hex)
52 {
54  int rv = 0;
55 
56  rv = lwgeom_parse_wkt(&p, hex, 0);
57  CU_ASSERT( LW_FAILURE == rv );
58  CU_ASSERT( p.errcode );
59  CU_ASSERT( ! p.geom );
61 }
62 
63 static void cu_wkb_in(char *wkt)
64 {
66  LWGEOM *g_a, *g_b;
67  uint8_t *wkb_a, *wkb_b;
68  size_t wkb_size_a, wkb_size_b;
69  /* int i; char *hex; */
70 
71  if ( hex_a ) free(hex_a);
72  if ( hex_b ) free(hex_b);
73 
74  /* Turn WKT into geom */
76  if ( pr.errcode )
77  {
78  printf("ERROR: %s\n", pr.message);
79  printf("POSITION: %d\n", pr.errlocation);
80  exit(0);
81  }
82 
83  /* Get the geom */
84  g_a = pr.geom;
85 
86  /* Turn geom into WKB */
87  wkb_a = lwgeom_to_wkb(g_a, WKB_NDR | WKB_EXTENDED, &wkb_size_a);
88 
89  /* Turn WKB back into geom */
90  g_b = lwgeom_from_wkb(wkb_a, wkb_size_a, LW_PARSER_CHECK_NONE);
91 
92  /* Turn geom to WKB again */
93  wkb_b = lwgeom_to_wkb(g_b, WKB_NDR | WKB_EXTENDED, &wkb_size_b);
94 
95  /* Turn geoms into WKB for comparisons */
96  hex_a = hexbytes_from_bytes(wkb_a, wkb_size_a);
97  hex_b = hexbytes_from_bytes(wkb_b, wkb_size_b);
98 
99  /* Clean up */
100  lwfree(wkb_a);
101  lwfree(wkb_b);
103  lwgeom_free(g_b);
104 }
105 
106 static void test_wkb_in_point(void)
107 {
108  cu_wkb_in("POINT(0 0 0 0)");
109 // printf("old: %s\nnew: %s\n",hex_a, hex_b);
110  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
111 
112  cu_wkb_in("SRID=4;POINTM(1 1 1)");
113 // printf("old: %s\nnew: %s\n",hex_a, hex_b);
114  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
115 }
116 
117 static void test_wkb_in_linestring(void)
118 {
119  cu_wkb_in("LINESTRING(0 0,1 1)");
120  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
121 
122  cu_wkb_in("LINESTRING(0 0 1,1 1 2,2 2 3)");
123  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
124 }
125 
126 static void test_wkb_in_polygon(void)
127 {
128  cu_wkb_in("SRID=4;POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0))");
129  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
130 
131  cu_wkb_in("SRID=14;POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
132  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
133 
134  cu_wkb_in("SRID=4;POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
135  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
136 
137  cu_wkb_in("POLYGON EMPTY");
138  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
139 }
140 
141 static void test_wkb_in_multipoint(void)
142 {
143  cu_wkb_in("SRID=4;MULTIPOINT(0 0 0,0 1 0,1 1 0,1 0 0,0 0 1)");
144  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
145 
146  cu_wkb_in("MULTIPOINT(0 0 0, 0.26794919243112270647255365849413 1 3)");
147  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
148 }
149 
150 static void test_wkb_in_multilinestring(void) {}
151 
152 static void test_wkb_in_multipolygon(void)
153 {
154  cu_wkb_in("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)))");
155  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
156  //printf("old: %s\nnew: %s\n",hex_a, hex_b);
157 }
158 
159 static void test_wkb_in_collection(void)
160 {
161  cu_wkb_in("SRID=14;GEOMETRYCOLLECTION(POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1))");
162  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
163 
164  cu_wkb_in("GEOMETRYCOLLECTION EMPTY");
165  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
166 
167  cu_wkb_in("SRID=14;GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0))),POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1),LINESTRING(0 0 0, 1 1 1))");
168  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
169 
170 }
171 
172 static void test_wkb_in_circularstring(void)
173 {
174  cu_wkb_in("CIRCULARSTRING(0 -2,-2 0,0 2,2 0,0 -2)");
175  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
176 
177  cu_wkb_in("CIRCULARSTRING(-5 0 0 4, 0 5 1 3, 5 0 2 2, 10 -5 3 1, 15 0 4 0)");
178  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
179 
180  cu_wkb_in("SRID=43;CIRCULARSTRING(-5 0 0 4, 0 5 1 3, 5 0 2 2, 10 -5 3 1, 15 0 4 0)");
181  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
182 }
183 
184 static void test_wkb_in_compoundcurve(void)
185 {
186  cu_wkb_in("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))");
187  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
188 }
189 
190 static void test_wkb_in_curvpolygon(void)
191 {
192  cu_wkb_in("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))");
193  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
194 }
195 
196 static void test_wkb_in_multicurve(void) {}
197 
198 static void test_wkb_in_multisurface(void) {}
199 
200 static void test_wkb_in_malformed(void)
201 {
202  /* See http://trac.osgeo.org/postgis/ticket/1445 */
203  cu_wkb_malformed_in("01060000400200000001040000400100000001010000400000000000000000000000000000000000000000000000000101000040000000000000F03F000000000000F03F000000000000F03F");
204  cu_wkb_malformed_in("01050000400200000001040000400100000001010000400000000000000000000000000000000000000000000000000101000040000000000000F03F000000000000F03F000000000000F03F");
205  cu_wkb_malformed_in("01040000400200000001040000400100000001010000400000000000000000000000000000000000000000000000000101000040000000000000F03F000000000000F03F000000000000F03F");
206  cu_wkb_malformed_in("01030000400200000001040000400100000001010000400000000000000000000000000000000000000000000000000101000040000000000000F03F000000000000F03F000000000000F03F");
207 
208  /* See http://trac.osgeo.org/postgis/ticket/168 */
209  cu_wkb_malformed_in("01060000C00100000001030000C00100000003000000E3D9107E234F5041A3DB66BC97A30F4122ACEF440DAF9440FFFFFFFFFFFFEFFFE3D9107E234F5041A3DB66BC97A30F4122ACEF440DAF9440FFFFFFFFFFFFEFFFE3D9107E234F5041A3DB66BC97A30F4122ACEF440DAF9440FFFFFFFFFFFFEFFF");
210 }
211 
212 
213 /*
214 ** Used by test harness to register the tests in this file.
215 */
216 void wkb_in_suite_setup(void);
218 {
219  CU_pSuite suite = CU_add_suite("WKB Input", init_wkb_in_suite, clean_wkb_in_suite);
233 }
#define WKB_NDR
Definition: liblwgeom.h:1770
void lwfree(void *mem)
Definition: lwutil.c:190
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
static void test_wkb_in_compoundcurve(void)
Definition: cu_in_wkb.c:184
static int init_wkb_in_suite(void)
Definition: cu_in_wkb.c:31
static int clean_wkb_in_suite(void)
Definition: cu_in_wkb.c:42
static void cu_wkb_malformed_in(char *hex)
Definition: cu_in_wkb.c:51
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:825
static void test_wkb_in_curvpolygon(void)
Definition: cu_in_wkb.c:190
char * hexbytes_from_bytes(uint8_t *bytes, size_t size)
Definition: lwout_wkb.c:23
char * hex_a
Definition: cu_in_wkb.c:24
static void test_wkb_in_multisurface(void)
Definition: cu_in_wkb.c:198
static void test_wkb_in_multilinestring(void)
Definition: cu_in_wkb.c:150
#define LW_FAILURE
Definition: liblwgeom.h:54
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1706
static void test_wkb_in_collection(void)
Definition: cu_in_wkb.c:159
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_in_multipolygon(void)
Definition: cu_in_wkb.c:152
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM...
Definition: liblwgeom.h:1713
#define PG_ADD_TEST(suite, testfunc)
void wkb_in_suite_setup(void)
Definition: cu_in_wkb.c:217
static void test_wkb_in_malformed(void)
Definition: cu_in_wkb.c:200
static void test_wkb_in_polygon(void)
Definition: cu_in_wkb.c:126
#define WKB_EXTENDED
Definition: liblwgeom.h:1769
static void cu_wkb_in(char *wkt)
Definition: cu_in_wkb.c:63
static void test_wkb_in_multicurve(void)
Definition: cu_in_wkb.c:196
static void test_wkb_in_circularstring(void)
Definition: cu_in_wkb.c:172
static void test_wkb_in_linestring(void)
Definition: cu_in_wkb.c:117
char * hex_b
Definition: cu_in_wkb.c:25
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
Definition: lwin_wkb.c:728
static void test_wkb_in_multipoint(void)
Definition: cu_in_wkb.c:141
static void test_wkb_in_point(void)
Definition: cu_in_wkb.c:106