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

Commit 3ce0fb9

Browse files
committed
implement getAnnotationsInBoundingBox()
1 parent 16dd454 commit 3ce0fb9

2 files changed

Lines changed: 55 additions & 19 deletions

File tree

‎include/mbgl/map/annotation.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AnnotationManager : private util::noncopyable {
4646
Map& map;
4747
std::string defaultPointAnnotationSymbol = "marker-red";
4848
std::map<uint32_t, std::unique_ptr<Annotation>> annotations;
49-
std::map<Tile::ID, std::unique_ptr<LiveTile>> annotationTiles;
49+
std::map<Tile::ID, std::pair<std::vector<uint32_t>, std::unique_ptr<LiveTile>>> annotationTiles;
5050
std::unique_ptr<LiveTile> nullTile;
5151
uint32_t nextID_ = 0;
5252
};

‎src/mbgl/map/annotation.cpp‎

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <mbgl/map/annotation.hpp>
22
#include <mbgl/util/ptr.hpp>
33

4+
#include <algorithm>
45
#include <memory>
56

67
using namespace mbgl;
@@ -39,13 +40,9 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPo
3940

4041
uint16_t extent = 4096;
4142

42-
std::vector<uint32_t> result;
43-
result.reserve(points.size());
44-
43+
std::vector<uint32_t> annotationIDs(points.size());
4544
std::vector<Tile::ID> affectedTiles;
4645

47-
std::vector<std::weak_ptr<const LiveTileFeature>> pointFeatures;
48-
4946
for (uint32_t i = 0; i < points.size(); ++i) {
5047
uint32_t annotationID = nextID();
5148

@@ -63,6 +60,7 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPo
6360
for (int8_t z = maxZoom; z >= 0; z--) {
6461
affectedTiles.emplace_back(z, x, y);
6562
Tile::ID tileID = affectedTiles.back();
63+
6664
Coordinate coordinate(extent * (p.x * z2 - x), extent * (p.y * z2 - y));
6765

6866
GeometryCollection geometries({{ {{ coordinate }} }});
@@ -75,29 +73,34 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPo
7573

7674
auto tile_it = annotationTiles.find(tileID);
7775
if (tile_it != annotationTiles.end()) {
78-
auto layer = tile_it->second->getLayer(util::ANNOTATIONS_POINTS_LAYER_ID);
76+
// get point layer & add feature
77+
auto layer = tile_it->second.second->getLayer(util::ANNOTATIONS_POINTS_LAYER_ID);
7978
auto liveLayer = std::static_pointer_cast<LiveTileLayer>(layer);
8079
liveLayer->addFeature(feature);
80+
// record annotation association with tile
81+
tile_it->second.first.push_back(annotationID);
8182
} else {
83+
// create point layer & add feature
8284
util::ptr<LiveTileLayer> layer = std::make_shared<LiveTileLayer>();
8385
layer->addFeature(feature);
84-
auto tile_pos = annotationTiles.emplace(tileID, util::make_unique<LiveTile>());
85-
tile_pos.first->second->addLayer(util::ANNOTATIONS_POINTS_LAYER_ID, layer);
86+
// create tile & record annotation association
87+
auto tile_pos = annotationTiles.emplace(tileID, std::make_pair(std::vector<uint32_t>({ annotationID }), util::make_unique<LiveTile>()));
88+
// add point layer to tile
89+
tile_pos.first->second.second->addLayer(util::ANNOTATIONS_POINTS_LAYER_ID, layer);
8690
}
8791

88-
pointFeatures.clear();
89-
pointFeatures.emplace_back(feature);
90-
anno_it.first->second->tileFeatures.emplace(tileID, pointFeatures);
92+
// record annotation association with tile feature
93+
anno_it.first->second->tileFeatures.emplace(tileID, std::vector<std::weak_ptr<const LiveTileFeature>>({ feature }));
9194

9295
z2 /= 2;
9396
x /= 2;
9497
y /= 2;
9598
}
9699

97-
result.push_back(annotationID);
100+
annotationIDs.push_back(annotationID);
98101
}
99102

