PostGIS  2.4.9dev-r@@SVN_REVISION@@
liblwgeom/cunit/cu_misc.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2008 Paul Ramsey
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 static void test_misc_force_2d(void)
22 {
23  LWGEOM *geom;
24  LWGEOM *geom2d;
25  char *wkt_out;
26 
27  geom = lwgeom_from_wkt("CIRCULARSTRINGM(-5 0 4,0 5 3,5 0 2,10 -5 1,15 0 0)", LW_PARSER_CHECK_NONE);
28  geom2d = lwgeom_force_2d(geom);
29  wkt_out = lwgeom_to_ewkt(geom2d);
30  CU_ASSERT_STRING_EQUAL("CIRCULARSTRING(-5 0,0 5,5 0,10 -5,15 0)",wkt_out);
31  lwgeom_free(geom);
32  lwgeom_free(geom2d);
33  lwfree(wkt_out);
34 
35  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0 0),LINESTRING(1 1 1,2 2 2),POLYGON((0 0 1,0 1 1,1 1 1,1 0 1,0 0 1)),CURVEPOLYGON(CIRCULARSTRING(0 0 0,1 1 1,2 2 2,1 1 1,0 0 0)))", LW_PARSER_CHECK_NONE);
36  geom2d = lwgeom_force_2d(geom);
37  wkt_out = lwgeom_to_ewkt(geom2d);
38  CU_ASSERT_STRING_EQUAL("GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(1 1,2 2),POLYGON((0 0,0 1,1 1,1 0,0 0)),CURVEPOLYGON(CIRCULARSTRING(0 0,1 1,2 2,1 1,0 0)))",wkt_out);
39  lwgeom_free(geom);
40  lwgeom_free(geom2d);
41  lwfree(wkt_out);
42 }
43 
44 static void test_misc_simplify(void)
45 {
46  LWGEOM *geom;
47  LWGEOM *geom2d;
48  char *wkt_out;
49 
50  geom = lwgeom_from_wkt("LINESTRING(0 0,0 10,0 51,50 20,30 20,7 32)", LW_PARSER_CHECK_NONE);
51  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
52  wkt_out = lwgeom_to_ewkt(geom2d);
53  CU_ASSERT_STRING_EQUAL("LINESTRING(0 0,0 51,50 20,30 20,7 32)",wkt_out);
54  lwgeom_free(geom);
55  lwgeom_free(geom2d);
56  lwfree(wkt_out);
57 
58  geom = lwgeom_from_wkt("MULTILINESTRING((0 0,0 10,0 51,50 20,30 20,7 32))", LW_PARSER_CHECK_NONE);
59  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
60  wkt_out = lwgeom_to_ewkt(geom2d);
61  CU_ASSERT_STRING_EQUAL("MULTILINESTRING((0 0,0 51,50 20,30 20,7 32))",wkt_out);
62  lwgeom_free(geom);
63  lwgeom_free(geom2d);
64  lwfree(wkt_out);
65 }
66 
67 static void test_misc_count_vertices(void)
68 {
69  LWGEOM *geom;
70  int count;
71 
72  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 0,1 1),POLYGON((0 0,0 1,1 0,0 0)),CIRCULARSTRING(0 0,0 1,1 1),CURVEPOLYGON(CIRCULARSTRING(0 0,0 1,1 1)))", LW_PARSER_CHECK_NONE);
73  count = lwgeom_count_vertices(geom);
74  CU_ASSERT_EQUAL(count,13);
75  lwgeom_free(geom);
76 
77  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(CIRCULARSTRING(0 0,0 1,1 1),POINT(0 0),CURVEPOLYGON(CIRCULARSTRING(0 0,0 1,1 1,1 0,0 0)))", LW_PARSER_CHECK_NONE);
78  count = lwgeom_count_vertices(geom);
79  CU_ASSERT_EQUAL(count,9);
80  lwgeom_free(geom);
81 
82  geom = lwgeom_from_wkt("CURVEPOLYGON((0 0,1 0,0 1,0 0),CIRCULARSTRING(0 0,1 0,1 1,1 0,0 0))", LW_PARSER_CHECK_NONE);
83  count = lwgeom_count_vertices(geom);
84  CU_ASSERT_EQUAL(count,9);
85  lwgeom_free(geom);
86 
87 
88  geom = lwgeom_from_wkt("POLYGON((0 0,1 0,0 1,0 0))", LW_PARSER_CHECK_NONE);
89  count = lwgeom_count_vertices(geom);
90  CU_ASSERT_EQUAL(count,4);
91  lwgeom_free(geom);
92 
93  geom = lwgeom_from_wkt("CURVEPOLYGON((0 0,1 0,0 1,0 0),CIRCULARSTRING(0 0,1 0,1 1,1 0,0 0))", LW_PARSER_CHECK_NONE);
94  count = lwgeom_count_vertices(geom);
95  CU_ASSERT_EQUAL(count,9);
96  lwgeom_free(geom);
97 }
98 
99 static void test_misc_area(void)
100 {
101  LWGEOM *geom;
102  double area;
103 
104  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_ALL);
105  area = lwgeom_area(geom);
106  CU_ASSERT_DOUBLE_EQUAL(area, 0.0, 0.0001);
107  lwgeom_free(geom);
108 }
109 
110 static void test_misc_wkb(void)
111 {
112  static char *wkb = "010A0000000200000001080000000700000000000000000000C00000000000000000000000000000F0BF000000000000F0BF00000000000000000000000000000000000000000000F03F000000000000F0BF000000000000004000000000000000000000000000000000000000000000004000000000000000C00000000000000000010200000005000000000000000000F0BF00000000000000000000000000000000000000000000E03F000000000000F03F00000000000000000000000000000000000000000000F03F000000000000F0BF0000000000000000";
114  char *str = lwgeom_to_wkt(geom, WKB_ISO, 8, 0);
115  CU_ASSERT_STRING_EQUAL(str, "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))");
116  lwfree(str);
117  lwgeom_free(geom);
118 
119 }
120 
121 
122 static void test_grid(void)
123 {
124  gridspec grid;
125  static char *wkt = "MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))";
127  LWGEOM *geomgrid;
128  char *str;
129 
130  grid.ipx = grid.ipy = 0;
131  grid.xsize = grid.ysize = 20;
132 
133  geomgrid = lwgeom_grid(geom, &grid);
134  str = lwgeom_to_ewkt(geomgrid);
135  CU_ASSERT_STRING_EQUAL(str, "MULTIPOLYGON EMPTY");
136  lwfree(str);
137  lwgeom_free(geom);
138  lwgeom_free(geomgrid);
139 }
140 
141 static void test_clone(void)
142 {
143  static char *wkt = "GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0))),POINT(1 1),LINESTRING(2 3,4 5))";
145  LWGEOM *geom2;
146 
147  /* Free in "backwards" order */
148  geom2 = lwgeom_clone(geom1);
149  lwgeom_free(geom1);
150  lwgeom_free(geom2);
151 
152  /* Free in "forewards" order */
153  geom1 = lwgeom_from_wkt(wkt, LW_PARSER_CHECK_ALL);
154  geom2 = lwgeom_clone(geom1);
155  lwgeom_free(geom2);
156  lwgeom_free(geom1);
157 }
158 
159 static void test_lwmpoint_from_lwgeom(void)
160 {
161  /* This cast is so ugly, we only want to do it once. And not even that. */
162  LWGEOM* (*to_points)(LWGEOM*) = (LWGEOM* (*)(LWGEOM*)) &lwmpoint_from_lwgeom;
163 
164  do_fn_test(to_points, "MULTIPOLYGON (EMPTY)", "MULTIPOINT EMPTY");
165  do_fn_test(to_points, "POINT (30 10)", "MULTIPOINT ((30 10))");
166  do_fn_test(to_points, "LINESTRING Z (30 10 4,10 30 5,40 40 6)", "MULTIPOINT Z (30 10 4,10 30 5, 40 40 6)");
167  do_fn_test(to_points, "POLYGON((35 10,45 45,15 40,10 20,35 10),(20 30,35 35,30 20,20 30))", "MULTIPOINT(35 10,45 45,15 40,10 20,35 10,20 30,35 35,30 20,20 30)");
168  do_fn_test(to_points, "MULTIPOINT M (10 40 1,40 30 2,20 20 3,30 10 4)", "MULTIPOINT M (10 40 1,40 30 2,20 20 3,30 10 4)");
169  do_fn_test(to_points, "COMPOUNDCURVE(CIRCULARSTRING(0 0,2 0, 2 1, 2 3, 4 3),(4 3, 4 5, 1 4, 0 0))", "MULTIPOINT(0 0, 2 0, 2 1, 2 3, 4 3, 4 3, 4 5, 1 4, 0 0)");
170  do_fn_test(to_points, "TIN(((80 130,50 160,80 70,80 130)),((50 160,10 190,10 70,50 160)))", "MULTIPOINT (80 130, 50 160, 80 70, 80 130, 50 160, 10 190, 10 70, 50 160)");
171 }
172 
173 /*
174 ** Used by the test harness to register the tests in this file.
175 */
176 void misc_suite_setup(void);
178 {
179  CU_pSuite suite = CU_add_suite("miscellaneous", NULL, NULL);
183  PG_ADD_TEST(suite, test_misc_area);
184  PG_ADD_TEST(suite, test_misc_wkb);
185  PG_ADD_TEST(suite, test_grid);
186  PG_ADD_TEST(suite, test_clone);
188 }
LWGEOM * lwgeom_grid(const LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:1912
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:669
void lwfree(void *mem)
Definition: lwutil.c:244
Datum area(PG_FUNCTION_ARGS)
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:518
static void test_misc_area(void)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
LWMPOINT * lwmpoint_from_lwgeom(const LWGEOM *g)
Definition: lwmpoint.c:127
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2013
#define LW_FALSE
Definition: liblwgeom.h:77
static void test_lwmpoint_from_lwgeom(void)
LWGEOM * lwgeom_simplify(const LWGEOM *igeom, double dist, int preserve_collapsed)
Definition: lwgeom.c:1602
#define PG_ADD_TEST(suite, testfunc)
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:739
void misc_suite_setup(void)
int count
Definition: genraster.py:56
#define WKB_ISO
Definition: liblwgeom.h:2074
static void test_misc_force_2d(void)
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition: lwgeom.c:444
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:797
void do_fn_test(LWGEOM *(*transfn)(LWGEOM *), char *input_wkt, char *expected_wkt)
static void test_clone(void)
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1623
static void test_misc_simplify(void)
static void test_grid(void)
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:2014
static void test_misc_wkb(void)
int lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1189
static void test_misc_count_vertices(void)
Snap to grid.