|
| 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 | +TEST_CASE("multipolygon geometry with single outer, no inner", "[NoDB]") |
| 19 | +{ |
| 20 | + geom::geometry_t geom{geom::multipolygon_t{}}; |
| 21 | + auto &mp = geom.get<geom::multipolygon_t>(); |
| 22 | + |
| 23 | + mp.add_geometry( |
| 24 | + geom::polygon_t{geom::ring_t{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}}); |
| 25 | + |
| 26 | + REQUIRE(geometry_type(geom) == "MULTIPOLYGON"); |
| 27 | + REQUIRE(num_geometries(geom) == 1); |
| 28 | + REQUIRE(area(geom) == Approx(1.0)); |
| 29 | + REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{0.5, 0.5}}); |
| 30 | +} |
| 31 | + |
| 32 | +TEST_CASE("multipolygon geometry with two polygons", "[NoDB]") |
| 33 | +{ |
| 34 | + geom::geometry_t geom{geom::multipolygon_t{}}; |
| 35 | + auto &mp = geom.get<geom::multipolygon_t>(); |
| 36 | + |
| 37 | + mp.add_geometry( |
| 38 | + geom::polygon_t{geom::ring_t{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}}); |
| 39 | + |
| 40 | + geom::polygon_t polygon{ |
| 41 | + geom::ring_t{{2, 2}, {2, 5}, {5, 5}, {5, 2}, {2, 2}}}; |
| 42 | + polygon.add_inner_ring( |
| 43 | + geom::ring_t{{3, 3}, {4, 3}, {4, 4}, {3, 4}, {3, 3}}); |
| 44 | + REQUIRE(polygon.num_geometries() == 1); |
| 45 | + REQUIRE(polygon.inners().size() == 1); |
| 46 | + |
| 47 | + mp.add_geometry(std::move(polygon)); |
| 48 | + |
| 49 | + REQUIRE(geometry_type(geom) == "MULTIPOLYGON"); |
| 50 | + REQUIRE(num_geometries(geom) == 2); |
| 51 | + REQUIRE(area(geom) == Approx(9.0)); |
| 52 | +} |
| 53 | + |
| 54 | +TEST_CASE("create_multipolygon creates simple polygon from OSM data", "[NoDB]") |
| 55 | +{ |
| 56 | + test_buffer_t buffer; |
| 57 | + buffer.add_way("w20 Nn1x1y1,n2x2y1,n3x2y2,n4x1y2"); |
| 58 | + buffer.add_way("w21 Nn4x1y2,n1x1y1"); |
| 59 | + auto const &relation = buffer.add_relation("r30 Mw20@,w21@"); |
| 60 | + |
| 61 | + auto const geom = geom::create_multipolygon(relation, buffer.buffer()); |
| 62 | + |
| 63 | + REQUIRE(geom.is_polygon()); |
| 64 | + REQUIRE(geometry_type(geom) == "POLYGON"); |
| 65 | + REQUIRE(num_geometries(geom) == 1); |
| 66 | + REQUIRE(area(geom) == Approx(1.0)); |
| 67 | + REQUIRE( |
| 68 | + geom.get<geom::polygon_t>() == |
| 69 | + geom::polygon_t{geom::ring_t{{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1}}}); |
| 70 | + REQUIRE(centroid(geom) == geom::geometry_t{geom::point_t{1.5, 1.5}}); |
| 71 | +} |
| 72 | + |
| 73 | +TEST_CASE("create_multipolygon from OSM data", "[NoDB]") |
| 74 | +{ |
| 75 | + test_buffer_t buffer; |
| 76 | + buffer.add_way("w20 Nn1x1y1,n2x2y1,n3x2y2,n4x1y2"); |
| 77 | + buffer.add_way("w21 Nn4x1y2,n1x1y1"); |
| 78 | + buffer.add_way("w22 Nn5x10y10,n6x10y20,n7x20y20,n5x10y10"); |
| 79 | + auto const &relation = buffer.add_relation("r30 Mw20@,w21@,w22@"); |
| 80 | + |
| 81 | + auto const geom = geom::create_multipolygon(relation, buffer.buffer()); |
| 82 | + |
| 83 | + REQUIRE(geom.is_multipolygon()); |
| 84 | + REQUIRE(geometry_type(geom) == "MULTIPOLYGON"); |
| 85 | + REQUIRE(num_geometries(geom) == 2); |
| 86 | + REQUIRE(area(geom) == Approx(51.0)); |
| 87 | +} |
| 88 | + |
| 89 | +TEST_CASE("create_multipolygon from OSM data without locations", "[NoDB]") |
| 90 | +{ |
| 91 | + test_buffer_t buffer; |
| 92 | + buffer.add_way("w20 Nn1,n2,n3,n1"); |
| 93 | + |
| 94 | + auto const &relation = buffer.add_relation("r30 Mw20@"); |
| 95 | + auto const geom = geom::create_multipolygon(relation, buffer.buffer()); |
| 96 | + |
| 97 | + REQUIRE(geom.is_null()); |
| 98 | +} |
| 99 | + |
| 100 | +TEST_CASE("create_multipolygon from invalid OSM data (single node)", "[NoDB]") |
| 101 | +{ |
| 102 | + test_buffer_t buffer; |
| 103 | + buffer.add_way("w20 Nn1x1y1"); |
| 104 | + |
| 105 | + auto const &relation = buffer.add_relation("r30 Mw20@"); |
| 106 | + auto const geom = geom::create_multipolygon(relation, buffer.buffer()); |
| 107 | + |
| 108 | + REQUIRE(geom.is_null()); |
| 109 | +} |
| 110 | + |
| 111 | +TEST_CASE("create_multipolygon from invalid OSM data (way node closed)", "[NoDB]") |
| 112 | +{ |
| 113 | + test_buffer_t buffer; |
| 114 | + buffer.add_way("w20 Nn1x1y1,n2x2y2"); |
| 115 | + |
| 116 | + auto const &relation = buffer.add_relation("r30 Mw20@"); |
| 117 | + auto const geom = geom::create_multipolygon(relation, buffer.buffer()); |
| 118 | + |
| 119 | + REQUIRE(geom.is_null()); |
| 120 | +} |
| 121 | + |
| 122 | +TEST_CASE("create_multipolygon from invalid OSM data (self-intersection)", "[NoDB]") |
| 123 | +{ |
| 124 | + test_buffer_t buffer; |
| 125 | + buffer.add_way("w20 Nn1x1y1,n2x1y2,n3x2y1,n4x2y2"); |
| 126 | + buffer.add_way("w21 Nn4x2y2,n1x1y1"); |
| 127 | + |
| 128 | + auto const &relation = buffer.add_relation("r30 Mw20@,w21@"); |
| 129 | + auto const geom = geom::create_multipolygon(relation, buffer.buffer()); |
| 130 | + |
| 131 | + REQUIRE(geom.is_null()); |
| 132 | +} |
0 commit comments