100-
return std::make_pair(affectedTiles, result);
103+
return std::make_pair(affectedTiles, annotationIDs);
101104
}
102105

103106
std::vector<uint32_t> AnnotationManager::addShapeAnnotations(std::vector<std::vector<AnnotationSegment>> shapes) {
@@ -125,7 +128,7 @@ std::vector<Tile::ID> AnnotationManager::removeAnnotations(std::vector<uint32_t>
125128
for (auto& tile_it : annotationTiles) {
126129
auto features_it = annotation->tileFeatures.find(tile_it.first);
127130
if (features_it != annotation->tileFeatures.end()) {
128-
auto layer = tile_it.second->getLayer(util::ANNOTATIONS_POINTS_LAYER_ID);
131+
auto layer = tile_it.second.second->getLayer(util::ANNOTATIONS_POINTS_LAYER_ID);
129132
auto liveLayer = std::static_pointer_cast<LiveTileLayer>(layer);
130133
auto& features = features_it->second;
131134
liveLayer->removeFeature(features[0]);
@@ -139,9 +142,42 @@ std::vector<Tile::ID> AnnotationManager::removeAnnotations(std::vector<uint32_t>
139142
return affectedTiles;
140143
}
141144

142-
std::vector<uint32_t> AnnotationManager::getAnnotationsInBoundingBox(BoundingBox bbox) const {
143-
printf("%f, %f\n", bbox.sw.latitude, bbox.sw.longitude);
144-
return {};
145+
std::vector<uint32_t> AnnotationManager::getAnnotationsInBoundingBox(BoundingBox queryBbox) const {
146+
uint8_t z = map.getMaxZoom();
147+
uint32_t z2 = 1 << z;
148+
vec2<double> swPoint = projectPoint(queryBbox.sw);
149+
vec2<double> nePoint = projectPoint(queryBbox.ne);
150+
151+
// tiles number y from top down
152+
Tile::ID nwTile(z, swPoint.x * z2, nePoint.y * z2);
153+
Tile::ID seTile(z, nePoint.x * z2, swPoint.y * z2);
154+
155+
std::vector<uint32_t> matchingAnnotations;
156+
157+
for (auto& tile : annotationTiles) {
158+
Tile::ID id = tile.first;
159+
if (id.z == z) {
160+
if (id.x >= nwTile.x && id.x <= seTile.x && id.y >= nwTile.y && id.y <= seTile.y) {
161+
if (id.x > nwTile.x && id.x < seTile.x && id.y > nwTile.y && id.y < seTile.y) {
162+
// trivial accept; grab all of the tile's annotations
163+
std::copy(tile.second.first.begin(), tile.second.first.end(), std::back_inserter(matchingAnnotations));
164+
} else {
165+
// check tile's annotations' bounding boxes
166+
std::copy_if(tile.second.first.begin(), tile.second.first.end(),
167+
std::back_inserter(matchingAnnotations), [&](const uint32_t annotationID) -> bool {
168+
printf("checking annotation %i\n", annotationID);
169+
BoundingBox annoBbox = this->annotations.find(annotationID)->second->getBoundingBox();
170+
return (annoBbox.sw.latitude >= queryBbox.sw.latitude &&
171+
annoBbox.ne.latitude <= queryBbox.ne.latitude &&
172+
annoBbox.sw.longitude >= queryBbox.sw.longitude &&
173+
annoBbox.ne.longitude <= queryBbox.ne.longitude);
174+
});
175+
}
176+
}
177+
}
178+
}
179+
180+
return matchingAnnotations;
145181
}
146182

147183
BoundingBox AnnotationManager::getBoundingBoxForAnnotations(std::vector<uint32_t> ids) const {
@@ -165,7 +201,7 @@ const std::unique_ptr<LiveTile>& AnnotationManager::getTile(Tile::ID const& id)
165201

166202
auto tile_it = annotationTiles.find(id);
167203
if (tile_it != annotationTiles.end()) {
168-
return tile_it->second;
204+
return tile_it->second.second;
169205
}
170206
return nullTile;
171207
}

0 commit comments

Comments
 (0)