PostGIS  2.1.10dev-r@@SVN_REVISION@@
liblwgeom/cunit/cu_misc.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: cu_print.c 6160 2010-11-01 01:28:12Z pramsey $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2008 Paul Ramsey
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 "liblwgeom_internal.h"
19 #include "cu_tester.h"
20 
21 
22 static void test_misc_force_2d(void)
23 {
24  LWGEOM *geom;
25  LWGEOM *geom2d;
26  char *wkt_out;
27 
28  geom = lwgeom_from_wkt("CIRCULARSTRINGM(-5 0 4,0 5 3,5 0 2,10 -5 1,15 0 0)", LW_PARSER_CHECK_NONE);
29  geom2d = lwgeom_force_2d(geom);
30  wkt_out = lwgeom_to_ewkt(geom2d);
31  CU_ASSERT_STRING_EQUAL("CIRCULARSTRING(-5 0,0 5,5 0,10 -5,15 0)",wkt_out);
32  lwgeom_free(geom);
33  lwgeom_free(geom2d);
34  lwfree(wkt_out);
35 
36  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);
37  geom2d = lwgeom_force_2d(geom);
38  wkt_out = lwgeom_to_ewkt(geom2d);
39  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);
40  lwgeom_free(geom);
41  lwgeom_free(geom2d);
42  lwfree(wkt_out);
43 }
44 
45 static void test_misc_simplify(void)
46 {
47  LWGEOM *geom;
48  LWGEOM *geom2d;
49  char *wkt_out;
50 
51  geom = lwgeom_from_wkt("LINESTRING(0 0,0 10,0 51,50 20,30 20,7 32)", LW_PARSER_CHECK_NONE);
52  geom2d = lwgeom_simplify(geom,2);
53  wkt_out = lwgeom_to_ewkt(geom2d);
54  CU_ASSERT_STRING_EQUAL("LINESTRING(0 0,0 51,50 20,30 20,7 32)",wkt_out);
55  lwgeom_free(geom);
56  lwgeom_free(geom2d);
57  lwfree(wkt_out);
58 
59  geom = lwgeom_from_wkt("MULTILINESTRING((0 0,0 10,0 51,50 20,30 20,7 32))", LW_PARSER_CHECK_NONE);
60  geom2d = lwgeom_simplify(geom,2);
61  wkt_out = lwgeom_to_ewkt(geom2d);
62  CU_ASSERT_STRING_EQUAL("MULTILINESTRING((0 0,0 51,50 20,30 20,7 32))",wkt_out);
63  lwgeom_free(geom);
64  lwgeom_free(geom2d);
65  lwfree(wkt_out);
66 }
67 
68 static void test_misc_count_vertices(void)
69 {
70  LWGEOM *geom;
71  int count;
72 
73  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);
74  count = lwgeom_count_vertices(geom);
75  CU_ASSERT_EQUAL(count,13);
76  lwgeom_free(geom);
77 
78  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);
79  count = lwgeom_count_vertices(geom);
80  CU_ASSERT_EQUAL(count,9);
81  lwgeom_free(geom);
82 
83  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);
84  count = lwgeom_count_vertices(geom);
85  CU_ASSERT_EQUAL(count,9);
86  lwgeom_free(geom);
87 
88 
89  geom = lwgeom_from_wkt("POLYGON((0 0,1 0,0 1,0 0))", LW_PARSER_CHECK_NONE);
90  count = lwgeom_count_vertices(geom);
91  CU_ASSERT_EQUAL(count,4);
92  lwgeom_free(geom);
93 
94  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);
95  count = lwgeom_count_vertices(geom);
96  CU_ASSERT_EQUAL(count,9);
97  lwgeom_free(geom);
98 }
99 
100 static void test_misc_area(void)
101 {
102  LWGEOM *geom;
103  double area;
104 
105  geom = lwgeom_from_wkt("LINESTRING EMPTY", LW_PARSER_CHECK_ALL);
106  area = lwgeom_area(geom);
107  CU_ASSERT_DOUBLE_EQUAL(area, 0.0, 0.0001);
108  lwgeom_free(geom);
109 }
110 
111 static void test_misc_wkb(void)
112 {
113  static char *wkb = "010A0000000200000001080000000700000000000000000000C00000000000000000000000000000F0BF000000000000F0BF00000000000000000000000000000000000000000000F03F000000000000F0BF000000000000004000000000000000000000000000000000000000000000004000000000000000C00000000000000000010200000005000000000000000000F0BF00000000000000000000000000000000000000000000E03F000000000000F03F00000000000000000000000000000000000000000000F03F000000000000F0BF0000000000000000";
115  char *str = lwgeom_to_wkt(geom, WKB_ISO, 8, 0);
116  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))");
117  lwfree(str);
118  lwgeom_free(geom);
119 
120 }
121 
122 /*
123 ** Used by the test harness to register the tests in this file.
124 */
125 void misc_suite_setup(void);
127 {
128  CU_pSuite suite = CU_add_suite("Miscellaneous", NULL, NULL);
132  PG_ADD_TEST(suite, test_misc_area);
133  PG_ADD_TEST(suite, test_misc_wkb);
134 }
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:190
Datum area(PG_FUNCTION_ARGS)
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:425
static void test_misc_area(void)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:844
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1706
#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:646
void misc_suite_setup(void)
int count
Definition: genraster.py:57
#define WKB_ISO
Definition: liblwgeom.h:1767
static void test_misc_force_2d(void)
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:753
double lwgeom_area(const LWGEOM *geom)
Definition: lwgeom.c:1499
static void test_misc_simplify(void)
LWGEOM * lwgeom_simplify(const LWGEOM *igeom, double dist)
Definition: lwgeom.c:1478
#define LW_PARSER_CHECK_ALL
Definition: liblwgeom.h:1707
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:1072
static void test_misc_count_vertices(void)