File: GEOSSnapTest.cpp

package info (click to toggle)
geos 3.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 31,212 kB
  • sloc: cpp: 199,103; xml: 56,065; ansic: 6,162; sh: 287; makefile: 26
file content (224 lines) | stat: -rw-r--r-- 5,419 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
// Test Suite for C-API GEOSSimplify

#include <tut/tut.hpp>
// geos
#include <geos_c.h>
// std
#include <cstdlib>

#include "capi_test_utils.h"

namespace tut {
//
// Test Group
//

// Common data used in test cases.
struct test_capigeossnap_data : public capitest::utility {
    test_capigeossnap_data()
    {
        GEOSWKTWriter_setRoundingPrecision(wktw_, 8);
    }
};

typedef test_group<test_capigeossnap_data> group;
typedef group::object object;

group test_capigeossnap_group("capi::GEOSSnap");

//
// Test Cases
//

/// Polygon snapped to point
template<>
template<>
void object::test<1>
()
{
    geom1_ = GEOSGeomFromWKT("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))");
    geom2_ = GEOSGeomFromWKT("POINT(0.5 0)");
    geom3_ = GEOSSnap(geom1_, geom2_, 1);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "POLYGON ((0.5 0, 10 0, 10 10, 0 10, 0.5 0))");
}

/// Line snapped to line (vertex)
template<>
template<>
void object::test<2>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING (-30 -20, 50 60, 50 0)");
    geom2_ = GEOSGeomFromWKT("LINESTRING (-29 -20, 40 60, 51 0)");
    geom3_ = GEOSSnap(geom1_, geom2_, 2);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "LINESTRING (-29 -20, 50 60, 51 0)");
}

/// Line snapped to line (segment)
template<>
template<>
void object::test<3>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING (-20 -20, 50 50, 100 100)");
    geom2_ = GEOSGeomFromWKT("LINESTRING (-10 -9, 40 20, 80 79)");
    geom3_ = GEOSSnap(geom1_, geom2_, 2);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out,
                  "LINESTRING (-20 -20, -10 -9, 50 50, 80 79, 100 100)"
                 );
}

/// Another single segment
template<>
template<>
void object::test<4>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING(0 0, 10 0)");
    geom2_ = GEOSGeomFromWKT("LINESTRING(0 0, 9 0)");
    geom3_ = GEOSSnap(geom1_, geom2_, 2);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "LINESTRING (0 0, 9 0)");
}

/// See http://trac.osgeo.org/geos/ticket/501
template<>
template<>
void object::test<5>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING(0 0, 10 0)");
    geom2_ = GEOSGeomFromWKT("LINESTRING(0 0, 9 0, 10 0, 11 0)");
    geom3_ = GEOSSnap(geom1_, geom2_, 2);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    //ensure_equals(out, "LINESTRING (0 0, 9 0, 10 0)");
    ensure_equals(out, "LINESTRING (0 0, 9 0, 10 0, 11 0)");
}

/// Test snapping of equidistant segments to outlyers snap point
template<>
template<>
void object::test<6>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING(0 3,4 1,0 1)");
    geom2_ = GEOSGeomFromWKT("MULTIPOINT((5 0),(4 1))");
    geom3_ = GEOSSnap(geom1_, geom2_, 2);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "LINESTRING (0 3, 4 1, 5 0, 0 1)");
    //ensure_equals(out, "LINESTRING (0 3, 4 1, 0 1)");
}

/// Test snapping of equidistant segments to outlyers snap point
/// Same as the above but with the snap points order reversed
template<>
template<>
void object::test<7>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING(0 3,4 1,0 1)");
    geom2_ = GEOSGeomFromWKT("MULTIPOINT((4 1),(5 0))");
    geom3_ = GEOSSnap(geom1_, geom2_, 2);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "LINESTRING (0 3, 4 1, 5 0, 0 1)");
    //ensure_equals(out, "LINESTRING (0 3, 4 1, 0 1)");
}

/// Test snapping of closed ring to outlyers snap point
template<>
template<>
void object::test<8>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING(0 0,10 0,10 10,0 10,0 0)");
    geom2_ = GEOSGeomFromWKT("MULTIPOINT((0 0),(-1 0))");
    geom3_ = GEOSSnap(geom1_, geom2_, 3);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "LINESTRING (-1 0, 0 0, 10 0, 10 10, 0 10, -1 0)");
}

template<>
template<>
void object::test<9>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING(0 2,5 2,9 2,5 0)");
    geom2_ = GEOSGeomFromWKT("POINT(5 0)");
    geom3_ = GEOSSnap(geom1_, geom2_, 3);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "LINESTRING (0 2, 5 2, 9 2, 5 0)");
}

// See http://trac.osgeo.org/geos/ticket/649
template<>
template<>
void object::test<10>
()
{
    geom1_ = GEOSGeomFromWKT("LINESTRING(-71.1317 42.2511,-71.1317 42.2509)");
    geom2_ = GEOSGeomFromWKT("MULTIPOINT((-71.1261 42.2703),(-71.1257 42.2703),(-71.1261 42.2702))");
    geom3_ = GEOSSnap(geom1_, geom2_, 0.5);

    char* wkt_c = GEOSWKTWriter_write(wktw_, geom3_);
    std::string out(wkt_c);
    free(wkt_c);

    ensure_equals(out, "LINESTRING (-71.1257 42.2703, -71.1261 42.2703, -71.1261 42.2702, -71.1317 42.2509)");
}

template<>
template<>
void object::test<11>()
{
    geom1_ = fromWKT("CIRCULARSTRING (0 0, 1 1, 2 0)");
    geom2_ = fromWKT("LINESTRING (1 1.0001, 2 1)");

    ensure(geom1_);
    ensure(geom2_);

    result_ = GEOSSnap(geom1_, geom2_, 0.1);
    ensure("curved geometry not supported", result_ == nullptr);
}

} // namespace tut