PostGIS  2.5.0beta2dev-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  uint8_t* wkb;
116  char* out_ewkt;
117  LWGEOM* geom1;
118  LWGEOM* geom2;
119 
120  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";
121  geom1 = lwgeom_from_wkb(wkb, 157, LW_PARSER_CHECK_NONE);
122  geom2 = lwgeom_make_valid(geom1);
123  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
124  ASSERT_STRING_EQUAL(out_ewkt, "GEOMETRYCOLLECTION(POLYGON((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,92114.014 463463.469)),MULTIPOINT(92115.5120743 463462.206937,92122.136 463412.826))");
125  lwfree(out_ewkt);
126  lwgeom_free(geom1);
127  lwgeom_free(geom2);
128 }
129 
130 
131 static void test_geos_subdivide(void)
132 {
133  char *ewkt = "LINESTRING(0 0, 10 10)";
134  char *out_ewkt;
136  /* segmentize as geography to generate a non-simple curve */
137  LWGEOM *geom2 = lwgeom_segmentize_sphere(geom1, 0.002);
138 
139  LWCOLLECTION *geom3 = lwgeom_subdivide(geom2, 80);
140  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
141  // printf("\n--------\n%s\n--------\n", out_ewkt);
142  CU_ASSERT_EQUAL(2, geom3->ngeoms);
143  lwfree(out_ewkt);
144  lwcollection_free(geom3);
145 
146  geom3 = lwgeom_subdivide(geom2, 20);
147  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
148  // printf("\n--------\n%s\n--------\n", out_ewkt);
149  CU_ASSERT_EQUAL(8, geom3->ngeoms);
150  lwfree(out_ewkt);
151  lwcollection_free(geom3);
152 
153  lwgeom_free(geom2);
154  lwgeom_free(geom1);
155 }
156 
157 /*
158 ** Used by test harness to register the tests in this file.
159 */
160 void geos_suite_setup(void);
162 {
163  CU_pSuite suite = CU_add_suite("geos", NULL, NULL);
164  PG_ADD_TEST(suite, test_geos_noop);
169 }
void lwfree(void *mem)
Definition: lwutil.c:244
#define ASSERT_STRING_EQUAL(o, e)
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
LWGEOM * lwgeom_geos_noop(const LWGEOM *geom)
Convert an LWGEOM to a GEOS Geometry and convert back – for debug only.
static void test_geos_subdivide(void)
Definition: cu_geos.c:131
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
void geos_suite_setup(void)
Definition: cu_geos.c:161
static void test_geos_noop(void)
Definition: cu_geos.c:23
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2004
LWGEOM * lwgeom_offsetcurve(const LWGEOM *geom, double size, int quadsegs, int joinStyle, double mitreLimit)
LWGEOM * lwgeom_make_valid(LWGEOM *geom)
Attempts to make an invalid geometries valid w/out losing points.
LWCOLLECTION * lwgeom_subdivide(const LWGEOM *geom, uint32_t maxvertices)
Definition: lwgeom.c:2412
#define PG_ADD_TEST(suite, testfunc)
static void test_geos_makevalid(void)
Definition: cu_geos.c:113
static void test_geos_offsetcurve(void)
Definition: cu_geos.c:95
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:1656
static void test_geos_linemerge(void)
Definition: cu_geos.c:68
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
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:770
unsigned char uint8_t
Definition: uthash.h:79
LWGEOM * lwgeom_linemerge(const LWGEOM *geom1)