PostGIS  2.5.7dev-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 test_geos_noop(void)
24 {
25  size_t i;
26 
27  char *ewkt[] =
28  {
29  "POINT(0 0.2)",
30  "LINESTRING(-1 -1,-1 2.5,2 2,2 -1)",
31  "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)",
32  "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))",
33  "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))",
34  "POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
35  "SRID=4326;POLYGON((-1 -1,-1 2.5,2 2,2 -1,-1 -1),(0 0,0 1,1 1,1 0,0 0))",
36  "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))",
37  "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))",
38  "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)))",
39  "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))))",
40  "GEOMETRYCOLLECTION( LINESTRING (1 1, 2 2), POINT EMPTY, TRIANGLE ((0 0, 1 0, 1 1, 0 0)) )",
41  };
42 
43 
44  for ( i = 0; i < (sizeof ewkt/sizeof(char *)); i++ )
45  {
46  LWGEOM *geom_in, *geom_out;
47  char *in_ewkt;
48  char *out_ewkt;
49 
50  in_ewkt = ewkt[i];
51  geom_in = lwgeom_from_wkt(in_ewkt, LW_PARSER_CHECK_NONE);
52  geom_out = lwgeom_geos_noop(geom_in);
53  if ( ! geom_out ) {
54  // fprintf(stderr, "\nNull return from lwgeom_geos_noop with wkt: %s\n", in_ewkt);
55  lwgeom_free(geom_in);
56  continue;
57  }
58  out_ewkt = lwgeom_to_ewkt(geom_out);
59  if (strcmp(in_ewkt, out_ewkt))
60  fprintf(stderr, "\nExp: %s\nObt: %s\n", in_ewkt, out_ewkt);
61  CU_ASSERT_STRING_EQUAL(in_ewkt, out_ewkt);
62  lwfree(out_ewkt);
63  lwgeom_free(geom_out);
64  lwgeom_free(geom_in);
65  }
66 }
67 
68 static void test_geos_linemerge(void)
69 {
70  char *ewkt;
71  char *out_ewkt;
72  LWGEOM *geom1;
73  LWGEOM *geom2;
74 
75  ewkt = "MULTILINESTRING((0 0, 0 100),(0 -5, 0 0))";
77  geom2 = lwgeom_linemerge(geom1);
78  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
79  ASSERT_STRING_EQUAL(out_ewkt, "LINESTRING(0 -5,0 0,0 100)");
80  lwfree(out_ewkt);
81  lwgeom_free(geom1);
82  lwgeom_free(geom2);
83 
84  ewkt = "MULTILINESTRING EMPTY";
86  geom2 = lwgeom_linemerge(geom1);
87  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
88  ASSERT_STRING_EQUAL(out_ewkt, "MULTILINESTRING EMPTY");
89  lwfree(out_ewkt);
90  lwgeom_free(geom1);
91  lwgeom_free(geom2);
92 }
93 
94 static void
96 {
97  char* ewkt;
98  char* out_ewkt;
99  LWGEOM* geom1;
100  LWGEOM* geom2;
101 
102  ewkt = "MULTILINESTRING((-10 0, -10 100), (0 -5, 0 0))";
103  geom1 = lwgeom_from_wkt(ewkt, LW_PARSER_CHECK_NONE);
104  geom2 = lwgeom_offsetcurve(geom1, 2, 10, 1, 1);
105  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
106  ASSERT_STRING_EQUAL(out_ewkt, "MULTILINESTRING((-12 0,-12 100),(-2 -5,-2 0))");
107  lwfree(out_ewkt);
108  lwgeom_free(geom1);
109  lwgeom_free(geom2);
110 }
111 
112 static void
114 {
115  // Test for Trac #4143. The specific output or lack of output is not important,
116  // we're just testing that we don't crash.
117  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);
118  LWGEOM* out = lwgeom_offsetcurve(in, -0.045, 8, 2, 5.0);
119 
120  lwgeom_free(in);
121  if (out) {
122  lwgeom_free(out);
123  }
124 }
125 
126 static void
128 {
129  uint8_t* wkb;
130  char* out_ewkt;
131  LWGEOM* geom1;
132  LWGEOM* geom2;
133  LWGEOM* geom3;
134 
135  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";
136  geom1 = lwgeom_from_wkb(wkb, 157, LW_PARSER_CHECK_NONE);
137  geom2 = lwgeom_make_valid(geom1);
138  geom3 = lwgeom_normalize(geom2); //so GEOS 3.9 and 3.10 agree
139  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
140 
141  #if POSTGIS_GEOS_VERSION < 39
143  out_ewkt,
144  "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))");
145 #else
147  out_ewkt,
148  "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))");
149 #endif
150  lwfree(out_ewkt);
151  lwgeom_free(geom1);
152  lwgeom_free(geom2);
153  lwgeom_free(geom3);
154 }
155 
156 
157 static void test_geos_subdivide(void)
158 {
159  char *ewkt = "LINESTRING(0 0, 10 10)";
160  char *out_ewkt;
162  /* segmentize as geography to generate a non-simple curve */
163  LWGEOM *geom2 = lwgeom_segmentize_sphere(geom1, 0.002);
164 
165  LWCOLLECTION *geom3 = lwgeom_subdivide(geom2, 80);
166  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
167  // printf("\n--------\n%s\n--------\n", out_ewkt);
168  CU_ASSERT_EQUAL(2, geom3->ngeoms);
169  lwfree(out_ewkt);
170  lwcollection_free(geom3);
171 
172  geom3 = lwgeom_subdivide(geom2, 20);
173  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
174  // printf("\n--------\n%s\n--------\n", out_ewkt);
175  CU_ASSERT_EQUAL(8, geom3->ngeoms);
176  lwfree(out_ewkt);
177  lwcollection_free(geom3);
178 
179  lwgeom_free(geom2);
180  lwgeom_free(geom1);
181 }
182 
183 /*
184 ** Used by test harness to register the tests in this file.
185 */
186 void geos_suite_setup(void);
188 {
189  CU_pSuite suite = CU_add_suite("geos", NULL, NULL);
190  PG_ADD_TEST(suite, test_geos_noop);
196 }
static void test_geos_linemerge(void)
Definition: cu_geos.c:68
static void test_geos_subdivide(void)
Definition: cu_geos.c:157
static void test_geos_offsetcurve(void)
Definition: cu_geos.c:95
static void test_geos_makevalid(void)
Definition: cu_geos.c:127
void geos_suite_setup(void)
Definition: cu_geos.c:187
static void test_geos_noop(void)
Definition: cu_geos.c:23
static void test_geos_offsetcurve_crash(void)
Definition: cu_geos.c:113
#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:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
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:556
LWCOLLECTION * lwgeom_subdivide(const LWGEOM *geom, uint32_t maxvertices)
Definition: lwgeom.c:2448
void lwfree(void *mem)
Definition: lwutil.c:244
LWGEOM * lwgeom_normalize(const LWGEOM *geom)
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
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:789
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
uint32_t ngeoms
Definition: liblwgeom.h:510
unsigned char uint8_t
Definition: uthash.h:79