|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + * |
| 4 | + * This file is part of osm2pgsql (https://osm2pgsql.org/). |
| 5 | + * |
| 6 | + * Copyright (C) 2006-2022 by the osm2pgsql developer community. |
| 7 | + * For a full list of authors see the git log. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <catch.hpp> |
| 11 | + |
| 12 | +#include "common-buffer.hpp" |
| 13 | + |
| 14 | +#include "geom-from-osm.hpp" |
| 15 | +#include "geom-functions.hpp" |
| 16 | +#include "geom.hpp" |
| 17 | + |
| 18 | +#include <array> |
| 19 | + |
| 20 | +TEST_CASE("geom::linestring_t", "[NoDB]") |
| 21 | +{ |
| 22 | + geom::linestring_t ls1; |
| 23 | + |
| 24 | + REQUIRE(ls1.empty()); |
| 25 | + ls1.emplace_back(17, 42); |
| 26 | + ls1.emplace_back(-3, 22); |
| 27 | + REQUIRE(ls1.size() == 2); |
| 28 | + |
| 29 | + auto it = ls1.cbegin(); |
| 30 | + REQUIRE(it != ls1.cend()); |
| 31 | + REQUIRE(it->x() == 17); |
| 32 | + ++it; |
| 33 | + REQUIRE(it != ls1.cend()); |
| 34 | + REQUIRE(it->y() == 22); |
| 35 | + ++it; |
| 36 | + REQUIRE(it == ls1.cend()); |
| 37 | + |
| 38 | + REQUIRE(ls1.num_geometries() == 1); |
| 39 | +} |
| 40 | + |
| 41 | +TEST_CASE("line geometry", "[NoDB]") |
| 42 | +{ |
| 43 | + geom::geometry_t const geom{geom::linestring_t{{1, 1}, {2, 2}}}; |
| 44 | + |
| 45 | + REQUIRE(num_geometries(geom) == 1); |
| 46 | + REQUIRE(area(geom) == Approx(0.0)); |
| 47 | + REQUIRE(geometry_type(geom) == "LINESTRING"); |
| 48 | + REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}}); |
| 49 | +} |
| 50 | + |
| 51 | +TEST_CASE("create_linestring from OSM data", "[NoDB]") |
| 52 | +{ |
| 53 | + test_buffer_t buffer; |
| 54 | + buffer.add_way("w20 Nn1x1y1,n2x2y2"); |
| 55 | + |
| 56 | + auto const geom = |
| 57 | + geom::create_linestring(buffer.buffer().get<osmium::Way>(0)); |
| 58 | + |
| 59 | + REQUIRE(geom.is_linestring()); |
| 60 | + REQUIRE(geometry_type(geom) == "LINESTRING"); |
| 61 | + REQUIRE(num_geometries(geom) == 1); |
| 62 | + REQUIRE(area(geom) == Approx(0.0)); |
| 63 | + REQUIRE(geom.get<geom::linestring_t>() == |
| 64 | + geom::linestring_t{{1, 1}, {2, 2}}); |
| 65 | + REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}}); |
| 66 | +} |
| 67 | + |
| 68 | +TEST_CASE("create_linestring from OSM data without locations", "[NoDB]") |
| 69 | +{ |
| 70 | + test_buffer_t buffer; |
| 71 | + buffer.add_way("w20 Nn1,n2"); |
| 72 | + |
| 73 | + auto const geom = |
| 74 | + geom::create_linestring(buffer.buffer().get<osmium::Way>(0)); |
| 75 | + |
| 76 | + REQUIRE(geom.is_null()); |
| 77 | +} |
| 78 | + |
| 79 | +TEST_CASE("create_linestring from invalid OSM data", "[NoDB]") |
| 80 | +{ |
| 81 | + test_buffer_t buffer; |
| 82 | + buffer.add_way("w20 Nn1x1y1"); |
| 83 | + |
| 84 | + auto const geom = |
| 85 | + geom::create_linestring(buffer.buffer().get<osmium::Way>(0)); |
| 86 | + |
| 87 | + REQUIRE(geom.is_null()); |
| 88 | +} |
| 89 | + |
| 90 | +TEST_CASE("geom::segmentize w/o split", "[NoDB]") |
| 91 | +{ |
| 92 | + geom::linestring_t const line{{0, 0}, {1, 2}, {2, 2}}; |
| 93 | + |
| 94 | + auto const geom = geom::segmentize(geom::geometry_t{line}, 10.0); |
| 95 | + |
| 96 | + REQUIRE(geom.is_multilinestring()); |
| 97 | + REQUIRE(num_geometries(geom) == 1); |
| 98 | + auto const &ml = geom.get<geom::multilinestring_t>(); |
| 99 | + REQUIRE(ml.num_geometries() == 1); |
| 100 | + REQUIRE(ml[0] == line); |
| 101 | +} |
| 102 | + |
| 103 | +TEST_CASE("geom::segmentize with split 0.5", "[NoDB]") |
| 104 | +{ |
| 105 | + geom::linestring_t const line{{0, 0}, {1, 0}}; |
| 106 | + |
| 107 | + std::array<geom::linestring_t, 2> const expected{ |
| 108 | + geom::linestring_t{{0, 0}, {0.5, 0}}, |
| 109 | + geom::linestring_t{{0.5, 0}, {1, 0}}}; |
| 110 | + |
| 111 | + auto const geom = geom::segmentize(geom::geometry_t{line}, 0.5); |
| 112 | + |
| 113 | + REQUIRE(geom.is_multilinestring()); |
| 114 | + auto const &ml = geom.get<geom::multilinestring_t>(); |
| 115 | + REQUIRE(ml.num_geometries() == 2); |
| 116 | + REQUIRE(ml[0] == expected[0]); |
| 117 | + REQUIRE(ml[1] == expected[1]); |
| 118 | +} |
| 119 | + |
| 120 | +TEST_CASE("geom::segmentize with split 0.4", "[NoDB]") |
| 121 | +{ |
| 122 | + geom::linestring_t const line{{0, 0}, {1, 0}}; |
| 123 | + |
| 124 | + std::array<geom::linestring_t, 3> const expected{ |
| 125 | + geom::linestring_t{{0, 0}, {0.4, 0}}, |
| 126 | + geom::linestring_t{{0.4, 0}, {0.8, 0}}, |
| 127 | + geom::linestring_t{{0.8, 0}, {1, 0}}}; |
| 128 | + |
| 129 | + auto const geom = geom::segmentize(geom::geometry_t{line}, 0.4); |
| 130 | + |
| 131 | + REQUIRE(geom.is_multilinestring()); |
| 132 | + auto const &ml = geom.get<geom::multilinestring_t>(); |
| 133 | + REQUIRE(ml.num_geometries() == 3); |
| 134 | + REQUIRE(ml[0] == expected[0]); |
| 135 | + REQUIRE(ml[1] == expected[1]); |
| 136 | + REQUIRE(ml[2] == expected[2]); |
| 137 | +} |
| 138 | + |
| 139 | +TEST_CASE("geom::segmentize with split 1.0 at start", "[NoDB]") |
| 140 | +{ |
| 141 | + geom::linestring_t const line{{0, 0}, {2, 0}, {3, 0}, {4, 0}}; |
| 142 | + |
| 143 | + std::array<geom::linestring_t, 4> const expected{ |
| 144 | + geom::linestring_t{{0, 0}, {1, 0}}, geom::linestring_t{{1, 0}, {2, 0}}, |
| 145 | + geom::linestring_t{{2, 0}, {3, 0}}, geom::linestring_t{{3, 0}, {4, 0}}}; |
| 146 | + |
| 147 | + auto const geom = geom::segmentize(geom::geometry_t{line}, 1.0); |
| 148 | + |
| 149 | + REQUIRE(geom.is_multilinestring()); |
| 150 | + auto const &ml = geom.get<geom::multilinestring_t>(); |
| 151 | + REQUIRE(ml.num_geometries() == 4); |
| 152 | + REQUIRE(ml[0] == expected[0]); |
| 153 | + REQUIRE(ml[1] == expected[1]); |
| 154 | + REQUIRE(ml[2] == expected[2]); |
| 155 | + REQUIRE(ml[3] == expected[3]); |
| 156 | +} |
| 157 | + |
| 158 | +TEST_CASE("geom::segmentize with split 1.0 in middle", "[NoDB]") |
| 159 | +{ |
| 160 | + geom::linestring_t const line{{0, 0}, {1, 0}, {3, 0}, {4, 0}}; |
| 161 | + |
| 162 | + std::array<geom::linestring_t, 4> const expected{ |
| 163 | + geom::linestring_t{{0, 0}, {1, 0}}, geom::linestring_t{{1, 0}, {2, 0}}, |
| 164 | + geom::linestring_t{{2, 0}, {3, 0}}, geom::linestring_t{{3, 0}, {4, 0}}}; |
| 165 | + |
| 166 | + auto const geom = geom::segmentize(geom::geometry_t{line}, 1.0); |
| 167 | + |
| 168 | + REQUIRE(geom.is_multilinestring()); |
| 169 | + auto const &ml = geom.get<geom::multilinestring_t>(); |
| 170 | + REQUIRE(ml.num_geometries() == 4); |
| 171 | + REQUIRE(ml[0] == expected[0]); |
| 172 | + REQUIRE(ml[1] == expected[1]); |
| 173 | + REQUIRE(ml[2] == expected[2]); |
| 174 | + REQUIRE(ml[3] == expected[3]); |
| 175 | +} |
| 176 | + |
| 177 | +TEST_CASE("geom::segmentize with split 1.0 at end", "[NoDB]") |
| 178 | +{ |
| 179 | + geom::linestring_t const line{{0, 0}, {1, 0}, {2, 0}, {4, 0}}; |
| 180 | + |
| 181 | + std::array<geom::linestring_t, 4> const expected{ |
| 182 | + geom::linestring_t{{0, 0}, {1, 0}}, geom::linestring_t{{1, 0}, {2, 0}}, |
| 183 | + geom::linestring_t{{2, 0}, {3, 0}}, geom::linestring_t{{3, 0}, {4, 0}}}; |
| 184 | + |
| 185 | + auto const geom = geom::segmentize(geom::geometry_t{line}, 1.0); |
| 186 | + |
| 187 | + REQUIRE(geom.is_multilinestring()); |
| 188 | + auto const &ml = geom.get<geom::multilinestring_t>(); |
| 189 | + REQUIRE(ml.num_geometries() == 4); |
| 190 | + REQUIRE(ml[0] == expected[0]); |
| 191 | + REQUIRE(ml[1] == expected[1]); |
| 192 | + REQUIRE(ml[2] == expected[2]); |
| 193 | + REQUIRE(ml[3] == expected[3]); |
| 194 | +} |
0 commit comments