PostGIS  3.4.0dev-r@@SVN_REVISION@@
cu_in_twkb.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2010 Paul Ramsey <pramsey@cleverelephant.ca>
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 /*
21 ** Global variable to hold TWKB strings
22 */
23 static char *hex_a;
24 static char *hex_b;
25 static uint8_t precision = 0;
26 static uint8_t variant = 0;
27 
28 /*
29 ** The suite initialization function.
30 ** Create any re-used objects.
31 */
32 static int init_twkb_in_suite(void)
33 {
34  hex_a = NULL;
35  hex_b = NULL;
36  return 0;
37 }
38 
39 /*
40 ** The suite cleanup function.
41 ** Frees any global objects.
42 */
43 static int clean_twkb_in_suite(void)
44 {
45  if (hex_a) free(hex_a);
46  if (hex_b) free(hex_b);
47  hex_a = NULL;
48  hex_b = NULL;
49  return 0;
50 }
51 
52 
53 static void cu_twkb_in(char *wkt)
54 {
56  LWGEOM *g_a, *g_b;
57  /* int i; char *hex; */
58 
59  /* Turn WKT into geom */
61  if ( pr.errcode )
62  {
63  printf("ERROR: %s\n", pr.message);
64  printf("POSITION: %d\n", pr.errlocation);
65  exit(0);
66  }
67 
68  /* Get the geom */
69  g_a = pr.geom;
70 
71  /* Turn geom into TWKB */
73 
74  // printf("\n Size: %ld\n", twkb_size_a);
75 
76  /* Turn TWKB back into geom */
77  g_b = lwgeom_from_twkb((uint8_t *)twkb_a->data, LWSIZE_GET(twkb_a->size) - LWVARHDRSZ, LW_PARSER_CHECK_NONE);
78 
79  // printf("\n Org: %s\n 1st: %s\n 2nd: %s\n", wkt, lwgeom_to_ewkt(g_a), lwgeom_to_ewkt(g_b));
80 
81  /* Turn geom to TWKB again */
83 
84  /* Turn TWKB into hex for comparisons */
85  if ( hex_a ) lwfree(hex_a);
86  if ( hex_b ) lwfree(hex_b);
87  hex_a = hexbytes_from_bytes((uint8_t *)twkb_a->data, LWSIZE_GET(twkb_a->size) - LWVARHDRSZ);
88  hex_b = hexbytes_from_bytes((uint8_t *)twkb_b->data, LWSIZE_GET(twkb_b->size) - LWVARHDRSZ);
89 
90  /* Clean up */
91  lwfree(twkb_a);
92  lwfree(twkb_b);
94  lwgeom_free(g_b);
95 }
96 
97 static void test_twkb_in_point(void)
98 {
99  cu_twkb_in("POINT(0 0 0 0)");
100 // printf("old: %s\nnew: %s\n",hex_a, hex_b);
101  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
102 
103  cu_twkb_in("POINT(1 1)");
104  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
105 
106  cu_twkb_in("POINT EMPTY");
107  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
108 }
109 
110 static void test_twkb_in_linestring(void)
111 {
112  cu_twkb_in("LINESTRING(0 0,1 1)");
113  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
114 
115  cu_twkb_in("LINESTRING(0 0 1,1 1 2,2 2 3)");
116  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
117 
118  cu_twkb_in("LINESTRING EMPTY");
119  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
120 }
121 
122 static void test_twkb_in_polygon(void)
123 {
124  cu_twkb_in("POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0))");
125  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
126 
127  cu_twkb_in("POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
128  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
129 
130  cu_twkb_in("POLYGON((0 0 0 1,0 1 0 2,1 1 0 3,1 0 0 4,0 0 0 5))");
131  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
132 
133  cu_twkb_in("POLYGON EMPTY");
134  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
135 }
136 
137 static void test_twkb_in_multipoint(void)
138 {
139  cu_twkb_in("MULTIPOINT Z EMPTY");
140  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
141 
142  cu_twkb_in("MULTIPOINT(1 2, EMPTY, 5 6)");
143  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
144  // printf(" 1st: %s\n 2nd: %s\n", hex_a, hex_b);
145 
146  cu_twkb_in("MULTIPOINT(0 0 0,0 1 0,1 1 0,1 0 0,0 0 1)");
147  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
148 
149  cu_twkb_in("MULTIPOINT(1 2 3, 1 2 3, 4 5 6, -3 -4 -5, -10 -5 -1)");
150  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
151 }
152 
154 {
155  cu_twkb_in("MULTILINESTRING((0 0,0 1),(1 1, 10 10))");
156  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
157 
158  cu_twkb_in("MULTILINESTRING((0 0,0 1),EMPTY,(1 1, 10 10))");
159  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
160 
161  cu_twkb_in("MULTILINESTRING((0 0 200000,0 1 10),(1 100000000 23, 10 10 45))");
162  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
163  // printf(" 1st: %s\n 2nd: %s\n", hex_a, hex_b);
164 
165  cu_twkb_in("MULTILINESTRING EMPTY");
166  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
167 }
168 
169 static void test_twkb_in_multipolygon(void)
170 {
171  cu_twkb_in("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)))");
172  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
173  //printf("old: %s\nnew: %s\n",hex_a, hex_b);
174 
175  cu_twkb_in("MULTIPOLYGON EMPTY");
176  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
177  //printf("old: %s\nnew: %s\n",hex_a, hex_b);
178 }
179 
180 static void test_twkb_in_collection(void)
181 {
182  cu_twkb_in("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))");
183  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
184 
185  cu_twkb_in("GEOMETRYCOLLECTION(POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1))");
186  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
187 
188  cu_twkb_in("GEOMETRYCOLLECTION EMPTY");
189  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
190 
191  cu_twkb_in("GEOMETRYCOLLECTION(POINT(1 2 3), LINESTRING EMPTY, POINT(4 5 6))");
192  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
193 
194  cu_twkb_in("GEOMETRYCOLLECTION(POINT(1 2 3), POINT EMPTY, POINT(4 5 6))");
195  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
196 }
197 
198 /*
199 ** PRECISION TESTS HERE: We ALTER THE 'precision' GLOBAL
200 */
201 
202 static void test_twkb_in_precision(void)
203 {
204  /* Try these cases at several different precisions */
205  for ( precision = 1; precision <= 6; precision++ )
206  {
207  cu_twkb_in("MULTILINESTRING((0 0,0 1),EMPTY,(1 1, 10 10))");
208  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
209 
210  cu_twkb_in("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)))");
211  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
212 
213  cu_twkb_in("GEOMETRYCOLLECTION(POLYGON((0 0 0,0 1 0,1 1 0,1 0 0,0 0 0)),POINT(1 1 1))");
214  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
215 
216  cu_twkb_in("MULTILINESTRING((0 0 200000,0 1 10),(1 100000000 23, 10 10 45))");
217  // printf("old: %s\nnew: %s\n",hex_a, hex_b);
218  CU_ASSERT_STRING_EQUAL(hex_a, hex_b);
219  }
220 
221  /* Go back to default precision */
222  precision = 0;
223 }
224 
225 
226 
227 /*
228 ** Used by test harness to register the tests in this file.
229 */
230 void twkb_in_suite_setup(void);
232 {
233  CU_pSuite suite = CU_add_suite("twkb_input", init_twkb_in_suite, clean_twkb_in_suite);
242 }
static int clean_twkb_in_suite(void)
Definition: cu_in_twkb.c:43
static void test_twkb_in_multipoint(void)
Definition: cu_in_twkb.c:137
static char * hex_a
Definition: cu_in_twkb.c:23
static void test_twkb_in_multilinestring(void)
Definition: cu_in_twkb.c:153
void twkb_in_suite_setup(void)
Definition: cu_in_twkb.c:231
static void test_twkb_in_multipolygon(void)
Definition: cu_in_twkb.c:169
static void test_twkb_in_precision(void)
Definition: cu_in_twkb.c:202
static void test_twkb_in_collection(void)
Definition: cu_in_twkb.c:180
static void test_twkb_in_polygon(void)
Definition: cu_in_twkb.c:122
static void test_twkb_in_linestring(void)
Definition: cu_in_twkb.c:110
static uint8_t variant
Definition: cu_in_twkb.c:26
static void test_twkb_in_point(void)
Definition: cu_in_twkb.c:97
static int init_twkb_in_suite(void)
Definition: cu_in_twkb.c:32
static uint8_t precision
Definition: cu_in_twkb.c:25
static void cu_twkb_in(char *wkt)
Definition: cu_in_twkb.c:53
static char * hex_b
Definition: cu_in_twkb.c:24
#define PG_ADD_TEST(suite, testfunc)
#define LWVARHDRSZ
Definition: liblwgeom.h:311
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
char * hexbytes_from_bytes(const uint8_t *bytes, size_t size)
Definition: lwout_wkb.c:40
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2114
LWGEOM * lwgeom_from_twkb(const uint8_t *twkb, size_t twkb_size, char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
Definition: lwin_twkb.c:654
#define LWSIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
Definition: liblwgeom.h:324
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
lwvarlena_t * lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m)
Definition: lwout_twkb.c:636
void lwfree(void *mem)
Definition: lwutil.c:242
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
Definition: lwin_wkt.c:886
void free(void *)
uint32_t size
Definition: liblwgeom.h:307
char data[]
Definition: liblwgeom.h:308
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.
Definition: liblwgeom.h:2122