PostGIS  2.5.7dev-r@@SVN_REVISION@@
cu_wrapx.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright (C) 2016 Sandro Santilli <strk@kbt.io>
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 "CUnit/Basic.h"
14 #include "cu_tester.h"
15 
16 #include "liblwgeom.h"
17 #include "liblwgeom_internal.h"
18 #include "../lwgeom_geos.h"
19 
20 static void test_lwgeom_wrapx(void)
21 {
22  LWGEOM *geom, *ret, *tmp, *tmp2;
23  char *exp_wkt, *obt_wkt;
24 
25  geom = lwgeom_from_wkt(
26  "POLYGON EMPTY",
28  CU_ASSERT_FATAL(geom != NULL);
29  ret = lwgeom_wrapx(geom, 0, 20);
30  CU_ASSERT_FATAL(ret != NULL);
31  obt_wkt = lwgeom_to_ewkt(ret);
32  exp_wkt = "POLYGON EMPTY";
33  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
34  lwfree(obt_wkt);
35  lwgeom_free(ret);
36  lwgeom_free(geom);
37 
38  geom = lwgeom_from_wkt(
39  "POINT(0 0)",
41  CU_ASSERT_FATAL(geom != NULL);
42  ret = lwgeom_wrapx(geom, 2, 10);
43  CU_ASSERT_FATAL(ret != NULL);
44  obt_wkt = lwgeom_to_ewkt(ret);
45  exp_wkt = "POINT(10 0)";
46  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
47  lwfree(obt_wkt);
48  lwgeom_free(ret);
49  lwgeom_free(geom);
50 
51  geom = lwgeom_from_wkt(
52  "POINT(0 0)",
54  CU_ASSERT_FATAL(geom != NULL);
55  ret = lwgeom_wrapx(geom, 0, 20);
56  CU_ASSERT_FATAL(ret != NULL);
57  obt_wkt = lwgeom_to_ewkt(ret);
58  exp_wkt = "POINT(0 0)";
59  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
60  lwfree(obt_wkt);
61  lwgeom_free(ret);
62  lwgeom_free(geom);
63 
64  geom = lwgeom_from_wkt(
65  "POINT(0 0)",
67  CU_ASSERT_FATAL(geom != NULL);
68  ret = lwgeom_wrapx(geom, 0, -20);
69  CU_ASSERT_FATAL(ret != NULL);
70  obt_wkt = lwgeom_to_ewkt(ret);
71  exp_wkt = "POINT(0 0)";
72  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
73  lwfree(obt_wkt);
74  lwgeom_free(ret);
75  lwgeom_free(geom);
76 
77  geom = lwgeom_from_wkt(
78  "LINESTRING(0 0,10 0)",
80  CU_ASSERT_FATAL(geom != NULL);
81  tmp = lwgeom_wrapx(geom, 8, -10);
82  ret = lwgeom_normalize(tmp);
83  lwgeom_free(tmp);
84  CU_ASSERT_FATAL(ret != NULL);
85  obt_wkt = lwgeom_to_ewkt(ret);
86  tmp = lwgeom_from_wkt(
87  "MULTILINESTRING((0 0,8 0),(-2 0,0 0))",
89  tmp2 = lwgeom_normalize(tmp);
90  lwgeom_free(tmp);
91  exp_wkt = lwgeom_to_ewkt(tmp2);
92  lwgeom_free(tmp2);
93  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
94  lwfree(obt_wkt);
95  lwfree(exp_wkt);
96  lwgeom_free(ret);
97  lwgeom_free(geom);
98 
99  geom = lwgeom_from_wkt(
100  "MULTILINESTRING((-5 -2,0 0),(0 0,10 10))",
102  CU_ASSERT_FATAL(geom != NULL);
103  tmp = lwgeom_wrapx(geom, 0, 20);
104  ret = lwgeom_normalize(tmp);
105  lwgeom_free(tmp);
106  CU_ASSERT_FATAL(ret != NULL);
107  obt_wkt = lwgeom_to_ewkt(ret);
108  tmp = lwgeom_from_wkt("MULTILINESTRING((15 -2,20 0),(0 0,10 10))", LW_PARSER_CHECK_NONE);
109  tmp2 = lwgeom_normalize(tmp);
110  lwgeom_free(tmp);
111  exp_wkt = lwgeom_to_ewkt(tmp2);
112  lwgeom_free(tmp2);
113  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
114  lwfree(obt_wkt);
115  lwfree(exp_wkt);
116  lwgeom_free(ret);
117  lwgeom_free(geom);
118 
119  geom = lwgeom_from_wkt(
120  "MULTIPOLYGON("
121  " ((0 0,10 0,10 10,0 10,0 0),(2 2,4 2,4 4,2 4,2 2)),"
122  " ((0 11,10 11,10 21,0 21,0 11),(2 13,4 13,4 15,2 15,2 13))"
123  ")",
125  CU_ASSERT_FATAL(geom != NULL);
126  tmp = lwgeom_wrapx(geom, 2, 20);
127  ret = lwgeom_normalize(tmp);
128  lwgeom_free(tmp);
129  CU_ASSERT_FATAL(ret != NULL);
130  obt_wkt = lwgeom_to_ewkt(ret);
131  tmp = lwgeom_from_wkt("GEOMETRYCOLLECTION("
132  "MULTIPOLYGON("
133  "((22 0,20 0,20 10,22 10,22 4,22 2,22 0)),"
134  "((2 10,10 10,10 0,2 0,2 2,4 2,4 4,2 4,2 10))"
135  "),"
136  "MULTIPOLYGON("
137  "((22 11,20 11,20 21,22 21,22 15,22 13,22 11)),"
138  "((2 21,10 21,10 11,2 11,2 13,4 13,4 15,2 15,2 21))"
139  ")"
140  ")",
142  tmp2 = lwgeom_normalize(tmp);
143  lwgeom_free(tmp);
144  exp_wkt = lwgeom_to_ewkt(tmp2);
145  lwgeom_free(tmp2);
146  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
147  lwfree(obt_wkt);
148  lwfree(exp_wkt);
149  lwgeom_free(ret);
150  lwgeom_free(geom);
151 
152  geom = lwgeom_from_wkt(
153  "GEOMETRYCOLLECTION("
154  " MULTILINESTRING((-5 -2,0 0),(0 0,10 10)),"
155  " POINT(-5 0),"
156  " POLYGON EMPTY"
157  ")",
159  CU_ASSERT_FATAL(geom != NULL);
160  tmp = lwgeom_wrapx(geom, 0, 20);
161  ret = lwgeom_normalize(tmp);
162  lwgeom_free(tmp);
163  CU_ASSERT_FATAL(ret != NULL);
164  obt_wkt = lwgeom_to_ewkt(ret);
165  tmp = lwgeom_from_wkt(
166  "GEOMETRYCOLLECTION("
167  "MULTILINESTRING((15 -2,20 0),(0 0,10 10)),"
168  "POINT(15 0),"
169  "POLYGON EMPTY"
170  ")",
172  tmp2 = lwgeom_normalize(tmp);
173  lwgeom_free(tmp);
174  exp_wkt = lwgeom_to_ewkt(tmp2);
175  lwgeom_free(tmp2);
176  ASSERT_STRING_EQUAL(obt_wkt, exp_wkt);
177  lwfree(obt_wkt);
178  lwfree(exp_wkt);
179  lwgeom_free(ret);
180  lwgeom_free(geom);
181 }
182 
183 static int
185 {
186  finishGEOS();
187  return 0;
188 }
189 
190 /*
191 ** Used by test harness to register the tests in this file.
192 */
193 void wrapx_suite_setup(void);
195 {
196  CU_pSuite suite = CU_add_suite("wrapx", NULL, clean_geos_wrapx_suite);
198 }
void wrapx_suite_setup(void)
Definition: cu_wrapx.c:194
static int clean_geos_wrapx_suite(void)
Definition: cu_wrapx.c:184
static void test_lwgeom_wrapx(void)
Definition: cu_wrapx.c:20
#define PG_ADD_TEST(suite, testfunc)
#define ASSERT_STRING_EQUAL(o, e)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
LWGEOM * lwgeom_wrapx(const LWGEOM *lwgeom, double cutx, double amount)
wrap geometry on given cut x value
Definition: lwgeom_wrapx.c:169
This library is the generic geometry handling section of PostGIS.