PostGIS  2.4.9dev-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  int 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 }
69 
70 static void test_geos_linemerge(void)
71 {
72  char *ewkt;
73  char *out_ewkt;
74  LWGEOM *geom1;
75  LWGEOM *geom2;
76 
77  ewkt = "MULTILINESTRING((0 0, 0 100),(0 -5, 0 0))";
79  geom2 = lwgeom_linemerge(geom1);
80  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
81  ASSERT_STRING_EQUAL(out_ewkt, "LINESTRING(0 -5,0 0,0 100)");
82  lwfree(out_ewkt);
83  lwgeom_free(geom1);
84  lwgeom_free(geom2);
85 
86  ewkt = "MULTILINESTRING EMPTY";
88  geom2 = lwgeom_linemerge(geom1);
89  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom2);
90  ASSERT_STRING_EQUAL(out_ewkt, "GEOMETRYCOLLECTION EMPTY");
91  lwfree(out_ewkt);
92  lwgeom_free(geom1);
93  lwgeom_free(geom2);
94 }
95 
96 
97 static void test_geos_subdivide(void)
98 {
99 #if POSTGIS_GEOS_VERSION < 35
100  // printf("%d\n", POSTGIS_GEOS_VERSION);
101  return;
102 #else
103  char *ewkt = "MULTILINESTRING((0 0, 0 100))";
104  char *out_ewkt;
106  LWGEOM *geom2 = lwgeom_segmentize2d(geom1, 1.0);
107 
108  LWCOLLECTION *geom3 = lwgeom_subdivide(geom2, 80);
109  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
110  // printf("\n--------\n%s\n--------\n", out_ewkt);
111  CU_ASSERT_EQUAL(2, geom3->ngeoms);
112  lwfree(out_ewkt);
113  lwcollection_free(geom3);
114 
115  geom3 = lwgeom_subdivide(geom2, 20);
116  out_ewkt = lwgeom_to_ewkt((LWGEOM*)geom3);
117  // printf("\n--------\n%s\n--------\n", out_ewkt);
118  CU_ASSERT_EQUAL(8, geom3->ngeoms);
119  lwfree(out_ewkt);
120  lwcollection_free(geom3);
121 
122  lwgeom_free(geom2);
123  lwgeom_free(geom1);
124 #endif
125 }
126 
127 /*
128 ** Used by test harness to register the tests in this file.
129 */
130 void geos_suite_setup(void);
132 {
133  CU_pSuite suite = CU_add_suite("GEOS", NULL, NULL);
134  PG_ADD_TEST(suite, test_geos_noop);
137 }
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:518
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
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:97
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
void geos_suite_setup(void)
Definition: cu_geos.c:131
static void test_geos_noop(void)
Definition: cu_geos.c:23
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2013
#define PG_ADD_TEST(suite, testfunc)
LWCOLLECTION * lwgeom_subdivide(const LWGEOM *geom, int maxvertices)
Definition: lwgeom.c:2054
static void test_geos_linemerge(void)
Definition: cu_geos.c:70
LWGEOM * lwgeom_segmentize2d(LWGEOM *line, double dist)
Definition: lwgeom.c:717
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:340
LWGEOM * lwgeom_linemerge(const LWGEOM *geom1)