PostGIS  2.2.7dev-r@@SVN_REVISION@@
cu_libgeom.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2009 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 static void test_typmod_macros(void)
21 {
22  int32_t typmod = 0;
23  int srid = 4326;
24  int type = 6;
25  int z = 1;
26  int rv;
27 
28  TYPMOD_SET_SRID(typmod,srid);
29  rv = TYPMOD_GET_SRID(typmod);
30  CU_ASSERT_EQUAL(rv, srid);
31 
32  srid = -5005;
33  TYPMOD_SET_SRID(typmod,srid);
34  rv = TYPMOD_GET_SRID(typmod);
35  CU_ASSERT_EQUAL(rv, srid);
36 
37  srid = 999999;
38  TYPMOD_SET_SRID(typmod,srid);
39  rv = TYPMOD_GET_SRID(typmod);
40  CU_ASSERT_EQUAL(rv, srid);
41 
42  srid = -999999;
43  TYPMOD_SET_SRID(typmod,srid);
44  rv = TYPMOD_GET_SRID(typmod);
45  CU_ASSERT_EQUAL(rv, srid);
46 
47  srid = SRID_UNKNOWN;
48  TYPMOD_SET_SRID(typmod,srid);
49  rv = TYPMOD_GET_SRID(typmod);
50  CU_ASSERT_EQUAL(rv, srid);
51 
52  srid = 0;
53  TYPMOD_SET_SRID(typmod,srid);
54  rv = TYPMOD_GET_SRID(typmod);
55  CU_ASSERT_EQUAL(rv, srid);
56 
57  srid = 1;
58  TYPMOD_SET_SRID(typmod,srid);
59  rv = TYPMOD_GET_SRID(typmod);
60  CU_ASSERT_EQUAL(rv, srid);
61 
62  TYPMOD_SET_TYPE(typmod,type);
63  rv = TYPMOD_GET_TYPE(typmod);
64  CU_ASSERT_EQUAL(rv,type);
65 
66  TYPMOD_SET_Z(typmod);
67  rv = TYPMOD_GET_Z(typmod);
68  CU_ASSERT_EQUAL(rv,z);
69 
70  rv = TYPMOD_GET_M(typmod);
71  CU_ASSERT_EQUAL(rv,0);
72 
73 }
74 
75 static void test_flags_macros(void)
76 {
77  uint8_t flags = 0;
78 
79  CU_ASSERT_EQUAL(0, FLAGS_GET_Z(flags));
80  FLAGS_SET_Z(flags, 1);
81  CU_ASSERT_EQUAL(1, FLAGS_GET_Z(flags));
82  FLAGS_SET_Z(flags, 0);
83  CU_ASSERT_EQUAL(0, FLAGS_GET_Z(flags));
84  CU_ASSERT_EQUAL(0, FLAGS_GET_BBOX(flags));
85 
86  CU_ASSERT_EQUAL(0, FLAGS_GET_M(flags));
87  FLAGS_SET_M(flags, 1);
88  CU_ASSERT_EQUAL(1, FLAGS_GET_M(flags));
89 
90  CU_ASSERT_EQUAL(0, FLAGS_GET_BBOX(flags));
91  FLAGS_SET_BBOX(flags, 1);
92  CU_ASSERT_EQUAL(1, FLAGS_GET_BBOX(flags));
93  CU_ASSERT_EQUAL(0, FLAGS_GET_READONLY(flags));
94 
95  FLAGS_SET_READONLY(flags, 1);
96  CU_ASSERT_EQUAL(1, FLAGS_GET_READONLY(flags));
97  FLAGS_SET_READONLY(flags, 0);
98  CU_ASSERT_EQUAL(0, FLAGS_GET_READONLY(flags));
99 
100  CU_ASSERT_EQUAL(0, FLAGS_GET_GEODETIC(flags));
101  FLAGS_SET_GEODETIC(flags, 1);
102  CU_ASSERT_EQUAL(1, FLAGS_GET_GEODETIC(flags));
103 
104  flags = gflags(1, 0, 1); /* z=1, m=0, geodetic=1 */
105 
106  CU_ASSERT_EQUAL(1, FLAGS_GET_GEODETIC(flags));
107  CU_ASSERT_EQUAL(1, FLAGS_GET_Z(flags));
108  CU_ASSERT_EQUAL(0, FLAGS_GET_M(flags));
109  CU_ASSERT_EQUAL(2, FLAGS_GET_ZM(flags));
110 
111  flags = gflags(1, 1, 1); /* z=1, m=1, geodetic=1 */
112 
113  CU_ASSERT_EQUAL(1, FLAGS_GET_GEODETIC(flags));
114  CU_ASSERT_EQUAL(1, FLAGS_GET_Z(flags));
115  CU_ASSERT_EQUAL(1, FLAGS_GET_M(flags));
116  CU_ASSERT_EQUAL(3, FLAGS_GET_ZM(flags));
117 
118  flags = gflags(0, 1, 0); /* z=0, m=1, geodetic=0 */
119 
120  CU_ASSERT_EQUAL(0, FLAGS_GET_GEODETIC(flags));
121  CU_ASSERT_EQUAL(0, FLAGS_GET_Z(flags));
122  CU_ASSERT_EQUAL(1, FLAGS_GET_M(flags));
123  CU_ASSERT_EQUAL(1, FLAGS_GET_ZM(flags));
124 }
125 
126 static void test_serialized_srid(void)
127 {
128  GSERIALIZED s;
129  int32_t srid, rv;
130 
131  srid = 4326;
132  gserialized_set_srid(&s, srid);
133  rv = gserialized_get_srid(&s);
134  CU_ASSERT_EQUAL(rv, srid);
135 
136  srid = -3005;
137  gserialized_set_srid(&s, srid);
138  rv = gserialized_get_srid(&s);
139  //printf("srid=%d rv=%d\n",srid,rv);
140  CU_ASSERT_EQUAL(rv, SRID_UNKNOWN);
141 
142  srid = SRID_UNKNOWN;
143  gserialized_set_srid(&s, srid);
144  rv = gserialized_get_srid(&s);
145  CU_ASSERT_EQUAL(rv, srid);
146 
147  srid = SRID_UNKNOWN;
148  gserialized_set_srid(&s, srid);
149  rv = gserialized_get_srid(&s);
150  CU_ASSERT_EQUAL(rv, srid);
151 
152  srid = 100000;
153  gserialized_set_srid(&s, srid);
154  rv = gserialized_get_srid(&s);
155  CU_ASSERT_EQUAL(rv, srid);
156 }
157 
159 {
160  LWGEOM *g;
161  size_t size = 0;
162 
163  g = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
165  CU_ASSERT_EQUAL( size, 32 );
166  lwgeom_free(g);
167 
168  g = lwgeom_from_wkt("POINT(0 0 0)", LW_PARSER_CHECK_NONE);
170  CU_ASSERT_EQUAL( size, 40 );
171  lwgeom_free(g);
172 
173  g = lwgeom_from_wkt("MULTIPOINT(0 0 0, 1 1 1)", LW_PARSER_CHECK_NONE);
175  CU_ASSERT_EQUAL( size, 80 );
176  lwgeom_free(g);
177 
178  g = lwgeom_from_wkt("LINESTRING(0 0, 1 1)", LW_PARSER_CHECK_NONE);
180  CU_ASSERT_EQUAL( size, 48 );
181  lwgeom_free(g);
182 
183  g = lwgeom_from_wkt("MULTILINESTRING((0 0, 1 1),(0 0, 1 1))", LW_PARSER_CHECK_NONE);
185  CU_ASSERT_EQUAL( size, 96 );
186  lwgeom_free(g);
187 
188  g = lwgeom_from_wkt("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))", LW_PARSER_CHECK_NONE);
190  CU_ASSERT_EQUAL( size, 104 );
191  lwgeom_free(g);
192 
193  g = lwgeom_from_wkt("POLYGON((-1 -1, -1 2, 2 2, 2 -1, -1 -1), (0 0, 0 1, 1 1, 1 0, 0 0))", LW_PARSER_CHECK_NONE);
195  CU_ASSERT_EQUAL( size, 184 );
196  lwgeom_free(g);
197 
198 }
199 
200 static void test_lwgeom_calculate_gbox(void)
201 {
202  LWGEOM *g;
203  GBOX b;
204 
205  g = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
207  CU_ASSERT_DOUBLE_EQUAL(b.xmin, 0.0, 0.0000001);
208  lwgeom_free(g);
209 
210  /* Inf = 0x7FF0000000000000 */
211  /* POINT(0 0) = 00 00000001 0000000000000000 0000000000000000 */
212  /* POINT(0 Inf) = 00 00000001 0000000000000000 7FF0000000000000 */
213  g = lwgeom_from_hexwkb("000000000100000000000000007FF0000000000000", LW_PARSER_CHECK_NONE);
215  CU_ASSERT_DOUBLE_EQUAL(b.xmin, 0.0, 0.0000001);
216  CU_ASSERT(isinf(b.ymax));
217  lwgeom_free(g);
218 
219  /* LINESTRING(0 0, 0 Inf) = 00 00000002 00000002 0000000000000000 7FF0000000000000 0000000000000000 0000000000000000 */
220  /* Inf should show up in bbox */
221  g = lwgeom_from_hexwkb("00000000020000000200000000000000007FF000000000000000000000000000000000000000000000", LW_PARSER_CHECK_NONE);
223  CU_ASSERT_DOUBLE_EQUAL(b.xmin, 0.0, 0.0000001);
224  CU_ASSERT(isinf(b.ymax));
225  lwgeom_free(g);
226 
227  /* Geometry with NaN 0101000020E8640000000000000000F8FF000000000000F8FF */
228  /* NaN should show up in bbox for "SRID=4326;POINT(0 NaN)" */
229  g = lwgeom_from_hexwkb("0101000020E86400000000000000000000000000000000F8FF", LW_PARSER_CHECK_NONE);
231  CU_ASSERT(isnan(b.ymax));
232  lwgeom_free(g);
233 
234 }
235 
236 static void test_gbox_serialized_size(void)
237 {
238  uint8_t flags = gflags(0, 0, 0);
239  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
240  FLAGS_SET_BBOX(flags, 1);
241  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
242  FLAGS_SET_Z(flags, 1);
243  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
244  FLAGS_SET_M(flags, 1);
245  CU_ASSERT_EQUAL(gbox_serialized_size(flags),32);
246  FLAGS_SET_GEODETIC(flags, 1);
247  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
248 
249 }
250 
251 
252 
254 {
255  LWGEOM *geom;
256  GSERIALIZED *g;
257  char *in_ewkt;
258  char *out_ewkt;
259  int i = 0;
260 
261  char *ewkt[] =
262  {
263  "POINT EMPTY",
264  "POINT(0 0.2)",
265  "LINESTRING EMPTY",
266  "LINESTRING(-1 -1,-1 2.5,2 2,2 -1)",
267  "MULTIPOINT EMPTY",
268  "MULTIPOINT(0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9)",
269  "SRID=1;MULTILINESTRING EMPTY",
270  "SRID=1;MULTILINESTRING((-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1))",
271  "SRID=1;MULTILINESTRING((-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1))",
272  "POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
273  "POLYGON EMPTY",
274  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
275  "SRID=4326;POLYGON EMPTY",
276  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5))",
277  "SRID=100000;POLYGON((-1 -1 3,-1 2.5 3,2 2 3,2 -1 3,-1 -1 3),(0 0 3,0 1 3,1 1 3,1 0 3,0 0 3),(-0.5 -0.5 3,-0.5 -0.4 3,-0.4 -0.4 3,-0.4 -0.5 3,-0.5 -0.5 3))",
278  "SRID=4326;MULTIPOLYGON(((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)),((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)))",
279  "SRID=4326;MULTIPOLYGON EMPTY",
280  "SRID=4326;GEOMETRYCOLLECTION(POINT(0 1),POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0)),MULTIPOLYGON(((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5))))",
281  "SRID=4326;GEOMETRYCOLLECTION EMPTY",
282  "SRID=4326;GEOMETRYCOLLECTION(POINT EMPTY,MULTIPOLYGON EMPTY)",
283  "MULTICURVE((5 5 1 3,3 5 2 2,3 3 3 1,0 3 1 1),CIRCULARSTRING(0 0 0 0,0.26794 1 3 -2,0.5857864 1.414213 1 2))",
284  "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)))",
285  "MULTISURFACE(CURVEPOLYGON(CIRCULARSTRING EMPTY))",
286  };
287 
288  for ( i = 0; i < (sizeof ewkt/sizeof(char*)); i++ )
289  {
290  LWGEOM* geom2;
291 
292  in_ewkt = ewkt[i];
293  geom = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
294  lwgeom_add_bbox(geom);
295  if ( geom->bbox ) gbox_float_round(geom->bbox);
296  g = gserialized_from_lwgeom(geom, 0, 0);
297 
298  geom2 = lwgeom_from_gserialized(g);
299  out_ewkt = lwgeom_to_ewkt(geom2);
300 
301  /* printf("\n in = %s\nout = %s\n", in_ewkt, out_ewkt); */
302  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
303 
304  /* either both or none of the bboxes are null */
305  CU_ASSERT( (geom->bbox != NULL) || (geom2->bbox == NULL) );
306 
307  /* either both are null or they are the same */
308  CU_ASSERT(geom->bbox == NULL || gbox_same(geom->bbox, geom2->bbox));
309 
310  lwgeom_free(geom);
311  lwgeom_free(geom2);
312  lwfree(g);
313  lwfree(out_ewkt);
314  }
315 
316 }
317 
318 
319 static void test_gserialized_is_empty(void)
320 {
321  int i = 0;
322  struct gserialized_empty_cases {
323  const char* wkt;
324  int isempty;
325  };
326 
327  struct gserialized_empty_cases cases[] = {
328  { "POINT EMPTY", 1 },
329  { "POINT(1 1)", 0 },
330  { "LINESTRING EMPTY", 1 },
331  { "MULTILINESTRING EMPTY", 1 },
332  { "MULTILINESTRING(EMPTY)", 1 },
333  { "MULTILINESTRING(EMPTY,EMPTY)", 1 },
334  { "MULTILINESTRING(EMPTY,(0 0,1 1))", 0 },
335  { "MULTILINESTRING((0 0,1 1),EMPTY)", 0 },
336  { "MULTILINESTRING(EMPTY,(0 0,1 1),EMPTY)", 0 },
337  { "MULTILINESTRING(EMPTY,EMPTY,EMPTY)", 1 },
338  { "GEOMETRYCOLLECTION(POINT EMPTY,MULTILINESTRING(EMPTY,EMPTY,EMPTY))", 1 },
339  { "GEOMETRYCOLLECTION(POINT EMPTY,MULTILINESTRING(EMPTY),POINT(1 1))", 0 },
340  { "GEOMETRYCOLLECTION(POINT EMPTY,MULTILINESTRING(EMPTY, (0 0)),POINT EMPTY)", 0 },
341  { "GEOMETRYCOLLECTION(POLYGON EMPTY,POINT EMPTY,MULTILINESTRING(EMPTY,EMPTY),POINT EMPTY)", 1 },
342  { "GEOMETRYCOLLECTION(POLYGON EMPTY,GEOMETRYCOLLECTION(POINT EMPTY),MULTILINESTRING(EMPTY,EMPTY),POINT EMPTY)", 1 },
343  { NULL, 0 }
344  };
345 
346  while( cases[i].wkt )
347  {
348  // i = 11;
349  LWGEOM *lw = lwgeom_from_wkt(cases[i].wkt, LW_PARSER_CHECK_NONE);
350  GSERIALIZED *g = gserialized_from_lwgeom(lw, 0, 0);
351  int ie = gserialized_is_empty(g);
352  // printf("%s: we say %d, they say %d\n", cases[i].wkt, cases[i].isempty, ie);
353  CU_ASSERT_EQUAL(ie, cases[i].isempty);
354  lwgeom_free(lw);
355  lwfree(g);
356  i++;
357  }
358 }
359 
360 
362 {
363  int rv;
364  uint8_t type = 0;
365  int z = 0, m = 0;
366  char *str;
367 
368  str = " POINTZ";
369  rv = geometry_type_from_string(str, &type, &z, &m);
370  //printf("\n in type: %s\nout type: %d\n out z: %d\n out m: %d", str, type, z, m);
371  CU_ASSERT_EQUAL(rv, LW_SUCCESS);
372  CU_ASSERT_EQUAL(type, POINTTYPE);
373  CU_ASSERT_EQUAL(z, 1);
374  CU_ASSERT_EQUAL(m, 0);
375 
376  str = "LINESTRINGM ";
377  rv = geometry_type_from_string(str, &type, &z, &m);
378  //printf("\n in type: %s\nout type: %d\n out z: %d\n out m: %d", str, type, z, m);
379  CU_ASSERT_EQUAL(rv, LW_SUCCESS);
380  CU_ASSERT_EQUAL(type, LINETYPE);
381  CU_ASSERT_EQUAL(z, 0);
382  CU_ASSERT_EQUAL(m, 1);
383 
384  str = "MULTIPOLYGONZM";
385  rv = geometry_type_from_string(str, &type, &z, &m);
386  //printf("\n in type: %s\nout type: %d\n out z: %d\n out m: %d", str, type, z, m);
387  CU_ASSERT_EQUAL(rv, LW_SUCCESS);
388  CU_ASSERT_EQUAL(type, MULTIPOLYGONTYPE);
389  CU_ASSERT_EQUAL(z, 1);
390  CU_ASSERT_EQUAL(m, 1);
391 
392  str = " GEOMETRYCOLLECTIONZM ";
393  rv = geometry_type_from_string(str, &type, &z, &m);
394  //printf("\n in type: %s\nout type: %d\n out z: %d\n out m: %d", str, type, z, m);
395  CU_ASSERT_EQUAL(rv, LW_SUCCESS);
396  CU_ASSERT_EQUAL(type, COLLECTIONTYPE);
397  CU_ASSERT_EQUAL(z, 1);
398  CU_ASSERT_EQUAL(m, 1);
399 
400  str = " GEOMERYCOLLECTIONZM ";
401  rv = geometry_type_from_string(str, &type, &z, &m);
402  //printf("\n in type: %s\nout type: %d\n out z: %d\n out m: %d", str, type, z, m);
403  CU_ASSERT_EQUAL(rv, LW_FAILURE);
404 
405 }
406 
407 static void test_lwgeom_count_vertices(void)
408 {
409  LWGEOM *geom;
410 
411  geom = lwgeom_from_wkt("MULTIPOINT(-1 -1,-1 2.5,2 2,2 -1)", LW_PARSER_CHECK_NONE);
412  CU_ASSERT_EQUAL(lwgeom_count_vertices(geom),4);
413  lwgeom_free(geom);
414 
415  geom = lwgeom_from_wkt("SRID=1;MULTILINESTRING((-1 -131,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1))", LW_PARSER_CHECK_NONE);
416  CU_ASSERT_EQUAL(lwgeom_count_vertices(geom),16);
417  lwgeom_free(geom);
418 
419  geom = lwgeom_from_wkt("SRID=4326;MULTIPOLYGON(((-1 -1,-1 2.5,211 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)),((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)))", LW_PARSER_CHECK_NONE);
420  CU_ASSERT_EQUAL(lwgeom_count_vertices(geom),30);
421  lwgeom_free(geom);
422 
423 }
424 
426 {
427  LWGEOM *lwgeom;
428  GSERIALIZED *g_ser1;
429  size_t ret_size;
430 
431  lwgeom = lwgeom_from_wkt("MULTIPOINT(-1 -1,-1 2.5,2 2,2 -1,1 1,2 2,4 5)", LW_PARSER_CHECK_NONE);
432  CU_ASSERT_EQUAL(lwgeom_count_vertices(lwgeom),7);
433  g_ser1 = gserialized_from_lwgeom(lwgeom, 1, &ret_size);
434  lwgeom_free(lwgeom);
435 
436  lwgeom = lwgeom_from_gserialized(g_ser1);
437  CU_ASSERT_EQUAL(lwgeom_count_vertices(lwgeom),7);
438  lwgeom_free(lwgeom);
439 
440  lwgeom = lwgeom_from_gserialized(g_ser1);
441 
442  CU_ASSERT_EQUAL(lwgeom_count_vertices(lwgeom),7);
443  lwgeom_free(lwgeom);
444 
445  lwfree(g_ser1);
446 
447 }
448 
449 static void test_lwcollection_extract(void)
450 {
451 
452  LWGEOM *geom;
453  LWCOLLECTION *col;
454 
455  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0))", LW_PARSER_CHECK_NONE);
456 
457  col = lwcollection_extract((LWCOLLECTION*)geom, 1);
458  CU_ASSERT_EQUAL(col->type, MULTIPOINTTYPE);
459  lwcollection_free(col);
460 
461  col = lwcollection_extract((LWCOLLECTION*)geom, 2);
462  CU_ASSERT_EQUAL(col->type, MULTILINETYPE);
463  lwcollection_free(col);
464 
465  col = lwcollection_extract((LWCOLLECTION*)geom, 3);
466  CU_ASSERT_EQUAL(col->type, MULTIPOLYGONTYPE);
467  lwcollection_free(col);
468 
469  lwgeom_free(geom);
470 
471  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION EMPTY", LW_PARSER_CHECK_NONE);
472 
473  col = lwcollection_extract((LWCOLLECTION*)geom, 1);
474  CU_ASSERT_EQUAL(col->type, MULTIPOINTTYPE);
475  lwcollection_free(col);
476 
477  col = lwcollection_extract((LWCOLLECTION*)geom, 2);
478  CU_ASSERT_EQUAL(col->type, MULTILINETYPE);
479  lwcollection_free(col);
480 
481  col = lwcollection_extract((LWCOLLECTION*)geom, 3);
482  CU_ASSERT_EQUAL(col->type, MULTIPOLYGONTYPE);
483  lwcollection_free(col);
484 
485  lwgeom_free(geom);
486 }
487 
488 static void test_lwgeom_free(void)
489 {
490  LWGEOM *geom;
491 
492  /* Empty geometries don't seem to free properly (#370) */
493  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION EMPTY", LW_PARSER_CHECK_NONE);
494  CU_ASSERT_EQUAL(geom->type, COLLECTIONTYPE);
495  lwgeom_free(geom);
496 
497  /* Empty geometries don't seem to free properly (#370) */
498  geom = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
499  CU_ASSERT_EQUAL(geom->type, POLYGONTYPE);
500  lwgeom_free(geom);
501 
502  /* Empty geometries don't seem to free properly (#370) */
503  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_NONE);
504  CU_ASSERT_EQUAL(geom->type, LINETYPE);
505  lwgeom_free(geom);
506 
507  /* Empty geometries don't seem to free properly (#370) */
508  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
509  CU_ASSERT_EQUAL(geom->type, POINTTYPE);
510  lwgeom_free(geom);
511 
512 }
513 
514 static void do_lwgeom_flip_coordinates(char *in, char *out)
515 {
516  LWGEOM *g;
517  char * t;
518  double xmax, ymax;
519  int testbox;
520 
522  lwgeom_add_bbox(g);
523 
524  testbox = (g->bbox != NULL);
525  if ( testbox )
526  {
527  xmax = g->bbox->xmax;
528  ymax = g->bbox->ymax;
529  }
530 
532 
533  if ( testbox )
534  {
535  CU_ASSERT_DOUBLE_EQUAL(g->bbox->xmax, ymax, 0.00001);
536  CU_ASSERT_DOUBLE_EQUAL(g->bbox->ymax, xmax, 0.00001);
537  }
538 
539  t = lwgeom_to_wkt(g, WKT_EXTENDED, 8, NULL);
540  if (t == NULL) fprintf(stderr, "In:%s", in);
541  if (strcmp(t, out))
542  fprintf(stderr, "\nIn: %s\nOut: %s\nTheo: %s\n", in, t, out);
543 
544  CU_ASSERT_STRING_EQUAL(t, out)
545 
546  lwgeom_free(g);
547  lwfree(t);
548 }
549 
551 {
552  /*
553  * 2D geometries types
554  */
556  "POINT(1 2)",
557  "POINT(2 1)"
558  );
559 
561  "LINESTRING(1 2,3 4)",
562  "LINESTRING(2 1,4 3)"
563  );
564 
566  "POLYGON((1 2,3 4,5 6,1 2))",
567  "POLYGON((2 1,4 3,6 5,2 1))"
568  );
569 
571  "POLYGON((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8))",
572  "POLYGON((2 1,4 3,6 5,2 1),(8 7,10 9,12 11,8 7))"
573  );
574 
576  "MULTIPOINT(1 2,3 4)",
577  "MULTIPOINT(2 1,4 3)"
578  );
579 
581  "MULTILINESTRING((1 2,3 4),(5 6,7 8))",
582  "MULTILINESTRING((2 1,4 3),(6 5,8 7))"
583  );
584 
586  "MULTIPOLYGON(((1 2,3 4,5 6,7 8)),((9 10,11 12,13 14,10 9)))",
587  "MULTIPOLYGON(((2 1,4 3,6 5,8 7)),((10 9,12 11,14 13,9 10)))"
588  );
589 
591  "GEOMETRYCOLLECTION EMPTY",
592  "GEOMETRYCOLLECTION EMPTY"
593  );
594 
596  "GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6))",
597  "GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(4 3,6 5))"
598  );
599 
601  "GEOMETRYCOLLECTION(POINT(1 2),GEOMETRYCOLLECTION(LINESTRING(3 4,5 6)))",
602  "GEOMETRYCOLLECTION(POINT(2 1),GEOMETRYCOLLECTION(LINESTRING(4 3,6 5)))"
603  );
604 
606  "CIRCULARSTRING(-2 0,0 2,2 0,0 2,2 4)",
607  "CIRCULARSTRING(0 -2,2 0,0 2,2 0,4 2)"
608  );
609 
611  "COMPOUNDCURVE(CIRCULARSTRING(0 1,1 1,1 0),(1 0,0 1))",
612  "COMPOUNDCURVE(CIRCULARSTRING(1 0,1 1,0 1),(0 1,1 0))"
613  );
614 
616  "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))",
617  "CURVEPOLYGON(CIRCULARSTRING(0 -2,-1 -1,0 0,-1 1,0 2,2 0,0 -2),(0 -1,0.5 0,0 1,1 0,0 -1))"
618  );
619 
621  "MULTICURVE((5 5,3 5,3 3,0 3),CIRCULARSTRING(0 0,2 1,2 3))",
622  "MULTICURVE((5 5,5 3,3 3,3 0),CIRCULARSTRING(0 0,1 2,3 2))"
623  );
624 
626  "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)))",
627  "MULTISURFACE(CURVEPOLYGON(CIRCULARSTRING(0 -2,-1 -1,0 0,-1 1,0 2,2 0,0 -2),(0 -1,0.5 0,0 1,1 0,0 -1)),((8 7,10 10,14 6,11 4,8 7)))"
628  );
629 
630 
631  /*
632  * Ndims
633  */
634 
636  "POINT(1 2 3)",
637  "POINT(2 1 3)"
638  );
639 
641  "POINTM(1 2 3)",
642  "POINTM(2 1 3)"
643  );
644 
646  "POINT(1 2 3 4)",
647  "POINT(2 1 3 4)"
648  );
649 
650 
651  /*
652  * Srid
653  */
654 
656  "SRID=4326;POINT(1 2)",
657  "SRID=4326;POINT(2 1)"
658  );
659 
661  "SRID=0;POINT(1 2)",
662  "POINT(2 1)"
663  );
664 }
665 
666 static void test_f2d(void)
667 {
668  double d = 1000000.123456789123456789;
669  float f;
670  double e;
671 
672  f = next_float_down(d);
673  d = next_float_down(f);
674  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
675 
676  e = (double)f;
677  CU_ASSERT_DOUBLE_EQUAL(f,e, 0.0000001);
678 
679  f = next_float_down(d);
680  d = next_float_down(f);
681  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
682 
683  f = next_float_up(d);
684  d = next_float_up(f);
685  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
686 
687  f = next_float_up(d);
688  d = next_float_up(f);
689  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
690 }
691 
692 /*
693  * This is a test for memory leaks, can't really test
694  * w/out checking with a leak detector (ie: valgrind)
695  *
696  * See http://trac.osgeo.org/postgis/ticket/1102
697  */
698 static void test_lwgeom_clone(void)
699 {
700  int i;
701 
702  char *ewkt[] =
703  {
704  "POINT(0 0.2)",
705  "LINESTRING(-1 -1,-1 2.5,2 2,2 -1)",
706  "MULTIPOINT(0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9,0.9 0.9)",
707  "SRID=1;MULTILINESTRING((-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1))",
708  "SRID=1;MULTILINESTRING((-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1),(-1 -1,-1 2.5,2 2,2 -1))",
709  "POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
710  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
711  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5))",
712  "SRID=100000;POLYGON((-1 -1 3,-1 2.5 3,2 2 3,2 -1 3,-1 -1 3),(0 0 3,0 1 3,1 1 3,1 0 3,0 0 3),(-0.5 -0.5 3,-0.5 -0.4 3,-0.4 -0.4 3,-0.4 -0.5 3,-0.5 -0.5 3))",
713  "SRID=4326;MULTIPOLYGON(((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)),((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5)))",
714  "SRID=4326;GEOMETRYCOLLECTION(POINT(0 1),POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0)),MULTIPOLYGON(((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0),(-0.5 -0.5,-0.5 -0.4,-0.4 -0.4,-0.4 -0.5,-0.5 -0.5))))",
715  "MULTICURVE((5 5 1 3,3 5 2 2,3 3 3 1,0 3 1 1),CIRCULARSTRING(0 0 0 0,0.26794 1 3 -2,0.5857864 1.414213 1 2))",
716  "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)))",
717  "TIN(((0 0 0,0 0 1,0 1 0,0 0 0)),((0 0 0,0 1 0,1 0 0,0 0 0)),((0 0 0,1 0 0,0 0 1,0 0 0)),((1 0 0,0 1 0,0 0 1,1 0 0)))"
718  };
719 
720 
721  for ( i = 0; i < (sizeof ewkt/sizeof(char *)); i++ )
722  {
723  LWGEOM *geom, *cloned;
724  char *in_ewkt;
725  char *out_ewkt;
726 
727  in_ewkt = ewkt[i];
728  geom = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
729  cloned = lwgeom_clone(geom);
730  out_ewkt = lwgeom_to_ewkt(cloned);
731  if (strcmp(in_ewkt, out_ewkt))
732  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
733  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
734  lwfree(out_ewkt);
735  lwgeom_free(cloned);
736  lwgeom_free(geom);
737  }
738 
739 
740 }
741 
742 /*
743  * Test lwgeom_force_clockwise
744  */
746 {
747  LWGEOM *geom;
748  LWGEOM *geom2;
749  char *in_ewkt, *out_ewkt;
750 
751  /* counterclockwise, must be reversed */
752  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
754  in_ewkt = "POLYGON((0 0,0 10,10 10,10 0,0 0))";
755  out_ewkt = lwgeom_to_ewkt(geom);
756  if (strcmp(in_ewkt, out_ewkt))
757  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
758  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
759  lwfree(out_ewkt);
760  lwgeom_free(geom);
761 
762  /* clockwise, fine as is */
763  geom = lwgeom_from_wkt("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", LW_PARSER_CHECK_NONE);
765  in_ewkt = "POLYGON((0 0,0 10,10 10,10 0,0 0))";
766  out_ewkt = lwgeom_to_ewkt(geom);
767  if (strcmp(in_ewkt, out_ewkt))
768  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
769  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
770  lwfree(out_ewkt);
771  lwgeom_free(geom);
772 
773  /* counterclockwise shell (must be reversed), mixed-wise holes */
774  geom = lwgeom_from_wkt("POLYGON((0 0,10 0,10 10,0 10,0 0),(2 2,2 4,4 2,2 2),(6 2,8 2,8 4,6 2))", LW_PARSER_CHECK_NONE);
776  in_ewkt = "POLYGON((0 0,0 10,10 10,10 0,0 0),(2 2,4 2,2 4,2 2),(6 2,8 2,8 4,6 2))";
777  out_ewkt = lwgeom_to_ewkt(geom);
778  if (strcmp(in_ewkt, out_ewkt))
779  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
780  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
781  lwfree(out_ewkt);
782  lwgeom_free(geom);
783 
784  /* clockwise shell (fine), mixed-wise holes */
785  geom = lwgeom_from_wkt("POLYGON((0 0,0 10,10 10,10 0,0 0),(2 2,4 2,2 4,2 2),(6 2,8 4,8 2,6 2))", LW_PARSER_CHECK_NONE);
787  in_ewkt = "POLYGON((0 0,0 10,10 10,10 0,0 0),(2 2,4 2,2 4,2 2),(6 2,8 2,8 4,6 2))";
788  out_ewkt = lwgeom_to_ewkt(geom);
789  if (strcmp(in_ewkt, out_ewkt))
790  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
791  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
792  lwfree(out_ewkt);
793  lwgeom_free(geom);
794 
795  /* clockwise narrow ring, fine as-is */
796  /* NOTE: this is a narrow ring, see ticket #1302 */
797  in_ewkt = "0103000000010000000500000000917E9BA468294100917E9B8AEA2841C976BE1FA4682941C976BE9F8AEA2841B39ABE1FA46829415ACCC29F8AEA284137894120A4682941C976BE9F8AEA284100917E9BA468294100917E9B8AEA2841";
798  geom = lwgeom_from_hexwkb(in_ewkt, LW_PARSER_CHECK_NONE);
799  geom2 = lwgeom_from_hexwkb(in_ewkt, LW_PARSER_CHECK_NONE);
800  lwgeom_force_clockwise(geom2);
801 
804  CU_ASSERT( lwgeom_same(geom, geom2) );
805  lwgeom_free(geom);
806  lwgeom_free(geom2);
807 }
808 
809 /*
810  * Test lwgeom_is_empty
811  */
812 static void test_lwgeom_is_empty(void)
813 {
814  LWGEOM *geom;
815 
816  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
817  CU_ASSERT( !lwgeom_is_empty(geom) );
818  lwgeom_free(geom);
819 
820  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
821  CU_ASSERT( lwgeom_is_empty(geom) );
822  lwgeom_free(geom);
823 
824  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_NONE);
825  CU_ASSERT( lwgeom_is_empty(geom) );
826  lwgeom_free(geom);
827 
828  geom = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
829  CU_ASSERT( lwgeom_is_empty(geom) );
830  lwgeom_free(geom);
831 
832  geom = lwgeom_from_wkt("MULTIPOINT EMPTY", LW_PARSER_CHECK_NONE);
833  CU_ASSERT( lwgeom_is_empty(geom) );
834  lwgeom_free(geom);
835 
836  geom = lwgeom_from_wkt("MULTILINESTRING EMPTY", LW_PARSER_CHECK_NONE);
837  CU_ASSERT( lwgeom_is_empty(geom) );
838  lwgeom_free(geom);
839 
840  geom = lwgeom_from_wkt("MULTIPOLYGON EMPTY", LW_PARSER_CHECK_NONE);
841  CU_ASSERT( lwgeom_is_empty(geom) );
842  lwgeom_free(geom);
843 
844  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION EMPTY, POINT EMPTY, LINESTRING EMPTY, POLYGON EMPTY, MULTIPOINT EMPTY, MULTILINESTRING EMPTY, MULTIPOLYGON EMPTY, GEOMETRYCOLLECTION(MULTIPOLYGON EMPTY))", LW_PARSER_CHECK_NONE);
845  CU_ASSERT( lwgeom_is_empty(geom) );
846  lwgeom_free(geom);
847 
848 }
849 
850 /*
851  * Test lwgeom_same
852  */
853 static void test_lwgeom_same(void)
854 {
855  LWGEOM *geom, *geom2;
856 
857  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
858  CU_ASSERT( lwgeom_same(geom, geom) );
859  lwgeom_add_bbox(geom);
860  CU_ASSERT( lwgeom_same(geom, geom) );
861  lwgeom_free(geom);
862 
863  geom = lwgeom_from_wkt("MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))", LW_PARSER_CHECK_NONE);
864  CU_ASSERT( lwgeom_same(geom, geom) );
865  lwgeom_add_bbox(geom);
866  CU_ASSERT( lwgeom_same(geom, geom) );
867  lwgeom_free(geom);
868 
869  geom = lwgeom_from_wkt("LINESTRING(0 0, 2 0)", LW_PARSER_CHECK_NONE);
870  CU_ASSERT( lwgeom_same(geom, geom) );
871  lwgeom_add_bbox(geom);
872  CU_ASSERT( lwgeom_same(geom, geom) );
873  lwgeom_free(geom);
874 
875  geom = lwgeom_from_wkt("MULTILINESTRING((0 0, 2 0))", LW_PARSER_CHECK_NONE);
876  CU_ASSERT( lwgeom_same(geom, geom) );
877  lwgeom_add_bbox(geom);
878  CU_ASSERT( lwgeom_same(geom, geom) );
879  lwgeom_free(geom);
880 
881  geom = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
882  CU_ASSERT( lwgeom_same(geom, geom) );
883  lwgeom_add_bbox(geom);
884  CU_ASSERT( lwgeom_same(geom, geom) );
885  lwgeom_free(geom);
886 
887  geom = lwgeom_from_wkt("MULTIPOINT((0 0),(4 5))", LW_PARSER_CHECK_NONE);
888  CU_ASSERT( lwgeom_same(geom, geom) );
889  lwgeom_add_bbox(geom);
890  CU_ASSERT( lwgeom_same(geom, geom) );
891  lwgeom_free(geom);
892 
893  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
894  CU_ASSERT( lwgeom_same(geom, geom) );
895  lwgeom_add_bbox(geom);
896  CU_ASSERT( lwgeom_same(geom, geom) );
897  lwgeom_free(geom);
898 
899  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_NONE);
900  CU_ASSERT( lwgeom_same(geom, geom) );
901  lwgeom_add_bbox(geom);
902  CU_ASSERT( lwgeom_same(geom, geom) );
903  lwgeom_free(geom);
904 
905  geom = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
906  CU_ASSERT( lwgeom_same(geom, geom) );
907  lwgeom_free(geom);
908 
909  geom = lwgeom_from_wkt("MULTIPOINT EMPTY", LW_PARSER_CHECK_NONE);
910  CU_ASSERT( lwgeom_same(geom, geom) );
911  lwgeom_free(geom);
912 
913  geom = lwgeom_from_wkt("MULTILINESTRING EMPTY", LW_PARSER_CHECK_NONE);
914  CU_ASSERT( lwgeom_same(geom, geom) );
915  lwgeom_free(geom);
916 
917  geom = lwgeom_from_wkt("MULTIPOLYGON EMPTY", LW_PARSER_CHECK_NONE);
918  CU_ASSERT( lwgeom_same(geom, geom) );
919  lwgeom_free(geom);
920 
921  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION EMPTY, POINT EMPTY, LINESTRING EMPTY, POLYGON EMPTY, MULTIPOINT EMPTY, MULTILINESTRING EMPTY, MULTIPOLYGON EMPTY, GEOMETRYCOLLECTION(MULTIPOLYGON EMPTY))", LW_PARSER_CHECK_NONE);
922  CU_ASSERT( lwgeom_same(geom, geom) );
923  lwgeom_free(geom);
924 
925  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
926  geom2 = lwgeom_from_wkt("GEOMETRYCOLLECTION(GEOMETRYCOLLECTION EMPTY, POINT EMPTY, LINESTRING EMPTY, POLYGON EMPTY, MULTIPOINT EMPTY, MULTILINESTRING EMPTY, MULTIPOLYGON EMPTY, GEOMETRYCOLLECTION(MULTIPOLYGON EMPTY))", LW_PARSER_CHECK_NONE);
927  CU_ASSERT( ! lwgeom_same(geom, geom2) );
928  lwgeom_free(geom);
929  lwgeom_free(geom2);
930 
931  geom = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
932  geom2 = lwgeom_from_wkt("MULTIPOINT((0 0))", LW_PARSER_CHECK_NONE);
933  CU_ASSERT( ! lwgeom_same(geom, geom2) );
934  lwgeom_free(geom);
935  lwgeom_free(geom2);
936 
937  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
938  geom2 = lwgeom_from_wkt("POINT Z EMPTY", LW_PARSER_CHECK_NONE);
939  CU_ASSERT( ! lwgeom_same(geom, geom2) );
940  lwgeom_free(geom);
941  lwgeom_free(geom2);
942 
943 }
944 
945 /*
946  * Test lwgeom_force_curve
947  */
948 static void test_lwgeom_as_curve(void)
949 {
950  LWGEOM *geom;
951  LWGEOM *geom2;
952  char *in_ewkt, *out_ewkt;
953 
954  geom = lwgeom_from_wkt("LINESTRING(0 0, 10 0)", LW_PARSER_CHECK_NONE);
955  geom2 = lwgeom_as_curve(geom);
956  in_ewkt = "COMPOUNDCURVE((0 0,10 0))";
957  out_ewkt = lwgeom_to_ewkt(geom2);
958  if (strcmp(in_ewkt, out_ewkt))
959  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
960  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
961  lwfree(out_ewkt);
962  lwgeom_free(geom);
963  lwgeom_free(geom2);
964 
965  geom = lwgeom_from_wkt("MULTILINESTRING((0 0, 10 0))", LW_PARSER_CHECK_NONE);
966  geom2 = lwgeom_as_curve(geom);
967  in_ewkt = "MULTICURVE((0 0,10 0))";
968  out_ewkt = lwgeom_to_ewkt(geom2);
969  if (strcmp(in_ewkt, out_ewkt))
970  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
971  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
972  lwfree(out_ewkt);
973  lwgeom_free(geom);
974  lwgeom_free(geom2);
975 
976  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
977  geom2 = lwgeom_as_curve(geom);
978  in_ewkt = "CURVEPOLYGON((0 0,10 0,10 10,0 10,0 0))";
979  out_ewkt = lwgeom_to_ewkt(geom2);
980  if (strcmp(in_ewkt, out_ewkt))
981  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
982  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
983  lwfree(out_ewkt);
984  lwgeom_free(geom);
985  lwgeom_free(geom2);
986 
987  geom = lwgeom_from_wkt("MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))", LW_PARSER_CHECK_NONE);
988  geom2 = lwgeom_as_curve(geom);
989  in_ewkt = "MULTISURFACE(((0 0,10 0,10 10,0 10,0 0)))";
990  out_ewkt = lwgeom_to_ewkt(geom2);
991  if (strcmp(in_ewkt, out_ewkt))
992  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
993  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
994  lwfree(out_ewkt);
995  lwgeom_free(geom);
996  lwgeom_free(geom2);
997 
998 }
999 
1000 static void test_lwline_from_lwmpoint(void)
1001 {
1002  LWLINE *line;
1003  LWMPOINT *mpoint;
1004 
1005 // LWLINE *
1006 // lwline_from_lwmpoint(int srid, LWMPOINT *mpoint)
1007 
1008  mpoint = (LWMPOINT*)lwgeom_from_wkt("MULTIPOINT(0 0, 0 1, 1 1, 1 2, 2 2)", LW_PARSER_CHECK_NONE);
1009  line = lwline_from_lwmpoint(SRID_DEFAULT, mpoint);
1010  CU_ASSERT_EQUAL(line->points->npoints, mpoint->ngeoms);
1011  CU_ASSERT_DOUBLE_EQUAL(lwline_length_2d(line), 4.0, 0.000001);
1012 
1013  lwline_free(line);
1014  lwmpoint_free(mpoint);
1015 }
1016 
1017 /*
1018  * Test lwgeom_scale
1019  */
1020 static void test_lwgeom_scale(void)
1021 {
1022  LWGEOM *geom;
1023  POINT4D factor;
1024  char *out_ewkt;
1025  GBOX *box;
1026 
1027  geom = lwgeom_from_wkt("SRID=4326;GEOMETRYCOLLECTION(POINT(0 1 2 3),POLYGON((-1 -1 0 1,-1 2.5 0 1,2 2 0 1,2 -1 0 1,-1 -1 0 1),(0 0 1 2,0 1 1 2,1 1 1 2,1 0 2 3,0 0 1 2)),LINESTRING(0 0 0 0, 1 2 3 4))", LW_PARSER_CHECK_NONE);
1028  factor.x = 2; factor.y = 3; factor.z = 4; factor.m = 5;
1029  lwgeom_scale(geom, &factor);
1030  out_ewkt = lwgeom_to_ewkt(geom);
1031  ASSERT_STRING_EQUAL(out_ewkt, "SRID=4326;GEOMETRYCOLLECTION(POINT(0 3 8 15),POLYGON((-2 -3 0 5,-2 7.5 0 5,4 6 0 5,4 -3 0 5,-2 -3 0 5),(0 0 4 10,0 3 4 10,2 3 4 10,2 0 8 15,0 0 4 10)),LINESTRING(0 0 0 0,2 6 12 20))");
1032  lwgeom_free(geom);
1033  lwfree(out_ewkt);
1034 
1035  geom = lwgeom_from_wkt("POINT(1 1 1 1)", LW_PARSER_CHECK_NONE);
1036  lwgeom_add_bbox(geom);
1037  factor.x = 2; factor.y = 3; factor.z = 4; factor.m = 5;
1038  lwgeom_scale(geom, &factor);
1039  box = geom->bbox;
1040  ASSERT_DOUBLE_EQUAL(box->xmin, 2);
1041  ASSERT_DOUBLE_EQUAL(box->xmax, 2);
1042  ASSERT_DOUBLE_EQUAL(box->ymin, 3);
1043  ASSERT_DOUBLE_EQUAL(box->ymax, 3);
1044  ASSERT_DOUBLE_EQUAL(box->zmin, 4);
1045  ASSERT_DOUBLE_EQUAL(box->zmax, 4);
1046  ASSERT_DOUBLE_EQUAL(box->mmin, 5);
1047  ASSERT_DOUBLE_EQUAL(box->mmax, 5);
1048  lwgeom_free(geom);
1049 }
1050 
1051 void test_gbox_same_2d(void);
1053 {
1054  LWGEOM* g1 = lwgeom_from_wkt("LINESTRING(0 0, 1 1)", LW_PARSER_CHECK_NONE);
1055  LWGEOM* g2 = lwgeom_from_wkt("LINESTRING(0 0, 0 1, 1 1)", LW_PARSER_CHECK_NONE);
1056  LWGEOM* g3 = lwgeom_from_wkt("LINESTRING(0 0, 1 1.000000000001)", LW_PARSER_CHECK_NONE);
1057 
1058  lwgeom_add_bbox(g1);
1059  lwgeom_add_bbox(g2);
1060  lwgeom_add_bbox(g3);
1061 
1062  CU_ASSERT_TRUE(gbox_same_2d(g1->bbox, g2->bbox));
1063  CU_ASSERT_FALSE(gbox_same_2d(g1->bbox, g3->bbox));
1064 
1065  /* Serializing a GBOX with precise coordinates renders the boxes not strictly equal,
1066  * but still equal according to gbox_same_2d_float.
1067  */
1068  GSERIALIZED* s3 = gserialized_from_lwgeom(g3, LW_FALSE, NULL);
1069  GBOX s3box;
1070  gserialized_read_gbox_p(s3, &s3box);
1071 
1072  CU_ASSERT_FALSE(gbox_same_2d(g3->bbox, &s3box));
1073  CU_ASSERT_TRUE(gbox_same_2d_float(g3->bbox, &s3box));
1074 
1075  /* The serialized box equals itself by either the exact or closest-float compares */
1076  CU_ASSERT_TRUE(gbox_same_2d(&s3box, &s3box));
1077  CU_ASSERT_TRUE(gbox_same_2d_float(&s3box, &s3box));
1078 
1079  lwgeom_free(g1);
1080  lwgeom_free(g2);
1081  lwgeom_free(g3);
1082  lwfree(s3);
1083 }
1084 
1085 void test_signum_macro(void);
1087 {
1088  CU_ASSERT_EQUAL(SIGNUM(-5.0),-1);
1089  CU_ASSERT_EQUAL(SIGNUM( 5.0), 1);
1090  CU_ASSERT_EQUAL(SIGNUM( 0.0), 0);
1091  CU_ASSERT_EQUAL(SIGNUM(10) * 5, 5);
1092  CU_ASSERT_EQUAL(SIGNUM(-10) * 5, -5);
1093 }
1094 
1095 /*
1096 ** Used by test harness to register the tests in this file.
1097 */
1098 void libgeom_suite_setup(void);
1100 {
1101  CU_pSuite suite = CU_add_suite("serialization/deserialization", NULL, NULL);
1112  PG_ADD_TEST(suite, test_lwgeom_free);
1114  PG_ADD_TEST(suite, test_f2d);
1119  PG_ADD_TEST(suite, test_lwgeom_same);
1126 }
double x
Definition: liblwgeom.h:336
#define LINETYPE
Definition: liblwgeom.h:71
#define TYPMOD_GET_TYPE(typmod)
Definition: liblwgeom.h:151
static void test_gserialized_from_lwgeom_size(void)
Definition: cu_libgeom.c:158
int gserialized_read_gbox_p(const GSERIALIZED *g, GBOX *gbox)
Pull a GBOX from the header of a GSERIALIZED, if one is available.
Definition: g_serialized.c:158
GBOX * bbox
Definition: liblwgeom.h:382
static void test_on_gser_lwgeom_count_vertices(void)
Definition: cu_libgeom.c:425
double m
Definition: liblwgeom.h:336
#define TYPMOD_GET_M(typmod)
Definition: liblwgeom.h:155
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:353
#define TYPMOD_SET_Z(typmod)
Definition: liblwgeom.h:154
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:655
static void test_typmod_macros(void)
Definition: cu_libgeom.c:20
void lwfree(void *mem)
Definition: lwutil.c:214
#define FLAGS_GET_READONLY(flags)
Definition: liblwgeom.h:128
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:355
void test_gbox_same_2d(void)
Definition: cu_libgeom.c:1052
uint8_t type
Definition: liblwgeom.h:487
static void test_gserialized_is_empty(void)
Definition: cu_libgeom.c:319
static void test_lwgeom_scale(void)
Definition: cu_libgeom.c:1020
#define ASSERT_STRING_EQUAL(o, e)
#define FLAGS_GET_GEODETIC(flags)
Definition: liblwgeom.h:127
#define POLYGONTYPE
Definition: liblwgeom.h:72
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:469
#define TYPMOD_GET_SRID(typmod)
Macros for manipulating the 'typemod' int.
Definition: liblwgeom.h:149
double xmax
Definition: liblwgeom.h:277
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
#define MULTIPOINTTYPE
Definition: liblwgeom.h:73
int geometry_type_from_string(const char *str, uint8_t *type, int *z, int *m)
Calculate type integer and dimensional flags from string input.
Definition: g_util.c:148
#define LW_SUCCESS
Definition: liblwgeom.h:65
void lwline_free(LWLINE *line)
Definition: lwline.c:63
void lwgeom_scale(LWGEOM *geom, const POINT4D *factors)
Definition: lwgeom.c:1742
static void test_geometry_type_from_string(void)
Definition: cu_libgeom.c:361
#define SRID_DEFAULT
Definition: liblwgeom.h:179
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:137
void gserialized_set_srid(GSERIALIZED *s, int32_t srid)
Write the SRID into the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:86
double lwline_length_2d(const LWLINE *line)
Definition: lwline.c:557
static void test_lwgeom_force_clockwise(void)
Definition: cu_libgeom.c:745
float next_float_down(double d)
Definition: lwgeom_api.c:143
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:890
static void test_lwcollection_extract(void)
Definition: cu_libgeom.c:449
#define FLAGS_SET_GEODETIC(flags, value)
Definition: liblwgeom.h:133
static void test_lwgeom_as_curve(void)
Definition: cu_libgeom.c:948
static void test_gbox_serialized_size(void)
Definition: cu_libgeom.c:236
static void test_flags_macros(void)
Definition: cu_libgeom.c:75
LWGEOM * lwgeom_flip_coordinates(LWGEOM *in)
Reverse the X and Y coordinate order.
Definition: lwgeom.c:1458
static void test_lwgeom_free(void)
Definition: cu_libgeom.c:488
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:139
#define LW_FAILURE
Definition: liblwgeom.h:64
float next_float_up(double d)
Definition: lwgeom_api.c:159
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1869
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:130
void lwgeom_force_clockwise(LWGEOM *lwgeom)
Ensure the outer ring is clockwise oriented and all inner rings are counter-clockwise.
Definition: lwgeom.c:23
void gbox_float_round(GBOX *gbox)
Round given GBOX to float boundaries.
Definition: g_box.c:682
static void do_lwgeom_flip_coordinates(char *in, char *out)
Definition: cu_libgeom.c:514
void lwmpoint_free(LWMPOINT *mpt)
Definition: lwmpoint.c:59
double zmax
Definition: liblwgeom.h:281
double ymin
Definition: liblwgeom.h:278
static void test_serialized_srid(void)
Definition: cu_libgeom.c:126
void libgeom_suite_setup(void)
Definition: cu_libgeom.c:1099
double xmin
Definition: liblwgeom.h:276
static void test_f2d(void)
Definition: cu_libgeom.c:666
#define FLAGS_GET_BBOX(flags)
Definition: liblwgeom.h:126
#define LW_FALSE
Definition: liblwgeom.h:62
void test_signum_macro(void)
Definition: cu_libgeom.c:1086
static void test_lwgeom_calculate_gbox(void)
Definition: cu_libgeom.c:200
LWLINE * lwline_from_lwmpoint(int srid, const LWMPOINT *mpoint)
Definition: lwline.c:261
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
#define PG_ADD_TEST(suite, testfunc)
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, int is_geodetic, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
Definition: g_serialized.c:906
static void test_lwgeom_is_empty(void)
Definition: cu_libgeom.c:812
double ymax
Definition: liblwgeom.h:279
char * s
Definition: cu_in_wkt.c:23
#define TYPMOD_SET_TYPE(typmod, type)
Definition: liblwgeom.h:152
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:124
int gbox_same_2d(const GBOX *g1, const GBOX *g2)
Check if 2 given GBOX are the same in x and y.
Definition: g_box.c:156
double z
Definition: liblwgeom.h:336
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:395
char lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
geom1 same as geom2 iff
Definition: lwgeom.c:495
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:130
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:779
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
#define WKT_EXTENDED
Definition: liblwgeom.h:1941
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:132
static void test_lwgeom_flip_coordinates(void)
Definition: cu_libgeom.c:550
static void test_lwgeom_clone(void)
Definition: cu_libgeom.c:698
size_t gserialized_from_lwgeom_size(const LWGEOM *geom)
Calculate required memory segment to contain a serialized form of the LWGEOM.
Definition: g_serialized.c:539
static void test_lwgeom_same(void)
Definition: cu_libgeom.c:853
static void test_lwline_from_lwmpoint(void)
Definition: cu_libgeom.c:1000
static void test_lwgeom_from_gserialized(void)
Definition: cu_libgeom.c:253
double mmin
Definition: liblwgeom.h:282
double zmin
Definition: liblwgeom.h:280
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:599
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:125
uint8_t type
Definition: liblwgeom.h:380
double mmax
Definition: liblwgeom.h:283
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:326
#define ASSERT_DOUBLE_EQUAL(o, e)
#define TYPMOD_SET_SRID(typmod, srid)
Definition: liblwgeom.h:150
LWGEOM * lwgeom_as_curve(const LWGEOM *lwgeom)
Create a new LWGEOM of the appropriate CURVE* type.
Definition: lwgeom.c:324
#define TYPMOD_GET_Z(typmod)
Definition: liblwgeom.h:153
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1297
int lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1140
int gbox_same_2d_float(const GBOX *g1, const GBOX *g2)
Check if two given GBOX are the same in x and y, or would round to the same GBOX in x and if serializ...
Definition: g_box.c:164
double y
Definition: liblwgeom.h:336
#define MULTILINETYPE
Definition: liblwgeom.h:74
int ngeoms
Definition: liblwgeom.h:452
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: g_box.c:141
static void test_lwgeom_count_vertices(void)
Definition: cu_libgeom.c:407
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:69
size_t gbox_serialized_size(uint8_t flags)
Return the number of bytes necessary to hold a GBOX of this dimension in serialized form...
Definition: g_box.c:416
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
#define SIGNUM(n)
Macro that returns: -1 if n < 0, 1 if n > 0, 0 if n == 0.
#define FLAGS_SET_READONLY(flags, value)
Definition: liblwgeom.h:134
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:131
POINTARRAY * points
Definition: liblwgeom.h:406
int lwgeom_calculate_gbox_cartesian(const LWGEOM *lwgeom, GBOX *gbox)
Calculate the 2-4D bounding box of a geometry.
Definition: g_box.c:648