From 1b78d42069003bcb1a55fbf6c3182f7b49310c7c Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Fri, 3 Aug 2018 18:12:16 +0300 Subject: [PATCH] Add a line metrics test for a single tile This patch also fixes buffer rounding in `geoJSONToTile()`. Based on patch from @lbud (Lauren Budorick). --- include/mapbox/geojsonvt.hpp | 2 +- test/fixtures/single-tile-tiles.json | 1 + test/test.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/single-tile-tiles.json diff --git a/include/mapbox/geojsonvt.hpp b/include/mapbox/geojsonvt.hpp index 1c59066..3f7b418 100644 --- a/include/mapbox/geojsonvt.hpp +++ b/include/mapbox/geojsonvt.hpp @@ -78,7 +78,7 @@ inline const Tile geoJSONToTile(const geojson& geojson_, features = detail::wrap(features, double(options.buffer) / options.extent, options.lineMetrics); } if (clip || options.lineMetrics) { - const double p = options.buffer / options.extent; + const double p = double(options.buffer) / options.extent; const auto left = detail::clip<0>(features, (x - p) / z2, (x + 1 + p) / z2, -1, 2, options.lineMetrics); features = detail::clip<1>(left, (y - p) / z2, (y + 1 + p) / z2, -1, 2, options.lineMetrics); diff --git a/test/fixtures/single-tile-tiles.json b/test/fixtures/single-tile-tiles.json new file mode 100644 index 0000000..f327302 --- /dev/null +++ b/test/fixtures/single-tile-tiles.json @@ -0,0 +1 @@ +[{"geometry":[[[1302,1800],[1302,1734],[1392,1728],[1472,1922],[1487,1915],[1558,1913],[2435,1898],[2553,1866],[3317,1868],[3336,1875],[3343,1885],[3361,1924],[3357,1946],[3382,1935],[3411,1941],[3434,1939],[3460,1926],[3477,1907],[3510,1908],[3954,2108],[4037,2177],[4064,2181],[4081,2177],[4160,2199]]],"type":2,"tags":{"mapbox_clip_start":0,"mapbox_clip_end":0.4210252774113827}}] diff --git a/test/test.cpp b/test/test.cpp index d9f3204..0b1dffd 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -412,3 +412,30 @@ TEST(geoJSONToTile, Clips) { auto name = (props.find("name")->second).get(); ASSERT_EQ(name, std::string("District of Columbia")); } + +TEST(geoJSONToTile, Metrics) { + auto geojson = mapbox::geojson::parse(loadFile("test/fixtures/single-tile.json")); + TileOptions options; + options.lineMetrics = true; + options.buffer = 64; + options.tolerance = 3; + const double kEpsilon = 1e-5; + + const Tile& tileLeft = mapbox::geojsonvt::geoJSONToTile(geojson, 13, 2342, 3133, options); + ASSERT_EQ(tileLeft.features.size(), 1u); + + const Tile& tileRight = mapbox::geojsonvt::geoJSONToTile(geojson, 13, 2343, 3133, options); + ASSERT_EQ(tileRight.features.size(), 1u); + + auto& leftProps = tileLeft.features.at(0).properties; + double leftClipStart = (leftProps.find("mapbox_clip_start")->second).get(); + EXPECT_DOUBLE_EQ(leftClipStart, 0.0); + double leftClipEnd = (leftProps.find("mapbox_clip_end")->second).get(); + EXPECT_NEAR(leftClipEnd, 0.42103, kEpsilon); + + auto& rightProps = tileRight.features.at(0).properties; + double rightClipStart = (rightProps.find("mapbox_clip_start")->second).get(); + EXPECT_NEAR(rightClipStart, 0.40349, kEpsilon); + double rightClipEnd = (rightProps.find("mapbox_clip_end")->second).get(); + EXPECT_DOUBLE_EQ(rightClipEnd, 1.0); +}