PostGIS  2.1.10dev-r@@SVN_REVISION@@
cu_libgeom.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cu_libgeom.c 13698 2015-06-24 20:21:57Z pramsey $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2009 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 static void test_typmod_macros(void)
22 {
23  int32_t typmod = 0;
24  int srid = 4326;
25  int type = 6;
26  int z = 1;
27  int rv;
28 
29  TYPMOD_SET_SRID(typmod,srid);
30  rv = TYPMOD_GET_SRID(typmod);
31  CU_ASSERT_EQUAL(rv, srid);
32 
33  srid = -5005;
34  TYPMOD_SET_SRID(typmod,srid);
35  rv = TYPMOD_GET_SRID(typmod);
36  CU_ASSERT_EQUAL(rv, srid);
37 
38  srid = SRID_UNKNOWN;
39  TYPMOD_SET_SRID(typmod,srid);
40  rv = TYPMOD_GET_SRID(typmod);
41  CU_ASSERT_EQUAL(rv, srid);
42 
43  srid = 0;
44  TYPMOD_SET_SRID(typmod,srid);
45  rv = TYPMOD_GET_SRID(typmod);
46  CU_ASSERT_EQUAL(rv, srid);
47 
48  srid = 1;
49  TYPMOD_SET_SRID(typmod,srid);
50  rv = TYPMOD_GET_SRID(typmod);
51  CU_ASSERT_EQUAL(rv, srid);
52 
53  TYPMOD_SET_TYPE(typmod,type);
54  rv = TYPMOD_GET_TYPE(typmod);
55  CU_ASSERT_EQUAL(rv,type);
56 
57  TYPMOD_SET_Z(typmod);
58  rv = TYPMOD_GET_Z(typmod);
59  CU_ASSERT_EQUAL(rv,z);
60 
61  rv = TYPMOD_GET_M(typmod);
62  CU_ASSERT_EQUAL(rv,0);
63 
64 }
65 
66 static void test_flags_macros(void)
67 {
68  uint8_t flags = 0;
69 
70  CU_ASSERT_EQUAL(0, FLAGS_GET_Z(flags));
71  FLAGS_SET_Z(flags, 1);
72  CU_ASSERT_EQUAL(1, FLAGS_GET_Z(flags));
73  FLAGS_SET_Z(flags, 0);
74  CU_ASSERT_EQUAL(0, FLAGS_GET_Z(flags));
75  CU_ASSERT_EQUAL(0, FLAGS_GET_BBOX(flags));
76 
77  CU_ASSERT_EQUAL(0, FLAGS_GET_M(flags));
78  FLAGS_SET_M(flags, 1);
79  CU_ASSERT_EQUAL(1, FLAGS_GET_M(flags));
80 
81  CU_ASSERT_EQUAL(0, FLAGS_GET_BBOX(flags));
82  FLAGS_SET_BBOX(flags, 1);
83  CU_ASSERT_EQUAL(1, FLAGS_GET_BBOX(flags));
84  CU_ASSERT_EQUAL(0, FLAGS_GET_READONLY(flags));
85 
86  FLAGS_SET_READONLY(flags, 1);
87  CU_ASSERT_EQUAL(1, FLAGS_GET_READONLY(flags));
88  FLAGS_SET_READONLY(flags, 0);
89  CU_ASSERT_EQUAL(0, FLAGS_GET_READONLY(flags));
90 
91  CU_ASSERT_EQUAL(0, FLAGS_GET_GEODETIC(flags));
92  FLAGS_SET_GEODETIC(flags, 1);
93  CU_ASSERT_EQUAL(1, FLAGS_GET_GEODETIC(flags));
94 
95  flags = gflags(1, 0, 1); /* z=1, m=0, geodetic=1 */
96 
97  CU_ASSERT_EQUAL(1, FLAGS_GET_GEODETIC(flags));
98  CU_ASSERT_EQUAL(1, FLAGS_GET_Z(flags));
99  CU_ASSERT_EQUAL(0, FLAGS_GET_M(flags));
100  CU_ASSERT_EQUAL(2, FLAGS_GET_ZM(flags));
101 
102  flags = gflags(1, 1, 1); /* z=1, m=1, geodetic=1 */
103 
104  CU_ASSERT_EQUAL(1, FLAGS_GET_GEODETIC(flags));
105  CU_ASSERT_EQUAL(1, FLAGS_GET_Z(flags));
106  CU_ASSERT_EQUAL(1, FLAGS_GET_M(flags));
107  CU_ASSERT_EQUAL(3, FLAGS_GET_ZM(flags));
108 
109  flags = gflags(0, 1, 0); /* z=0, m=1, geodetic=0 */
110 
111  CU_ASSERT_EQUAL(0, FLAGS_GET_GEODETIC(flags));
112  CU_ASSERT_EQUAL(0, FLAGS_GET_Z(flags));
113  CU_ASSERT_EQUAL(1, FLAGS_GET_M(flags));
114  CU_ASSERT_EQUAL(1, FLAGS_GET_ZM(flags));
115 }
116 
117 static void test_serialized_srid(void)
118 {
119  GSERIALIZED s;
120  int32_t srid, rv;
121 
122  srid = 4326;
123  gserialized_set_srid(&s, srid);
124  rv = gserialized_get_srid(&s);
125  CU_ASSERT_EQUAL(rv, srid);
126 
127  srid = -3005;
128  gserialized_set_srid(&s, srid);
129  rv = gserialized_get_srid(&s);
130  //printf("srid=%d rv=%d\n",srid,rv);
131  CU_ASSERT_EQUAL(rv, SRID_UNKNOWN);
132 
133  srid = SRID_UNKNOWN;
134  gserialized_set_srid(&s, srid);
135  rv = gserialized_get_srid(&s);
136  CU_ASSERT_EQUAL(rv, srid);
137 
138  srid = SRID_UNKNOWN;
139  gserialized_set_srid(&s, srid);
140  rv = gserialized_get_srid(&s);
141  CU_ASSERT_EQUAL(rv, srid);
142 
143  srid = 100000;
144  gserialized_set_srid(&s, srid);
145  rv = gserialized_get_srid(&s);
146  CU_ASSERT_EQUAL(rv, srid);
147 }
148 
150 {
151  LWGEOM *g;
152  size_t size = 0;
153 
154  g = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
156  CU_ASSERT_EQUAL( size, 32 );
157  lwgeom_free(g);
158 
159  g = lwgeom_from_wkt("POINT(0 0 0)", LW_PARSER_CHECK_NONE);
161  CU_ASSERT_EQUAL( size, 40 );
162  lwgeom_free(g);
163 
164  g = lwgeom_from_wkt("MULTIPOINT(0 0 0, 1 1 1)", LW_PARSER_CHECK_NONE);
166  CU_ASSERT_EQUAL( size, 80 );
167  lwgeom_free(g);
168 
169  g = lwgeom_from_wkt("LINESTRING(0 0, 1 1)", LW_PARSER_CHECK_NONE);
171  CU_ASSERT_EQUAL( size, 48 );
172  lwgeom_free(g);
173 
174  g = lwgeom_from_wkt("MULTILINESTRING((0 0, 1 1),(0 0, 1 1))", LW_PARSER_CHECK_NONE);
176  CU_ASSERT_EQUAL( size, 96 );
177  lwgeom_free(g);
178 
179  g = lwgeom_from_wkt("POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))", LW_PARSER_CHECK_NONE);
181  CU_ASSERT_EQUAL( size, 104 );
182  lwgeom_free(g);
183 
184  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);
186  CU_ASSERT_EQUAL( size, 184 );
187  lwgeom_free(g);
188 
189 }
190 
191 static void test_lwgeom_calculate_gbox(void)
192 {
193  LWGEOM *g;
194  GBOX b;
195 
196  g = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
198  CU_ASSERT_DOUBLE_EQUAL(b.xmin, 0.0, 0.0000001);
199  lwgeom_free(g);
200 
201  /* Inf = 0x7FF0000000000000 */
202  /* POINT(0 0) = 00 00000001 0000000000000000 0000000000000000 */
203  /* POINT(0 Inf) = 00 00000001 0000000000000000 7FF0000000000000 */
204  g = lwgeom_from_hexwkb("000000000100000000000000007FF0000000000000", LW_PARSER_CHECK_NONE);
206  CU_ASSERT_DOUBLE_EQUAL(b.xmin, 0.0, 0.0000001);
207  CU_ASSERT(isinf(b.ymax));
208  lwgeom_free(g);
209 
210  /* LINESTRING(0 0, 0 Inf) = 00 00000002 00000002 0000000000000000 7FF0000000000000 0000000000000000 0000000000000000 */
211  /* Inf should show up in bbox */
212  g = lwgeom_from_hexwkb("00000000020000000200000000000000007FF000000000000000000000000000000000000000000000", LW_PARSER_CHECK_NONE);
214  CU_ASSERT_DOUBLE_EQUAL(b.xmin, 0.0, 0.0000001);
215  CU_ASSERT(isinf(b.ymax));
216  lwgeom_free(g);
217 
218  /* Geometry with NaN 0101000020E8640000000000000000F8FF000000000000F8FF */
219  /* NaN should show up in bbox */
220  g = lwgeom_from_hexwkb("0101000020E8640000000000000000F8FF000000000000F8FF", LW_PARSER_CHECK_NONE);
222  CU_ASSERT(isnan(b.ymax));
223  lwgeom_free(g);
224 
225 }
226 
227 static void test_gbox_serialized_size(void)
228 {
229  uint8_t flags = gflags(0, 0, 0);
230  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
231  FLAGS_SET_BBOX(flags, 1);
232  CU_ASSERT_EQUAL(gbox_serialized_size(flags),16);
233  FLAGS_SET_Z(flags, 1);
234  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
235  FLAGS_SET_M(flags, 1);
236  CU_ASSERT_EQUAL(gbox_serialized_size(flags),32);
237  FLAGS_SET_GEODETIC(flags, 1);
238  CU_ASSERT_EQUAL(gbox_serialized_size(flags),24);
239 
240 }
241 
242 
243 
244 
246 {
247  LWGEOM *geom;
248  GSERIALIZED *g;
249  char *in_ewkt;
250  char *out_ewkt;
251  int i = 0;
252 
253  char ewkt[][512] =
254  {
255  "POINT EMPTY",
256  "POINT(0 0.2)",
257  "LINESTRING EMPTY",
258  "LINESTRING(-1 -1,-1 2.5,2 2,2 -1)",
259  "MULTIPOINT EMPTY",
260  "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)",
261  "SRID=1;MULTILINESTRING EMPTY",
262  "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))",
263  "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))",
264  "POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
265  "POLYGON EMPTY",
266  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
267  "SRID=4326;POLYGON EMPTY",
268  "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))",
269  "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))",
270  "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)))",
271  "SRID=4326;MULTIPOLYGON EMPTY",
272  "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))))",
273  "SRID=4326;GEOMETRYCOLLECTION EMPTY",
274  "SRID=4326;GEOMETRYCOLLECTION(POINT EMPTY, MULTIPOLYGON EMPTY)",
275  "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))",
276  "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)))",
277  "MULTISURFACE(CURVEPOLYGON(CIRCULARSTRING EMPTY))",
278  };
279 
280  for ( i = 0; i < 13; i++ )
281  {
282  LWGEOM* geom2;
283 
284  in_ewkt = ewkt[i];
285  geom = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
286  lwgeom_add_bbox(geom);
287  if ( geom->bbox ) gbox_float_round(geom->bbox);
288  g = gserialized_from_lwgeom(geom, 0, 0);
289 
290  geom2 = lwgeom_from_gserialized(g);
291  out_ewkt = lwgeom_to_ewkt(geom2);
292 
293  /* printf("\n in = %s\nout = %s\n", in_ewkt, out_ewkt); */
294  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
295 
296  /* either both or none of the bboxes are null */
297  CU_ASSERT( (geom->bbox != NULL) || (geom2->bbox == NULL) );
298 
299  /* either both are null or they are the same */
300  CU_ASSERT(geom->bbox == NULL || gbox_same(geom->bbox, geom2->bbox));
301 
302  lwgeom_free(geom);
303  lwgeom_free(geom2);
304  lwfree(g);
305  lwfree(out_ewkt);
306  }
307 
308 }
309 
310 
311 static void test_gserialized_is_empty(void)
312 {
313  int i = 0;
314  struct gserialized_empty_cases {
315  const char* wkt;
316  int isempty;
317  };
318 
319  struct gserialized_empty_cases cases[] = {
320  { "POINT EMPTY", 1 },
321  { "POINT(1 1)", 0 },
322  { "LINESTRING EMPTY", 1 },
323  { "MULTILINESTRING EMPTY", 1 },
324  { "MULTILINESTRING(EMPTY)", 1 },
325  { "MULTILINESTRING(EMPTY,EMPTY)", 1 },
326  { "MULTILINESTRING(EMPTY,(0 0,1 1))", 0 },
327  { "MULTILINESTRING((0 0,1 1),EMPTY)", 0 },
328  { "MULTILINESTRING(EMPTY,(0 0,1 1),EMPTY)", 0 },
329  { "MULTILINESTRING(EMPTY,EMPTY,EMPTY)", 1 },
330  { "GEOMETRYCOLLECTION(POINT EMPTY,MULTILINESTRING(EMPTY,EMPTY,EMPTY))", 1 },
331  { "GEOMETRYCOLLECTION(POINT EMPTY,MULTILINESTRING(EMPTY),POINT(1 1))", 0 },
332  { "GEOMETRYCOLLECTION(POINT EMPTY,MULTILINESTRING(EMPTY, (0 0)),POINT EMPTY)", 0 },
333  { "GEOMETRYCOLLECTION(POLYGON EMPTY,POINT EMPTY,MULTILINESTRING(EMPTY,EMPTY),POINT EMPTY)", 1 },
334  { "GEOMETRYCOLLECTION(POLYGON EMPTY,GEOMETRYCOLLECTION(POINT EMPTY),MULTILINESTRING(EMPTY,EMPTY),POINT EMPTY)", 1 },
335  { NULL, 0 }
336  };
337 
338  while( cases[i].wkt )
339  {
340  // i = 11;
341  LWGEOM *lw = lwgeom_from_wkt(cases[i].wkt, LW_PARSER_CHECK_NONE);
342  GSERIALIZED *g = gserialized_from_lwgeom(lw, 0, 0);
343  int ie = gserialized_is_empty(g);
344  // printf("%s: we say %d, they say %d\n", cases[i].wkt, cases[i].isempty, ie);
345  CU_ASSERT_EQUAL(ie, cases[i].isempty);
346  lwgeom_free(lw);
347  lwfree(g);
348  i++;
349  }
350 }
351 
352 
354 {
355  int rv;
356  uint8_t type = 0;
357  int z = 0, m = 0;
358  char *str;
359 
360  str = " POINTZ";
361  rv = geometry_type_from_string(str, &type, &z, &m);
362  //printf("\n in type: %s\nout type: %d\n out z: %d\n out m: %d", str, type, z, m);
363  CU_ASSERT_EQUAL(rv, LW_SUCCESS);
364  CU_ASSERT_EQUAL(type, POINTTYPE);
365  CU_ASSERT_EQUAL(z, 1);
366  CU_ASSERT_EQUAL(m, 0);
367 
368  str = "LINESTRINGM ";
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, LINETYPE);
373  CU_ASSERT_EQUAL(z, 0);
374  CU_ASSERT_EQUAL(m, 1);
375 
376  str = "MULTIPOLYGONZM";
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, MULTIPOLYGONTYPE);
381  CU_ASSERT_EQUAL(z, 1);
382  CU_ASSERT_EQUAL(m, 1);
383 
384  str = " GEOMETRYCOLLECTIONZM ";
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, COLLECTIONTYPE);
389  CU_ASSERT_EQUAL(z, 1);
390  CU_ASSERT_EQUAL(m, 1);
391 
392  str = " GEOMERYCOLLECTIONZM ";
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_FAILURE);
396 
397 }
398 
399 static void test_lwgeom_count_vertices(void)
400 {
401  LWGEOM *geom;
402 
403  geom = lwgeom_from_wkt("MULTIPOINT(-1 -1,-1 2.5,2 2,2 -1)", LW_PARSER_CHECK_NONE);
404  CU_ASSERT_EQUAL(lwgeom_count_vertices(geom),4);
405  lwgeom_free(geom);
406 
407  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);
408  CU_ASSERT_EQUAL(lwgeom_count_vertices(geom),16);
409  lwgeom_free(geom);
410 
411  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);
412  CU_ASSERT_EQUAL(lwgeom_count_vertices(geom),30);
413  lwgeom_free(geom);
414 
415 }
416 
418 {
419  LWGEOM *lwgeom;
420  GSERIALIZED *g_ser1;
421  size_t ret_size;
422 
423  lwgeom = lwgeom_from_wkt("MULTIPOINT(-1 -1,-1 2.5,2 2,2 -1,1 1,2 2,4 5)", LW_PARSER_CHECK_NONE);
424  CU_ASSERT_EQUAL(lwgeom_count_vertices(lwgeom),7);
425  g_ser1 = gserialized_from_lwgeom(lwgeom, 1, &ret_size);
426  lwgeom_free(lwgeom);
427 
428  lwgeom = lwgeom_from_gserialized(g_ser1);
429  CU_ASSERT_EQUAL(lwgeom_count_vertices(lwgeom),7);
430  lwgeom_free(lwgeom);
431 
432  lwgeom = lwgeom_from_gserialized(g_ser1);
433 
434  CU_ASSERT_EQUAL(lwgeom_count_vertices(lwgeom),7);
435  lwgeom_free(lwgeom);
436 
437  lwfree(g_ser1);
438 
439 }
440 
441 static void test_lwcollection_extract(void)
442 {
443 
444  LWGEOM *geom;
445  LWCOLLECTION *col;
446 
447  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0))", LW_PARSER_CHECK_NONE);
448 
449  col = lwcollection_extract((LWCOLLECTION*)geom, 1);
450  CU_ASSERT_EQUAL(col->type, MULTIPOINTTYPE);
451  lwcollection_free(col);
452 
453  col = lwcollection_extract((LWCOLLECTION*)geom, 2);
454  CU_ASSERT_EQUAL(col->type, MULTILINETYPE);
455  lwcollection_free(col);
456 
457  col = lwcollection_extract((LWCOLLECTION*)geom, 3);
458  CU_ASSERT_EQUAL(col->type, MULTIPOLYGONTYPE);
459  lwcollection_free(col);
460 
461  lwgeom_free(geom);
462 
463  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION EMPTY", LW_PARSER_CHECK_NONE);
464 
465  col = lwcollection_extract((LWCOLLECTION*)geom, 1);
466  CU_ASSERT_EQUAL(col->type, MULTIPOINTTYPE);
467  lwcollection_free(col);
468 
469  col = lwcollection_extract((LWCOLLECTION*)geom, 2);
470  CU_ASSERT_EQUAL(col->type, MULTILINETYPE);
471  lwcollection_free(col);
472 
473  col = lwcollection_extract((LWCOLLECTION*)geom, 3);
474  CU_ASSERT_EQUAL(col->type, MULTIPOLYGONTYPE);
475  lwcollection_free(col);
476 
477  lwgeom_free(geom);
478 }
479 
480 static void test_lwgeom_free(void)
481 {
482  LWGEOM *geom;
483 
484  /* Empty geometries don't seem to free properly (#370) */
485  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION EMPTY", LW_PARSER_CHECK_NONE);
486  CU_ASSERT_EQUAL(geom->type, COLLECTIONTYPE);
487  lwgeom_free(geom);
488 
489  /* Empty geometries don't seem to free properly (#370) */
490  geom = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
491  CU_ASSERT_EQUAL(geom->type, POLYGONTYPE);
492  lwgeom_free(geom);
493 
494  /* Empty geometries don't seem to free properly (#370) */
495  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_NONE);
496  CU_ASSERT_EQUAL(geom->type, LINETYPE);
497  lwgeom_free(geom);
498 
499  /* Empty geometries don't seem to free properly (#370) */
500  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
501  CU_ASSERT_EQUAL(geom->type, POINTTYPE);
502  lwgeom_free(geom);
503 
504 }
505 
506 static void do_lwgeom_flip_coordinates(char *in, char *out)
507 {
508  LWGEOM *g;
509  char * t;
510  double xmax, ymax;
511  int testbox;
512 
514  lwgeom_add_bbox(g);
515 
516  testbox = (g->bbox != NULL);
517  if ( testbox )
518  {
519  xmax = g->bbox->xmax;
520  ymax = g->bbox->ymax;
521  }
522 
524 
525  if ( testbox )
526  {
527  CU_ASSERT_DOUBLE_EQUAL(g->bbox->xmax, ymax, 0.00001);
528  CU_ASSERT_DOUBLE_EQUAL(g->bbox->ymax, xmax, 0.00001);
529  }
530 
531  t = lwgeom_to_wkt(g, WKT_EXTENDED, 8, NULL);
532  if (t == NULL) fprintf(stderr, "In:%s", in);
533  if (strcmp(t, out))
534  fprintf(stderr, "\nIn: %s\nOut: %s\nTheo: %s\n", in, t, out);
535 
536  CU_ASSERT_STRING_EQUAL(t, out)
537 
538  lwgeom_free(g);
539  lwfree(t);
540 }
541 
543 {
544  /*
545  * 2D geometries types
546  */
548  "POINT(1 2)",
549  "POINT(2 1)"
550  );
551 
553  "LINESTRING(1 2,3 4)",
554  "LINESTRING(2 1,4 3)"
555  );
556 
558  "POLYGON((1 2,3 4,5 6,1 2))",
559  "POLYGON((2 1,4 3,6 5,2 1))"
560  );
561 
563  "POLYGON((1 2,3 4,5 6,1 2),(7 8,9 10,11 12,7 8))",
564  "POLYGON((2 1,4 3,6 5,2 1),(8 7,10 9,12 11,8 7))"
565  );
566 
568  "MULTIPOINT(1 2,3 4)",
569  "MULTIPOINT(2 1,4 3)"
570  );
571 
573  "MULTILINESTRING((1 2,3 4),(5 6,7 8))",
574  "MULTILINESTRING((2 1,4 3),(6 5,8 7))"
575  );
576 
578  "MULTIPOLYGON(((1 2,3 4,5 6,7 8)),((9 10,11 12,13 14,10 9)))",
579  "MULTIPOLYGON(((2 1,4 3,6 5,8 7)),((10 9,12 11,14 13,9 10)))"
580  );
581 
583  "GEOMETRYCOLLECTION EMPTY",
584  "GEOMETRYCOLLECTION EMPTY"
585  );
586 
588  "GEOMETRYCOLLECTION(POINT(1 2),LINESTRING(3 4,5 6))",
589  "GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(4 3,6 5))"
590  );
591 
593  "GEOMETRYCOLLECTION(POINT(1 2),GEOMETRYCOLLECTION(LINESTRING(3 4,5 6)))",
594  "GEOMETRYCOLLECTION(POINT(2 1),GEOMETRYCOLLECTION(LINESTRING(4 3,6 5)))"
595  );
596 
598  "CIRCULARSTRING(-2 0,0 2,2 0,0 2,2 4)",
599  "CIRCULARSTRING(0 -2,2 0,0 2,2 0,4 2)"
600  );
601 
603  "COMPOUNDCURVE(CIRCULARSTRING(0 1,1 1,1 0),(1 0,0 1))",
604  "COMPOUNDCURVE(CIRCULARSTRING(1 0,1 1,0 1),(0 1,1 0))"
605  );
606 
608  "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))",
609  "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))"
610  );
611 
613  "MULTICURVE((5 5,3 5,3 3,0 3),CIRCULARSTRING(0 0,2 1,2 3))",
614  "MULTICURVE((5 5,5 3,3 3,3 0),CIRCULARSTRING(0 0,1 2,3 2))"
615  );
616 
618  "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)))",
619  "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)))"
620  );
621 
622 
623  /*
624  * Ndims
625  */
626 
628  "POINT(1 2 3)",
629  "POINT(2 1 3)"
630  );
631 
633  "POINTM(1 2 3)",
634  "POINTM(2 1 3)"
635  );
636 
638  "POINT(1 2 3 4)",
639  "POINT(2 1 3 4)"
640  );
641 
642 
643  /*
644  * Srid
645  */
646 
648  "SRID=4326;POINT(1 2)",
649  "SRID=4326;POINT(2 1)"
650  );
651 
653  "SRID=0;POINT(1 2)",
654  "POINT(2 1)"
655  );
656 }
657 
658 static void test_f2d(void)
659 {
660  double d = 1000000.123456789123456789;
661  float f;
662  double e;
663 
664  f = next_float_down(d);
665  d = next_float_down(f);
666  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
667 
668  e = (double)f;
669  CU_ASSERT_DOUBLE_EQUAL(f,e, 0.0000001);
670 
671  f = next_float_down(d);
672  d = next_float_down(f);
673  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
674 
675  f = next_float_up(d);
676  d = next_float_up(f);
677  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
678 
679  f = next_float_up(d);
680  d = next_float_up(f);
681  CU_ASSERT_DOUBLE_EQUAL(f,d, 0.0000001);
682 }
683 
684 /*
685  * This is a test for memory leaks, can't really test
686  * w/out checking with a leak detector (ie: valgrind)
687  *
688  * See http://trac.osgeo.org/postgis/ticket/1102
689  */
690 static void test_lwgeom_clone(void)
691 {
692  int i;
693 
694  char *ewkt[] =
695  {
696  "POINT(0 0.2)",
697  "LINESTRING(-1 -1,-1 2.5,2 2,2 -1)",
698  "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)",
699  "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))",
700  "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))",
701  "POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
702  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
703  "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))",
704  "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))",
705  "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)))",
706  "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))))",
707  "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))",
708  "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)))",
709  "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)))"
710  };
711 
712 
713  for ( i = 0; i < (sizeof ewkt/sizeof(char *)); i++ )
714  {
715  LWGEOM *geom, *cloned;
716  char *in_ewkt;
717  char *out_ewkt;
718 
719  in_ewkt = ewkt[i];
720  geom = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
721  cloned = lwgeom_clone(geom);
722  out_ewkt = lwgeom_to_ewkt(cloned);
723  if (strcmp(in_ewkt, out_ewkt))
724  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
725  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
726  lwfree(out_ewkt);
727  lwgeom_free(cloned);
728  lwgeom_free(geom);
729  }
730 
731 
732 }
733 
734 /*
735  * Test lwgeom_force_clockwise
736  */
738 {
739  LWGEOM *geom;
740  LWGEOM *geom2;
741  char *in_ewkt, *out_ewkt;
742 
743  /* counterclockwise, must be reversed */
744  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
746  in_ewkt = "POLYGON((0 0,0 10,10 10,10 0,0 0))";
747  out_ewkt = lwgeom_to_ewkt(geom);
748  if (strcmp(in_ewkt, out_ewkt))
749  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
750  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
751  lwfree(out_ewkt);
752  lwgeom_free(geom);
753 
754  /* clockwise, fine as is */
755  geom = lwgeom_from_wkt("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))", LW_PARSER_CHECK_NONE);
757  in_ewkt = "POLYGON((0 0,0 10,10 10,10 0,0 0))";
758  out_ewkt = lwgeom_to_ewkt(geom);
759  if (strcmp(in_ewkt, out_ewkt))
760  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
761  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
762  lwfree(out_ewkt);
763  lwgeom_free(geom);
764 
765  /* counterclockwise shell (must be reversed), mixed-wise holes */
766  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);
768  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))";
769  out_ewkt = lwgeom_to_ewkt(geom);
770  if (strcmp(in_ewkt, out_ewkt))
771  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
772  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
773  lwfree(out_ewkt);
774  lwgeom_free(geom);
775 
776  /* clockwise shell (fine), mixed-wise holes */
777  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);
779  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))";
780  out_ewkt = lwgeom_to_ewkt(geom);
781  if (strcmp(in_ewkt, out_ewkt))
782  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
783  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
784  lwfree(out_ewkt);
785  lwgeom_free(geom);
786 
787  /* clockwise narrow ring, fine as-is */
788  /* NOTE: this is a narrow ring, see ticket #1302 */
789  in_ewkt = "0103000000010000000500000000917E9BA468294100917E9B8AEA2841C976BE1FA4682941C976BE9F8AEA2841B39ABE1FA46829415ACCC29F8AEA284137894120A4682941C976BE9F8AEA284100917E9BA468294100917E9B8AEA2841";
790  geom = lwgeom_from_hexwkb(in_ewkt, LW_PARSER_CHECK_NONE);
791  geom2 = lwgeom_from_hexwkb(in_ewkt, LW_PARSER_CHECK_NONE);
792  lwgeom_force_clockwise(geom2);
793 
796  CU_ASSERT( lwgeom_same(geom, geom2) );
797  lwgeom_free(geom);
798  lwgeom_free(geom2);
799 }
800 
801 /*
802  * Test lwgeom_is_empty
803  */
804 static void test_lwgeom_is_empty(void)
805 {
806  LWGEOM *geom;
807 
808  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
809  CU_ASSERT( !lwgeom_is_empty(geom) );
810  lwgeom_free(geom);
811 
812  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
813  CU_ASSERT( lwgeom_is_empty(geom) );
814  lwgeom_free(geom);
815 
816  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_NONE);
817  CU_ASSERT( lwgeom_is_empty(geom) );
818  lwgeom_free(geom);
819 
820  geom = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
821  CU_ASSERT( lwgeom_is_empty(geom) );
822  lwgeom_free(geom);
823 
824  geom = lwgeom_from_wkt("MULTIPOINT EMPTY", LW_PARSER_CHECK_NONE);
825  CU_ASSERT( lwgeom_is_empty(geom) );
826  lwgeom_free(geom);
827 
828  geom = lwgeom_from_wkt("MULTILINESTRING EMPTY", LW_PARSER_CHECK_NONE);
829  CU_ASSERT( lwgeom_is_empty(geom) );
830  lwgeom_free(geom);
831 
832  geom = lwgeom_from_wkt("MULTIPOLYGON EMPTY", LW_PARSER_CHECK_NONE);
833  CU_ASSERT( lwgeom_is_empty(geom) );
834  lwgeom_free(geom);
835 
836  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);
837  CU_ASSERT( lwgeom_is_empty(geom) );
838  lwgeom_free(geom);
839 
840 }
841 
842 /*
843  * Test lwgeom_same
844  */
845 static void test_lwgeom_same(void)
846 {
847  LWGEOM *geom, *geom2;
848 
849  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
850  CU_ASSERT( lwgeom_same(geom, geom) );
851  lwgeom_add_bbox(geom);
852  CU_ASSERT( lwgeom_same(geom, geom) );
853  lwgeom_free(geom);
854 
855  geom = lwgeom_from_wkt("MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))", LW_PARSER_CHECK_NONE);
856  CU_ASSERT( lwgeom_same(geom, geom) );
857  lwgeom_add_bbox(geom);
858  CU_ASSERT( lwgeom_same(geom, geom) );
859  lwgeom_free(geom);
860 
861  geom = lwgeom_from_wkt("LINESTRING(0 0, 2 0)", LW_PARSER_CHECK_NONE);
862  CU_ASSERT( lwgeom_same(geom, geom) );
863  lwgeom_add_bbox(geom);
864  CU_ASSERT( lwgeom_same(geom, geom) );
865  lwgeom_free(geom);
866 
867  geom = lwgeom_from_wkt("MULTILINESTRING((0 0, 2 0))", LW_PARSER_CHECK_NONE);
868  CU_ASSERT( lwgeom_same(geom, geom) );
869  lwgeom_add_bbox(geom);
870  CU_ASSERT( lwgeom_same(geom, geom) );
871  lwgeom_free(geom);
872 
873  geom = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
874  CU_ASSERT( lwgeom_same(geom, geom) );
875  lwgeom_add_bbox(geom);
876  CU_ASSERT( lwgeom_same(geom, geom) );
877  lwgeom_free(geom);
878 
879  geom = lwgeom_from_wkt("MULTIPOINT((0 0),(4 5))", LW_PARSER_CHECK_NONE);
880  CU_ASSERT( lwgeom_same(geom, geom) );
881  lwgeom_add_bbox(geom);
882  CU_ASSERT( lwgeom_same(geom, geom) );
883  lwgeom_free(geom);
884 
885  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
886  CU_ASSERT( lwgeom_same(geom, geom) );
887  lwgeom_add_bbox(geom);
888  CU_ASSERT( lwgeom_same(geom, geom) );
889  lwgeom_free(geom);
890 
891  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_NONE);
892  CU_ASSERT( lwgeom_same(geom, geom) );
893  lwgeom_add_bbox(geom);
894  CU_ASSERT( lwgeom_same(geom, geom) );
895  lwgeom_free(geom);
896 
897  geom = lwgeom_from_wkt("POLYGON EMPTY", LW_PARSER_CHECK_NONE);
898  CU_ASSERT( lwgeom_same(geom, geom) );
899  lwgeom_free(geom);
900 
901  geom = lwgeom_from_wkt("MULTIPOINT EMPTY", LW_PARSER_CHECK_NONE);
902  CU_ASSERT( lwgeom_same(geom, geom) );
903  lwgeom_free(geom);
904 
905  geom = lwgeom_from_wkt("MULTILINESTRING EMPTY", LW_PARSER_CHECK_NONE);
906  CU_ASSERT( lwgeom_same(geom, geom) );
907  lwgeom_free(geom);
908 
909  geom = lwgeom_from_wkt("MULTIPOLYGON EMPTY", LW_PARSER_CHECK_NONE);
910  CU_ASSERT( lwgeom_same(geom, geom) );
911  lwgeom_free(geom);
912 
913  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);
914  CU_ASSERT( lwgeom_same(geom, geom) );
915  lwgeom_free(geom);
916 
917  geom = lwgeom_from_wkt("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", LW_PARSER_CHECK_NONE);
918  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);
919  CU_ASSERT( ! lwgeom_same(geom, geom2) );
920  lwgeom_free(geom);
921  lwgeom_free(geom2);
922 
923  geom = lwgeom_from_wkt("POINT(0 0)", LW_PARSER_CHECK_NONE);
924  geom2 = lwgeom_from_wkt("MULTIPOINT((0 0))", LW_PARSER_CHECK_NONE);
925  CU_ASSERT( ! lwgeom_same(geom, geom2) );
926  lwgeom_free(geom);
927  lwgeom_free(geom2);
928 
929  geom = lwgeom_from_wkt("POINT EMPTY", LW_PARSER_CHECK_NONE);
930  geom2 = lwgeom_from_wkt("POINT Z EMPTY", LW_PARSER_CHECK_NONE);
931  CU_ASSERT( ! lwgeom_same(geom, geom2) );
932  lwgeom_free(geom);
933  lwgeom_free(geom2);
934 
935 }
936 
937 static void test_lwline_from_lwmpoint(void)
938 {
939  LWLINE *line;
940  LWMPOINT *mpoint;
941 
942 // LWLINE *
943 // lwline_from_lwmpoint(int srid, LWMPOINT *mpoint)
944 
945  mpoint = (LWMPOINT*)lwgeom_from_wkt("MULTIPOINT(0 0, 0 1, 1 1, 1 2, 2 2)", LW_PARSER_CHECK_NONE);
946  line = lwline_from_lwmpoint(SRID_DEFAULT, mpoint);
947  CU_ASSERT_EQUAL(line->points->npoints, mpoint->ngeoms);
948  CU_ASSERT_DOUBLE_EQUAL(lwline_length_2d(line), 4.0, 0.000001);
949 
950  lwline_free(line);
951  lwmpoint_free(mpoint);
952 }
953 
954 /*
955 ** Used by test harness to register the tests in this file.
956 */
957 void libgeom_suite_setup(void);
959 {
960  CU_pSuite suite = CU_add_suite("Serialization/Deserialization", NULL, NULL);
973  PG_ADD_TEST(suite, test_f2d);
981 }
#define LINETYPE
Definition: liblwgeom.h:61
#define TYPMOD_GET_TYPE(typmod)
Definition: liblwgeom.h:133
static void test_gserialized_from_lwgeom_size(void)
Definition: cu_libgeom.c:149
GBOX * bbox
Definition: liblwgeom.h:354
static void test_on_gser_lwgeom_count_vertices(void)
Definition: cu_libgeom.c:417
#define TYPMOD_GET_M(typmod)
Definition: liblwgeom.h:137
LWCOLLECTION * lwcollection_extract(LWCOLLECTION *col, int type)
Takes a potentially heterogeneous collection and returns a homogeneous collection consisting only of ...
Definition: lwcollection.c:343
#define TYPMOD_SET_Z(typmod)
Definition: liblwgeom.h:136
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:21
LWLINE * lwline_from_lwmpoint(int srid, LWMPOINT *mpoint)
Definition: lwline.c:260
void lwfree(void *mem)
Definition: lwutil.c:190
#define FLAGS_GET_READONLY(flags)
Definition: liblwgeom.h:110
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int npoints
Definition: liblwgeom.h:327
uint8_t type
Definition: liblwgeom.h:459
static void test_gserialized_is_empty(void)
Definition: cu_libgeom.c:311
#define FLAGS_GET_GEODETIC(flags)
Definition: liblwgeom.h:109
#define POLYGONTYPE
Definition: liblwgeom.h:62
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:425
#define TYPMOD_GET_SRID(typmod)
Macros for manipulating the 'typemod' int.
Definition: liblwgeom.h:131
double xmax
Definition: liblwgeom.h:249
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
#define MULTIPOINTTYPE
Definition: liblwgeom.h:63
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:149
#define LW_SUCCESS
Definition: liblwgeom.h:55
void lwline_free(LWLINE *line)
Definition: lwline.c:63
static void test_geometry_type_from_string(void)
Definition: cu_libgeom.c:353
#define SRID_DEFAULT
Definition: liblwgeom.h:161
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:119
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:87
double lwline_length_2d(const LWLINE *line)
Definition: lwline.c:503
static void test_lwgeom_force_clockwise(void)
Definition: cu_libgeom.c:737
float next_float_down(double d)
Definition: lwgeom_api.c:129
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:844
static void test_lwcollection_extract(void)
Definition: cu_libgeom.c:441
#define FLAGS_SET_GEODETIC(flags, value)
Definition: liblwgeom.h:115
static void test_gbox_serialized_size(void)
Definition: cu_libgeom.c:227
static void test_flags_macros(void)
Definition: cu_libgeom.c:66
LWGEOM * lwgeom_flip_coordinates(LWGEOM *in)
Reverse the X and Y coordinate order.
Definition: lwgeom.c:1390
static void test_lwgeom_free(void)
Definition: cu_libgeom.c:480
int gserialized_is_empty(const GSERIALIZED *g)
Check if a GSERIALIZED is empty without deserializing first.
Definition: g_serialized.c:140
#define LW_FAILURE
Definition: liblwgeom.h:54
float next_float_up(double d)
Definition: lwgeom_api.c:145
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1706
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:112
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:641
static void do_lwgeom_flip_coordinates(char *in, char *out)
Definition: cu_libgeom.c:506
void lwmpoint_free(LWMPOINT *mpt)
Definition: lwmpoint.c:59
static void test_serialized_srid(void)
Definition: cu_libgeom.c:117
void libgeom_suite_setup(void)
Definition: cu_libgeom.c:958
double xmin
Definition: liblwgeom.h:248
static void test_f2d(void)
Definition: cu_libgeom.c:658
#define FLAGS_GET_BBOX(flags)
Definition: liblwgeom.h:108
static void test_lwgeom_calculate_gbox(void)
Definition: cu_libgeom.c:191
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:154
#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:908
static void test_lwgeom_is_empty(void)
Definition: cu_libgeom.c:804
double ymax
Definition: liblwgeom.h:251
char * s
Definition: cu_in_wkt.c:24
#define TYPMOD_SET_TYPE(typmod, type)
Definition: liblwgeom.h:134
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:106
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:351
char lwgeom_same(const LWGEOM *lwgeom1, const LWGEOM *lwgeom2)
geom1 same as geom2 iff
Definition: lwgeom.c:451
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:131
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:753
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:65
#define WKT_EXTENDED
Definition: liblwgeom.h:1778
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:114
static void test_lwgeom_flip_coordinates(void)
Definition: cu_libgeom.c:542
static void test_lwgeom_clone(void)
Definition: cu_libgeom.c:690
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:541
static void test_lwgeom_same(void)
Definition: cu_libgeom.c:845
static void test_lwline_from_lwmpoint(void)
Definition: cu_libgeom.c:937
static void test_lwgeom_from_gserialized(void)
Definition: cu_libgeom.c:245
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:555
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:107
uint8_t type
Definition: liblwgeom.h:352
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:316
#define TYPMOD_SET_SRID(typmod, srid)
Definition: liblwgeom.h:132
#define TYPMOD_GET_Z(typmod)
Definition: liblwgeom.h:135
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:1229
int lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1072
#define MULTILINETYPE
Definition: liblwgeom.h:64
int ngeoms
Definition: liblwgeom.h:424
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: g_box.c:139
static void test_lwgeom_count_vertices(void)
Definition: cu_libgeom.c:399
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:70
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:375
#define COLLECTIONTYPE
Definition: liblwgeom.h:66
#define FLAGS_SET_READONLY(flags, value)
Definition: liblwgeom.h:116
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:113
POINTARRAY * points
Definition: liblwgeom.h:378
int lwgeom_calculate_gbox_cartesian(const LWGEOM *lwgeom, GBOX *gbox)
Calculate the 2-4D bounding box of a geometry.
Definition: g_box.c:607