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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
//
// Test Suite for geos::noding::NodedSegmentString class.
#include <tut/tut.hpp>
#include <utility.h>
// geos
#include <geos/io/WKTReader.h>
#include <geos/noding/NodedSegmentString.h>
#include <geos/noding/SegmentString.h>
#include <geos/noding/Octant.h>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/util.h>
// std
#include <memory>
using geos::io::WKTReader;
using geos::geom::CoordinateSequence;
using geos::geom::Geometry;
using geos::geom::GeometryFactory;
using geos::noding::SegmentString;
namespace tut {
//
// Test Group
//
// Common data used by tests
struct test_nodedsegmentstring_data {
typedef std::unique_ptr<geos::geom::CoordinateSequence> \
CoordinateSequenceAutoPtr;
typedef std::unique_ptr<geos::noding::NodedSegmentString> \
SegmentStringAutoPtr;
WKTReader r;
SegmentStringAutoPtr
makeSegmentString(geos::geom::CoordinateSequence* cs, void* d = nullptr)
{
return SegmentStringAutoPtr(
new geos::noding::NodedSegmentString(cs, true, false, d)
);
}
std::unique_ptr<Geometry>
toLines(SegmentString::NonConstVect& ss, const GeometryFactory* gf)
{
std::vector<std::unique_ptr<Geometry>> lines;
for (auto s: ss)
{
std::unique_ptr<CoordinateSequence> cs = s->getCoordinates()->clone();
lines.push_back(gf->createLineString(std::move(cs)));
}
return gf->createMultiLineString(std::move(lines));
}
void
checkNoding(const std::string& wktLine, const std::string& wktNodes, std::vector<size_t> segmentIndex, const std::string& wktExpected)
{
using geos::noding::NodedSegmentString;
std::unique_ptr<Geometry> line = r.read(wktLine);
std::unique_ptr<Geometry> pts = r.read(wktNodes);
NodedSegmentString nss(line->getCoordinates().release(), true, false, 0);
std::unique_ptr<CoordinateSequence> node = pts->getCoordinates();
for (std::size_t i = 0, n=node->size(); i < n; ++i) {
nss.addIntersection(node->getAt(i), segmentIndex.at(i));
}
SegmentString::NonConstVect nodedSS;
nss.getNodeList().addSplitEdges(nodedSS);
std::unique_ptr<Geometry> result = toLines(nodedSS, line->getFactory());
//System.out.println(result);
for (auto ss: nodedSS) {
delete ss;
}
std::unique_ptr<Geometry> expected = r.read(wktExpected);
ensure_equals_geometry(expected.get(), result.get());
}
test_nodedsegmentstring_data()
{
}
~test_nodedsegmentstring_data()
{
}
};
typedef test_group<test_nodedsegmentstring_data> group;
typedef group::object object;
group test_nodedsegmentstring_group("geos::noding::NodedSegmentString");
//
// Test Cases
//
// test constructor with 2 equal points
template<>
template<>
void object::test<1>
()
{
auto cs = geos::detail::make_unique<geos::geom::CoordinateSequence>(0u, 2u);
ensure(nullptr != cs.get());
geos::geom::Coordinate c0(0, 0);
geos::geom::Coordinate c1(0, 0);
cs->add(c0);
cs->add(c1);
ensure_equals(cs->size(), 2u);
SegmentStringAutoPtr ss(makeSegmentString(cs.release()));
ensure(nullptr != ss.get());
ensure_equals(ss->size(), 2u);
ensure_equals(ss->getData(), (void*)nullptr);
ensure_equals(ss->getCoordinate(0), c0);
ensure_equals(ss->getCoordinate(1), c1);
ensure_equals(ss->isClosed(), true);
ensure_equals(ss->getNodeList().size(), 0u);
ensure_equals(ss->getSegmentOctant(0), 0);
}
// test constructor with 2 different points
template<>
template<>
void object::test<2>
()
{
auto cs = geos::detail::make_unique<geos::geom::CoordinateSequence>(0u, 2u);
ensure(nullptr != cs.get());
geos::geom::Coordinate c0(0, 0);
geos::geom::Coordinate c1(1, 0);
cs->add(c0);
cs->add(c1);
ensure_equals(cs->size(), 2u);
SegmentStringAutoPtr ss(makeSegmentString(cs.release()));
ensure(nullptr != ss.get());
ensure_equals(ss->size(), 2u);
ensure_equals(ss->getData(), (void*)nullptr);
ensure_equals(ss->getCoordinate(0), c0);
ensure_equals(ss->getCoordinate(1), c1);
ensure_equals(ss->isClosed(), false);
ensure_equals(ss->getSegmentOctant(0), 0);
ensure_equals(ss->getNodeList().size(), 0u);
}
// test constructor with 4 different points forming a ring
template<>
template<>
void object::test<3>
()
{
auto cs = geos::detail::make_unique<geos::geom::CoordinateSequence>(0u, 2u);
ensure(nullptr != cs.get());
geos::geom::Coordinate c0(0, 0);
geos::geom::Coordinate c1(1, 0);
geos::geom::Coordinate c2(1, 1);
cs->add(c0);
cs->add(c1);
cs->add(c2);
cs->add(c0);
ensure_equals(cs->size(), 4u);
SegmentStringAutoPtr ss(makeSegmentString(cs.release()));
ensure(nullptr != ss.get());
ensure_equals(ss->size(), 4u);
ensure_equals(ss->getData(), (void*)nullptr);
ensure_equals(ss->getCoordinate(0), c0);
ensure_equals(ss->getCoordinate(1), c1);
ensure_equals(ss->getCoordinate(2), c2);
ensure_equals(ss->getCoordinate(3), c0);
ensure_equals(ss->isClosed(), true);
ensure_equals(ss->getSegmentOctant(2), 4);
ensure_equals(ss->getSegmentOctant(1), 1);
ensure_equals(ss->getSegmentOctant(0), 0);
ensure_equals(ss->getNodeList().size(), 0u);
}
// test Octant class
template<>
template<>
void object::test<4>
()
{
geos::geom::Coordinate p0(0, 0);
geos::geom::Coordinate p1(5, -5);
int octant_rc1 = 0;
int octant_rc2 = 0;
int testPassed = true;
try {
octant_rc1 = geos::noding::Octant::octant(p0, p1);
octant_rc2 = geos::noding::Octant::octant(&p0, &p1);
testPassed = (octant_rc1 == octant_rc2);
}
catch(...) {
testPassed = false;
}
ensure(0 != testPassed);
}
// test adding intersections
template<>
template<>
void object::test<5>
()
{
geos::geom::Coordinate p0(0, 0);
geos::geom::Coordinate p1(10, 0);
auto cs = geos::detail::make_unique<geos::geom::CoordinateSequence>(0u, 2u);
cs->add(p0);
cs->add(p1);
SegmentStringAutoPtr ss(makeSegmentString(cs.release()));
ensure_equals(ss->getNodeList().size(), 0u);
// the intersection is invalid, but SegmentString trusts us
ss->addIntersection(p0, 0);
ensure_equals(ss->getNodeList().size(), 1u);
// This node is already present, so shouldn't be
// accepted as a new one
ss->addIntersection(p0, 0);
ensure_equals(ss->getNodeList().size(), 1u);
ss->addIntersection(p1, 0);
ensure_equals(ss->getNodeList().size(), 2u);
ss->addIntersection(p1, 0);
ensure_equals(ss->getNodeList().size(), 2u);
ss->addIntersection(p0, 0);
ensure_equals(ss->getNodeList().size(), 2u);
}
/**
* Tests a case which involves nodes added when using the SnappingNoder.
* In this case one of the added nodes is relatively "far" from its segment,
* and "near" the start vertex of the segment.
* Computing the noding correctly requires the fix to {@link SegmentNode#compareTo(Object)}
* added in https://github.com/locationtech/jts/pull/399
*
* See https://trac.osgeo.org/geos/ticket/1051
*/
template<>
template<>
void object::test<6>
()
{
std::vector<size_t> segmentIndex;
segmentIndex.push_back(0);
segmentIndex.push_back(0);
segmentIndex.push_back(1);
segmentIndex.push_back(1);
checkNoding("LINESTRING(655103.6628454948 1794805.456674405, 655016.20226 1794940.10998, 655014.8317182435 1794941.5196832407)",
"MULTIPOINT((655016.29615051334 1794939.965427252),(655016.20226531825 1794940.1099718122), (655016.20226 1794940.10998),(655016.20225819293 1794940.1099794197))",
segmentIndex,
"MULTILINESTRING ((655014.8317182435 1794941.5196832407,655016.2022581929 1794940.1099794197), (655016.2022581929 1794940.1099794197, 655016.20226 1794940.10998), (655016.20226 1794940.10998, 655016.2022653183 1794940.1099718122), (655016.2022653183 1794940.1099718122, 655016.2961505133 1794939.965427252), (655016.2961505133 1794939.965427252, 655103.6628454948 1794805.456674405))");
}
// TODO: test getting noded substrings
// template<>
// template<>
// void object::test<6>()
// {
// geos::geom::Coordinate cs1p0(0, 0);
// geos::geom::Coordinate cs1p1(10, 0);
// CoordinateSequenceAutoPtr cs1(csFactory->create(0, 2));
// cs1->add(cs1p0);
// cs1->add(cs1p1);
//
// geos::geom::Coordinate cs2p0(5, -5);
// geos::geom::Coordinate cs2p1(5, 5);
// CoordinateSequenceAutoPtr cs2(csFactory->create(0, 2));
// cs2->add(cs2p0);
// cs2->add(cs2p1);
//
// using geos::noding::SegmentString;
// using geos::noding::NodedSegmentString;
//
// SegmentString::NonConstVect inputStrings;
// inputStrings.push_back(makeSegmentString(cs2.get()).get());
//
// std::unique_ptr<SegmentString::NonConstVect> nodedStrings(
// NodedSegmentString::getNodedSubstrings(inputStrings)
// );
//
// ensure_equals(nodedStrings->size(), 0u);
// }
} // namespace tut
|