Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 848f48a

Browse files
committed
[core] Fix within meridian issue
1 parent 9009089 commit 848f48a

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

src/mbgl/style/expression/within.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,41 @@ MultiPolygon<int64_t> getTilePolygons(const Feature::geometry_type& polygonGeoSe
6767
[](const auto&) { return MultiPolygon<int64_t>(); });
6868
}
6969

70+
void updatePoint(Point<int64_t>& p, WithinBBox& bbox, const WithinBBox& polyBBox, const int64_t worldSize) {
71+
if (p.x <= polyBBox[0] || p.x >= polyBBox[2]) {
72+
int64_t shift = 0;
73+
shift = (p.x - polyBBox[0] > worldSize / 2) ? -worldSize : (polyBBox[0] - p.x > worldSize / 2) ? worldSize : 0;
74+
if (shift == 0) {
75+
shift =
76+
(p.x - polyBBox[2] > worldSize / 2) ? -worldSize : (polyBBox[2] - p.x > worldSize / 2) ? worldSize : 0;
77+
}
78+
p.x += shift;
79+
}
80+
updateBBox(bbox, p);
81+
}
82+
7083
MultiPoint<int64_t> getTilePoints(const GeometryCoordinates& points,
7184
const mbgl::CanonicalTileID& canonical,
72-
WithinBBox& bbox) {
85+
WithinBBox& bbox,
86+
const WithinBBox& polyBBox) {
7387
const int64_t xShift = util::EXTENT * canonical.x;
7488
const int64_t yShift = util::EXTENT * canonical.y;
89+
const auto worldSize = util::EXTENT * std::pow(2, canonical.z);
7590
MultiPoint<int64_t> results;
7691
results.reserve(points.size());
7792
for (const auto& p : points) {
78-
const auto point = Point<int64_t>(p.x + xShift, p.y + yShift);
79-
updateBBox(bbox, point);
93+
auto point = Point<int64_t>(p.x + xShift, p.y + yShift);
94+
updatePoint(point, bbox, polyBBox, worldSize);
8095
results.push_back(point);
8196
}
97+
8298
return results;
8399
}
84100

85101
MultiLineString<int64_t> getTileLines(const GeometryCollection& lines,
86102
const mbgl::CanonicalTileID& canonical,
87-
WithinBBox& bbox) {
103+
WithinBBox& bbox,
104+
const WithinBBox& polyBBox) {
88105
const int64_t xShift = util::EXTENT * canonical.x;
89106
const int64_t yShift = util::EXTENT * canonical.y;
90107
MultiLineString<int64_t> results;
@@ -99,6 +116,16 @@ MultiLineString<int64_t> getTileLines(const GeometryCollection& lines,
99116
}
100117
results.push_back(std::move(lineString));
101118
}
119+
120+
const auto worldSize = util::EXTENT * std::pow(2, canonical.z);
121+
if (bbox[2] - bbox[0] <= worldSize / 2) {
122+
bbox = DefaultBBox;
123+
for (auto& line : results) {
124+
for (auto& p : line) {
125+
updatePoint(p, bbox, polyBBox, worldSize);
126+
}
127+
}
128+
}
102129
return results;
103130
}
104131

@@ -113,15 +140,15 @@ bool featureWithinPolygons(const GeometryTileFeature& feature,
113140
case FeatureType::Point: {
114141
assert(!geometries.empty());
115142
WithinBBox pointBBox = DefaultBBox;
116-
MultiPoint<int64_t> points = getTilePoints(geometries.at(0), canonical, pointBBox);
143+
MultiPoint<int64_t> points = getTilePoints(geometries.at(0), canonical, pointBBox, polyBBox);
117144
if (!boxWithinBox(pointBBox, polyBBox)) return false;
118145

119146
return std::all_of(
120147
points.begin(), points.end(), [&polygons](const auto& p) { return pointWithinPolygons(p, polygons); });
121148
}
122149
case FeatureType::LineString: {
123150
WithinBBox lineBBox = DefaultBBox;
124-
MultiLineString<int64_t> multiLineString = getTileLines(geometries, canonical, lineBBox);
151+
MultiLineString<int64_t> multiLineString = getTileLines(geometries, canonical, lineBBox, polyBBox);
125152
if (!boxWithinBox(lineBBox, polyBBox)) return false;
126153

127154
return std::all_of(multiLineString.begin(), multiLineString.end(), [&polygons](const auto& line) {

src/mbgl/util/geometry_within.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ bool lineIntersectLine(const Point<int64_t>& a,
4747
y2 = p2.y - q1.y;
4848
x3 = q2.x - q1.x;
4949
y3 = q2.y - q1.y;
50-
if ((x1 * y3 - x3 * y1) * (x2 * y3 - x3 * y2) < 0) return true;
50+
auto ret1 = (x1 * y3 - x3 * y1);
51+
auto ret2 = (x2 * y3 - x3 * y2);
52+
if ((ret1 > 0 && ret2 < 0) || (ret1 < 0 && ret2 > 0)) {
53+
return true;
54+
}
5155
return false;
5256
};
5357

0 commit comments

Comments
 (0)