PostGIS  2.2.7dev-r@@SVN_REVISION@@
liblwgeom/cunit/cu_misc.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2008 Paul Ramsey
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "CUnit/Basic.h"
16 
17 #include "liblwgeom_internal.h"
18 #include "cu_tester.h"
19 
20 
21 static void test_misc_force_2d(void)
22 {
23  LWGEOM *geom;
24  LWGEOM *geom2d;
25  char *wkt_out;
26 
27  geom = lwgeom_from_wkt("CIRCULARSTRINGM(-5 0 4,0 5 3,5 0 2,10 -5 1,15 0 0)", LW_PARSER_CHECK_NONE);
28  geom2d = lwgeom_force_2d(geom);
29  wkt_out = lwgeom_to_ewkt(geom2d);
30  CU_ASSERT_STRING_EQUAL("CIRCULARSTRING(-5 0,0 5,5 0,10 -5,15 0)",wkt_out);
31  lwgeom_free(geom);
32  lwgeom_free(geom2d);
33  lwfree(wkt_out);
34 
35  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0 0),LINESTRING(1 1 1,2 2 2),POLYGON((0 0 1,0 1 1,1 1 1,1 0 1,0 0 1)),CURVEPOLYGON(CIRCULARSTRING(0 0 0,1 1 1,2 2 2,1 1 1,0 0 0)))", LW_PARSER_CHECK_NONE);
36  geom2d = lwgeom_force_2d(geom);
37  wkt_out = lwgeom_to_ewkt(geom2d);
38  CU_ASSERT_STRING_EQUAL("GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(1 1,2 2),POLYGON((0 0,0 1,1 1,1 0,0 0)),CURVEPOLYGON(CIRCULARSTRING(0 0,1 1,2 2,1 1,0 0)))",wkt_out);
39  lwgeom_free(geom);
40  lwgeom_free(geom2d);
41  lwfree(wkt_out);
42 }
43 
44 static void test_misc_simplify(void)
45 {
46  LWGEOM *geom;
47  LWGEOM *geom2d;
48  char *wkt_out;
49 
50  geom = lwgeom_from_wkt("LINESTRING(0 0,0 10,0 51,50 20,30 20,7 32)", LW_PARSER_CHECK_NONE);
51  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
52  wkt_out = lwgeom_to_ewkt(geom2d);
53  CU_ASSERT_STRING_EQUAL("LINESTRING(0 0,0 51,50 20,30 20,7 32)",wkt_out);
54  lwgeom_free(geom);
55  lwgeom_free(geom2d);
56  lwfree(wkt_out);
57 
58  geom = lwgeom_from_wkt("MULTILINESTRING((0 0,0 10,0 51,50 20,30 20,7 32))", LW_PARSER_CHECK_NONE);
59  geom2d = lwgeom_simplify(geom, 2, LW_FALSE);
60  wkt_out = lwgeom_to_ewkt(geom2d);
61  CU_ASSERT_STRING_EQUAL("MULTILINESTRING((0 0,0 51,50 20,30 20,7 32))",wkt_out);
62  lwgeom_free(geom);
63  lwgeom_free(geom2d);
64  lwfree(wkt_out);
65 }
66 
67 static void test_misc_count_vertices(void)
68 {
69  LWGEOM *geom;
70  int count;
71 
72  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(POINT(0 0),LINESTRING(0 0,1 1),POLYGON((0 0,0 1,1 0,0 0)),CIRCULARSTRING(0 0,0 1,1 1),CURVEPOLYGON(CIRCULARSTRING(0 0,0 1,1 1)))", LW_PARSER_CHECK_NONE);
73  count = lwgeom_count_vertices(geom);
74  CU_ASSERT_EQUAL(count,13);
75  lwgeom_free(geom);
76 
77  geom = lwgeom_from_wkt("GEOMETRYCOLLECTION(CIRCULARSTRING(0 0,0 1,1 1),POINT(0 0),CURVEPOLYGON(CIRCULARSTRING(0 0,0 1,1 1,1 0,0 0)))", LW_PARSER_CHECK_NONE);
78  count = lwgeom_count_vertices(geom);
79  CU_ASSERT_EQUAL(count,9);
80  lwgeom_free(geom);
81 
82  geom = lwgeom_from_wkt("CURVEPOLYGON((0 0,1 0,0 1,0 0),CIRCULARSTRING(0 0,1 0,1 1,1 0,0 0))", LW_PARSER_CHECK_NONE);
83  count = lwgeom_count_vertices(geom);
84  CU_ASSERT_EQUAL(count,9);
85  lwgeom_free(geom);
86 
87 
88  geom = lwgeom_from_wkt("POLYGON((0 0,1 0,0 1,0 0))", LW_PARSER_CHECK_NONE);
89  count = lwgeom_count_vertices(geom);
90  CU_ASSERT_EQUAL(count,4);
91  lwgeom_free(geom);
92 
93  geom = lwgeom_from_wkt("CURVEPOLYGON((0 0,1 0,0 1,0 0),CIRCULARSTRING(0 0,1 0,1 1,1 0,0 0))", LW_PARSER_CHECK_NONE);
94  count = lwgeom_count_vertices(geom);
95  CU_ASSERT_EQUAL(count,9);
96  lwgeom_free(geom);
97 }
98 
99 static void test_misc_area(void)
100 {
101  LWGEOM *geom;
102  double area;
103 
104  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_ALL);
105  area = lwgeom_area(geom);
106  CU_ASSERT_DOUBLE_EQUAL(area, 0.0, 0.0001);
107  lwgeom_free(geom);
108 }
109 
110 static void test_misc_wkb(void)
111 {
112  static char *wkb = "010A0000000200000001080000000700000000000000000000C00000000000000000000000000000F0BF000000000000F0BF00000000000000000000000000000000000000000000F03F000000000000F0BF000000000000004000000000000000000000000000000000000000000000004000000000000000C00000000000000000010200000005000000000000000000F0BF00000000000000000000000000000000000000000000E03F000000000000F03F00000000000000000000000000000000000000000000F03F000000000000F0BF0000000000000000";
114  char *str = lwgeom_to_wkt(geom, WKB_ISO, 8, 0);
115  CU_ASSERT_STRING_EQUAL(str, "CURVEPOLYGON(CIRCULARSTRING(-2 0,-1 -1,0 0,1 -1,2 0,0 2,-2 0),(-1 0,0 0.5,1 0,0 1,-1 0))");
116  lwfree(str);
117  lwgeom_free(geom);
118 
119 }
120 
121 
122 static void test_grid(void)
123 {
124  gridspec grid;
125  static char *wkt = "MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)))";
127  LWGEOM *geomgrid;
128  char *str;
129 
130  grid.ipx = grid.ipy = 0;
131  grid.xsize = grid.ysize = 20;
132 
133  geomgrid = lwgeom_grid(geom, &grid);
134  str = lwgeom_to_ewkt(geomgrid);
135  CU_ASSERT_STRING_EQUAL(str, "MULTIPOLYGON EMPTY");
136  lwfree(str);
137  lwgeom_free(geom);
138  lwgeom_free(geomgrid);
139 }
140 
141 
142 /*
143 ** Used by the test harness to register the tests in this file.
144 */
145 void misc_suite_setup(void);
147 {
148  CU_pSuite suite = CU_add_suite("miscellaneous", NULL, NULL);
152  PG_ADD_TEST(suite, test_misc_area);
153  PG_ADD_TEST(suite, test_misc_wkb);
154  PG_ADD_TEST(suite, test_grid);
155 }
LWGEOM * lwgeom_grid(const LWGEOM *lwgeom, const gridspec *grid)
Definition: lwgeom.c:1868
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:655
void lwfree(void *mem)
Definition: lwutil.c:214
Datum area(PG_FUNCTION_ARGS)
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:469
static void test_misc_area(void)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:890
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1869
#define LW_FALSE
Definition: liblwgeom.h:62
LWGEOM * lwgeom_simplify(const LWGEOM *igeom, double dist, int preserve_collapsed)
Definition: lwgeom.c:1558
#define PG_ADD_TEST(suite, testfunc)
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
Definition: lwgeom.c:690
void misc_suite_setup(void)
int count
Definition: genraster.py:56
#define WKB_ISO
Definition: liblwgeom.h:1930
static void test_misc_force_2d(void)
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:779
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1579
static void test_misc_simplify(void)
static void test_grid(void)
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:1870
static void test_misc_wkb(void)
int lwgeom_count_vertices(const LWGEOM *geom)
Count the total number of vertices in any LWGEOM.
Definition: lwgeom.c:1140
static void test_misc_count_vertices(void)
Snap to grid.