2016-03-16 15:06:27 +01:00
|
|
|
#include "catch.hpp"
|
|
|
|
// mapnik
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/geometry.hpp>
|
2016-08-02 15:43:02 +02:00
|
|
|
#include <mapnik/geometry/boost_adapters.hpp>
|
|
|
|
#include <mapnik/geometry/is_empty.hpp>
|
2016-03-16 15:06:27 +01:00
|
|
|
#include <mapnik/util/geometry_to_wkt.hpp>
|
|
|
|
// bool
|
|
|
|
#include <boost/version.hpp>
|
|
|
|
#include <boost/geometry/algorithms/equals.hpp>
|
|
|
|
#include <boost/geometry/policies/compare.hpp>
|
|
|
|
// stl
|
|
|
|
#include "parse_hex.hpp"
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
namespace {
|
2016-03-16 15:06:27 +01:00
|
|
|
|
|
|
|
struct spatially_equal_visitor
|
|
|
|
{
|
|
|
|
using result_type = bool;
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
result_type operator()(mapnik::geometry::geometry_empty, mapnik::geometry::geometry_empty) const { return true; }
|
2016-03-16 15:06:27 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
result_type operator()(mapnik::geometry::geometry_collection<double> const& lhs,
|
|
|
|
mapnik::geometry::geometry_collection<double> const& rhs) const
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
|
|
|
std::size_t size0 = lhs.size();
|
|
|
|
std::size_t size1 = rhs.size();
|
2022-01-26 23:25:53 +01:00
|
|
|
if (size0 != size1)
|
|
|
|
return false;
|
|
|
|
for (std::size_t index = 0; index < size0; ++index)
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
|
|
|
if (!mapnik::util::apply_visitor(*this, lhs[index], rhs[index]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
result_type operator()(mapnik::geometry::multi_point<double> const& lhs,
|
|
|
|
mapnik::geometry::multi_point<double> const& rhs) const
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
|
|
|
std::size_t size0 = lhs.size();
|
|
|
|
std::size_t size1 = rhs.size();
|
2022-01-26 23:25:53 +01:00
|
|
|
if (size0 != size1)
|
|
|
|
return false;
|
2016-03-16 15:06:27 +01:00
|
|
|
auto tmp0 = lhs;
|
|
|
|
auto tmp1 = rhs;
|
|
|
|
std::sort(tmp0.begin(), tmp0.end(), boost::geometry::less<mapnik::geometry::point<double>>());
|
|
|
|
std::sort(tmp1.begin(), tmp1.end(), boost::geometry::less<mapnik::geometry::point<double>>());
|
2022-01-26 23:25:53 +01:00
|
|
|
for (std::size_t index = 0; index < size0; ++index)
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
2022-01-26 23:25:53 +01:00
|
|
|
if (!boost::geometry::equals(tmp0[index], tmp1[index]))
|
|
|
|
return false;
|
2016-03-16 15:06:27 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-30 11:40:25 +02:00
|
|
|
result_type operator()(mapnik::geometry::multi_line_string<double> const& lhs,
|
|
|
|
mapnik::geometry::multi_line_string<double> const& rhs) const
|
|
|
|
{
|
|
|
|
std::size_t size0 = lhs.size();
|
|
|
|
std::size_t size1 = rhs.size();
|
|
|
|
if (size0 != size1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (std::size_t index = 0; index < size0; ++index)
|
|
|
|
{
|
|
|
|
if (!boost::geometry::equals(lhs[index], rhs[index]))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
template<typename T>
|
|
|
|
result_type operator()(T const& lhs, T const& rhs) const
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
|
|
|
if (mapnik::geometry::is_empty(lhs) && mapnik::geometry::is_empty(rhs))
|
2022-01-26 23:25:53 +01:00
|
|
|
return true; // Empty geometries of the same type are considered to be spatially equal
|
2016-03-16 15:06:27 +01:00
|
|
|
return boost::geometry::equals(lhs, rhs);
|
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
template<typename T0, typename T1>
|
|
|
|
result_type operator()(T0 const& lhs, T1 const& rhs) const
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
template<typename T>
|
2016-03-16 15:06:27 +01:00
|
|
|
bool spatially_equal(mapnik::geometry::geometry<T> const& g0, mapnik::geometry::geometry<T> const& g1)
|
|
|
|
{
|
|
|
|
return mapnik::util::apply_visitor(spatially_equal_visitor(), g0, g1);
|
|
|
|
}
|
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
} // namespace
|
2022-11-04 15:07:28 +01:00
|
|
|
|
2016-03-16 15:06:27 +01:00
|
|
|
TEST_CASE("Well-known-geometries")
|
|
|
|
{
|
2017-05-05 16:32:17 +02:00
|
|
|
SECTION("wkb+wkt")
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
|
|
|
std::string filename("test/unit/data/well-known-geometries.test");
|
2022-01-26 23:25:53 +01:00
|
|
|
std::ifstream is(filename.c_str(), std::ios_base::in | std::ios_base::binary);
|
|
|
|
if (!is)
|
|
|
|
throw std::runtime_error("could not open: '" + filename + "'");
|
2016-03-16 15:06:27 +01:00
|
|
|
|
2022-01-26 23:25:53 +01:00
|
|
|
for (std::string line; std::getline(is, line, '\n');)
|
2016-03-16 15:06:27 +01:00
|
|
|
{
|
|
|
|
std::vector<std::string> columns;
|
|
|
|
boost::split(columns, line, boost::is_any_of(";"));
|
|
|
|
REQUIRE(columns.size() == 3);
|
|
|
|
std::vector<char> wkb, twkb;
|
|
|
|
REQUIRE(mapnik::util::parse_hex(columns[1], wkb));
|
|
|
|
REQUIRE(mapnik::util::parse_hex(columns[2], twkb));
|
2022-01-26 23:25:53 +01:00
|
|
|
mapnik::geometry::geometry<double> geom_0 =
|
|
|
|
mapnik::geometry_utils::from_wkb(wkb.data(), wkb.size(), mapnik::wkbAuto);
|
2016-03-16 15:06:27 +01:00
|
|
|
mapnik::geometry::geometry<double> geom_1 = mapnik::geometry_utils::from_twkb(twkb.data(), twkb.size());
|
2016-11-15 00:23:29 +01:00
|
|
|
// compare WKTs as doubles
|
2017-05-05 16:32:17 +02:00
|
|
|
std::string wkt, wkt0, wkt1;
|
|
|
|
wkt = columns[0];
|
2022-01-26 23:25:53 +01:00
|
|
|
// wkt.erase(std::remove(wkt.begin(), wkt.end(), ' '), wkt.end());
|
|
|
|
// ^ we can't use this approach because spaces are part of format e.g POINT(100 200)
|
2016-03-16 15:06:27 +01:00
|
|
|
REQUIRE(mapnik::util::to_wkt(wkt0, geom_0));
|
|
|
|
REQUIRE(mapnik::util::to_wkt(wkt1, geom_1));
|
|
|
|
if (!mapnik::geometry::is_empty(geom_0) && !mapnik::geometry::is_empty(geom_1))
|
|
|
|
{
|
|
|
|
REQUIRE(wkt0 == wkt1);
|
2017-05-05 16:32:17 +02:00
|
|
|
REQUIRE(wkt0 == wkt); // WKT round-trip
|
2016-03-16 15:06:27 +01:00
|
|
|
// compare spatially (NOTE: GeometryCollection comparison also enforces strict order)
|
|
|
|
REQUIRE(spatially_equal(geom_0, geom_1));
|
|
|
|
}
|
2016-11-15 00:23:29 +01:00
|
|
|
|
|
|
|
// compare WKTS as ints
|
|
|
|
// note: mapnik::util::to_wkt<std::int64_t> used in mapnik-vt
|
|
|
|
std::string wkt2, wkt3;
|
|
|
|
mapnik::geometry::line_string<std::int64_t> geom_2;
|
2022-01-26 23:25:53 +01:00
|
|
|
geom_2.emplace_back(0, 0);
|
|
|
|
geom_2.emplace_back(1, 1);
|
|
|
|
geom_2.emplace_back(2, 2);
|
2016-11-15 00:23:29 +01:00
|
|
|
mapnik::geometry::line_string<std::int64_t> geom_3;
|
2022-01-26 23:25:53 +01:00
|
|
|
geom_3.emplace_back(0, 0);
|
|
|
|
geom_3.emplace_back(1, 1);
|
|
|
|
geom_3.emplace_back(2, 2);
|
2017-07-06 16:03:28 +02:00
|
|
|
REQUIRE(mapnik::util::to_wkt(wkt0, mapnik::geometry::geometry<std::int64_t>(geom_2)));
|
|
|
|
REQUIRE(mapnik::util::to_wkt(wkt1, mapnik::geometry::geometry<std::int64_t>(geom_3)));
|
2016-11-15 00:23:29 +01:00
|
|
|
if (!mapnik::geometry::is_empty(geom_2) && !mapnik::geometry::is_empty(geom_3))
|
|
|
|
{
|
|
|
|
REQUIRE(wkt2 == wkt3);
|
|
|
|
// compare spatially (NOTE: GeometryCollection comparison also enforces strict order)
|
|
|
|
}
|
2016-03-16 15:06:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|