PostGIS  3.0.6dev-r@@SVN_REVISION@@
cu_geos.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright 2011 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 <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include "CUnit/Basic.h"
17 
18 #include "lwgeom_geos.h"
19 #include "cu_tester.h"
20 
21 #include "liblwgeom_internal.h"
22 
23 static void
25 {
26  size_t i;
27  char *in_ewkt;
28  char *out_ewkt;
29  LWGEOM *geom_in;
30  LWGEOM *geom_out;
31 
32  char *ewkt[] = {
33  "POINT(0 0.2)",
34  "LINESTRING(-1 -1,-1 2.5,2 2,2 -1)",
35  "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)",
36  "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))",
37  "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))",
38  "POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
39  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
40  "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))",
41  "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))",
42  "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)))",
43  "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))))",
44  };
45 
46  for (i = 0; i < (sizeof ewkt / sizeof(char *)); i++)
47  {
48  in_ewkt = ewkt[i];
49  geom_in = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
50  geom_out = lwgeom_geos_noop(geom_in);
51  CU_ASSERT_PTR_NOT_NULL_FATAL(geom_out);
52  out_ewkt = lwgeom_to_ewkt(geom_out);
53  ASSERT_STRING_EQUAL(out_ewkt, in_ewkt);
54  lwfree(out_ewkt);
55  lwgeom_free(geom_out);
56  lwgeom_free(geom_in);
57  }
58 
59  /* TINs become collections of Polygons */
60  in_ewkt = "TIN(((0 0, 1 1, 2 2, 0 0)), ((0 0, 1 1, 2 2, 0 0)))";
61  geom_in = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
62  geom_out = lwgeom_geos_noop(geom_in);
63  out_ewkt = lwgeom_to_ewkt(geom_out);
64  ASSERT_STRING_EQUAL(out_ewkt, "GEOMETRYCOLLECTION(POLYGON((0 0,1 1,2 2,0 0)),POLYGON((0 0,1 1,2 2,0 0)))");
65  lwfree(out_ewkt);
66  lwgeom_free(geom_in);
67  lwgeom_free(geom_out);
68 
69  /* Empties disappear */
70  in_ewkt = "GEOMETRYCOLLECTION( LINESTRING (1 1, 2 2), POINT EMPTY, TRIANGLE ((0 0, 1 0, 1 1, 0 0)) )";
71  geom_in = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
72  geom_out = lwgeom_geos_noop(geom_in);
73  out_ewkt = lwgeom_to_ewkt(geom_out);
74  ASSERT_STRING_EQUAL(out_ewkt, "GEOMETRYCOLLECTION(LINESTRING(1 1,2 2),POLYGON((0 0,1 0,1 1,0 0)))");
75  lwfree(out_ewkt);
76  lwgeom_free(geom_in);
77  lwgeom_free(geom_out);
78 }
79 
80 static void test_geos_linemerge(void)
81 {
82  char *ewkt;
83  char *out_ewkt;
84  LWGEOM *geom1;
85  LWGEOM *geom2;
86 
87  ewkt = "MULTILINESTRING((0 0, 0 100),(0 -5, 0 0))";
89  geom2 = lwgeom_linemerge(geom1);
90  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
91  ASSERT_STRING_EQUAL(out_ewkt, "LINESTRING(0 -5,0 0,0 100)");
92  lwfree(out_ewkt);
93  lwgeom_free(geom1);
94  lwgeom_free(geom2);
95 
96  ewkt = "MULTILINESTRING EMPTY";
98  geom2 = lwgeom_linemerge(geom1);
99  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
100  ASSERT_STRING_EQUAL(out_ewkt, "MULTILINESTRING EMPTY");
101  lwfree(out_ewkt);
102  lwgeom_free(geom1);
103  lwgeom_free(geom2);
104 }
105 
106 static void
108 {
109  char* ewkt;
110  char* out_ewkt;
111  LWGEOM* geom1;
112  LWGEOM* geom2;
113 
114  ewkt = "MULTILINESTRING((-10 0, -10 100), (0 -5, 0 0))";
115  geom1 = lwgeom_from_wkt(ewkt, LW_PARSER_CHECK_NONE);
116  geom2 = lwgeom_offsetcurve(geom1, 2, 10, 1, 1);
117  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
118  ASSERT_STRING_EQUAL(out_ewkt, "MULTILINESTRING((-12 0,-12 100),(-2 -5,-2 0))");
119  lwfree(out_ewkt);
120  lwgeom_free(geom1);
121  lwgeom_free(geom2);
122 }
123 
124 static void
126 {
127  // Test for Trac #4143. The specific output or lack of output is not important,
128  // we're just testing that we don't crash.
129  LWGEOM* in = lwgeom_from_wkt("LINESTRING(362194.505 5649993.044,362197.451 5649994.125,362194.624 5650001.876,362189.684 5650000.114,362192.542 5649992.324,362194.505 5649993.044)", LW_PARSER_CHECK_NONE);
130  LWGEOM* out = lwgeom_offsetcurve(in, -0.045, 8, 2, 5.0);
131 
132  lwgeom_free(in);
133  if (out) {
134  lwgeom_free(out);
135  }
136 }
137 
138 static void
140 {
141  uint8_t* wkb;
142  char* out_ewkt;
143  LWGEOM* geom1;
144  LWGEOM* geom2;
145  LWGEOM* geom3;
146 
147  wkb = (uint8_t*) "\001\003\000\000\000\001\000\000\000\011\000\000\000b\020X9 }\366@7\211A\340\235I\034A\316\326t18}\366@\306g\347\323\230I\034Ay\351&18}\366@\331\316\367\323\230I\034A\372~j\274\370}\366@\315\314\314LpI\034A\343\245\233\304R}\366@R\270\036\005?I\034A\315\314\314\314Z~\366@\343\245\233\304\007I\034A\004V\016-\242}\366@\252\361\322M\323H\034A\351&1\010\306{\366@H\341z\0247I\034Ab\020X9 }\366@7\211A\340\235I\034A";
148  geom1 = lwgeom_from_wkb(wkb, 157, LW_PARSER_CHECK_NONE);
149  geom2 = lwgeom_make_valid(geom1);
150  geom3 = lwgeom_normalize(geom2); //so GEOS 3.9 and 3.10 agree
151  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
152 
153 #if POSTGIS_GEOS_VERSION < 39
155  out_ewkt,
156  "GEOMETRYCOLLECTION(POLYGON((92092.377 463437.77,92114.014 463463.469,92115.5120743 463462.206937,92115.512 463462.207,92127.546 463452.075,92117.173 463439.755,92133.675 463425.942,92122.136 463412.826,92092.377 463437.77)),MULTIPOINT(92122.136 463412.826,92115.5120743 463462.206937))");
157 #else
159  out_ewkt,
160  "POLYGON((92092.377 463437.77,92114.014 463463.469,92115.512 463462.207,92115.5120743 463462.206937,92127.546 463452.075,92117.173 463439.755,92133.675 463425.942,92122.136 463412.826,92092.377 463437.77))");
161 #endif
162  lwfree(out_ewkt);
163  lwgeom_free(geom1);
164  lwgeom_free(geom2);
165  lwgeom_free(geom3);
166 }
167 
168 
169 static void test_geos_subdivide(void)
170 {
171  char *ewkt = "LINESTRING(0 0, 10 10)";
172  char *out_ewkt;
174  /* segmentize as geography to generate a non-simple curve */
175  LWGEOM *geom2 = lwgeom_segmentize_sphere(geom1, 0.002);
176 
177  LWCOLLECTION *geom3 = lwgeom_subdivide(geom2, 80);
178  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
179  // printf("\n--------\n%s\n--------\n", out_ewkt);
180  CU_ASSERT_EQUAL(2, geom3->ngeoms);
181  lwfree(out_ewkt);
182  lwcollection_free(geom3);
183 
184  geom3 = lwgeom_subdivide(geom2, 20);
185  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
186  // printf("\n--------\n%s\n--------\n", out_ewkt);
187  CU_ASSERT_EQUAL(8, geom3->ngeoms);
188  lwfree(out_ewkt);
189  lwcollection_free(geom3);
190 
191  lwgeom_free(geom2);
192  lwgeom_free(geom1);
193 }
194 
195 /*
196 ** Used by test harness to register the tests in this file.
197 */
198 void geos_suite_setup(void);
200 {
201  CU_pSuite suite = CU_add_suite("geos", NULL, NULL);
202  PG_ADD_TEST(suite, test_geos_noop);
208 }
static void test_geos_linemerge(void)
Definition: cu_geos.c:80
static void test_geos_subdivide(void)
Definition: cu_geos.c:169
static void test_geos_offsetcurve(void)
Definition: cu_geos.c:107
static void test_geos_makevalid(void)
Definition: cu_geos.c:139
void geos_suite_setup(void)
Definition: cu_geos.c:199
static void test_geos_noop(void)
Definition: cu_geos.c:24
static void test_geos_offsetcurve_crash(void)
Definition: cu_geos.c:125
#define PG_ADD_TEST(suite, testfunc)
#define ASSERT_STRING_EQUAL(o, e)
LWGEOM * lwgeom_geos_noop(const LWGEOM *geom)
Convert an LWGEOM to a GEOS Geometry and convert back – for debug only.
LWGEOM * lwgeom_segmentize_sphere(const LWGEOM *lwg_in, double max_seg_length)
Derive a new geometry with vertices added to ensure no vertex is more than max_seg_length (in radians...
Definition: lwgeodetic.c:1743
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2060
LWGEOM * lwgeom_offsetcurve(const LWGEOM *geom, double size, int quadsegs, int joinStyle, double mitreLimit)
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:547
LWCOLLECTION * lwgeom_subdivide(const LWGEOM *geom, uint32_t maxvertices)
Definition: lwgeom.c:2439
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:357
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:905
LWGEOM * lwgeom_linemerge(const LWGEOM *geom1)
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
Definition: lwin_wkb.c:825
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
uint32_t ngeoms
Definition: liblwgeom.h:566