Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a polygon clipping edge case #83

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/mapbox/geojsonvt/clip.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ class clipper {

slice = newSlice(line);

} else if (bk >= k1) { // ---|--> |
} else if (bk > k1) { // ---|--> |
t = calc_progress<I>(a, b, k1);
slice.push_back(intersect<I>(a, b, k1, t));
if (lineMetrics) slice.segStart = lineLen + segLen * t;

if (i == len - 2)
slice.push_back(b); // last point
}
} else if (ak >= k2) {
} else if (ak > k2) {
if (bk < k1) { // <--|-----|---
t = calc_progress<I>(a, b, k2);
slice.push_back(intersect<I>(a, b, k2, t));
Expand Down Expand Up @@ -202,7 +202,7 @@ class clipper {
const double bk = get<I>(b);

if (ak < k1) {
if (bk >= k1) {
if (bk > k1) {
// ---|--> |
slice.push_back(intersect<I>(a, b, k1, calc_progress<I>(a, b, k1)));
if (bk > k2)
Expand All @@ -211,7 +211,7 @@ class clipper {
else if (i == len - 2)
slice.push_back(b); // last point
}
} else if (ak >= k2) {
} else if (ak > k2) {
if (bk < k2) { // | <--|---
slice.push_back(intersect<I>(a, b, k2, calc_progress<I>(a, b, k2)));
if (bk < k1) // <--|-----|---
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/polygon-bug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "Polygon",
"coordinates": [[
[42.1875, 57.32652122521708],
[47.8125, 57.32652122521708],
[47.8125, 54.16243396806781],
[42.1875, 54.16243396806781],
[42.1875, 57.32652122521708]
]]
}
21 changes: 21 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ TEST(GetTile, AntimeridianTriangle) {
}
}

TEST(GetTile, PolygonClippingBug) {
const auto geojson = mapbox::geojson::parse(loadFile("test/fixtures/polygon-bug.json"));

Options options;
options.buffer = 1024;

GeoJSONVT index{ geojson, options };

auto tile = index.getTile(5, 19, 9);
ASSERT_EQ(tile.features.size(), 1);
ASSERT_EQ(tile.num_points, 5);

const mapbox::geometry::polygon<int16_t> expected{
{ {3072, 3072}, {5120, 3072}, {5120, 5120}, {3072, 5120}, {3072, 3072} }
};

const auto actual = tile.features[0].geometry.get<mapbox::geometry::polygon<int16_t>>();

ASSERT_EQ(actual, expected);
}

TEST(GetTile, Projection) {
const auto geojson = mapbox::geojson::parse(loadFile("test/fixtures/linestring.json"));

Expand Down