Skip to content

Commit

Permalink
add methods getTiles and setTiles to mimick maplibre-gl-js interface (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbu42 authored Jan 5, 2024
1 parent 210eb45 commit fe728eb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/mbgl/style/sources/vector_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class VectorSource final : public Source {

void loadDescription(FileSource&) final;

/// @brief Gets the tile urls for this vector source.
/// @return List of tile urls.
const std::vector<std::string> getTiles() const;

/// @brief Sets the tile urls for this vector source.
/// @param tiles List of tile urls.
void setTiles(const std::vector<std::string>& tiles);

bool supportsLayerType(const mbgl::style::LayerTypeInfo*) const override;

mapbox::base::WeakPtr<Source> makeWeakPtr() override { return weakFactory.makeWeakPtr(); }
Expand Down
19 changes: 19 additions & 0 deletions src/mbgl/style/sources/vector_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,25 @@ void VectorSource::loadDescription(FileSource& fileSource) {
});
}

const std::vector<std::string> VectorSource::getTiles() const {
auto tileset = impl().tileset;
if (tileset.has_value()) {
return tileset->tiles;
} else {
return {};
}
}

void VectorSource::setTiles(const std::vector<std::string>& tiles) {
auto& tileset = impl().tileset;
if (!tileset.has_value()) return;
if (tileset->tiles == tiles) return;
Tileset newtileset(*tileset);
newtileset.tiles = tiles;
baseImpl = makeMutable<Impl>(impl(), newtileset);
observer->onSourceChanged(*this);
}

bool VectorSource::supportsLayerType(const mbgl::style::LayerTypeInfo* info) const {
return mbgl::underlying_type(Tile::Kind::Geometry) == mbgl::underlying_type(info->tileKind);
}
Expand Down
44 changes: 44 additions & 0 deletions test/style/source.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,50 @@ TEST(Source, RenderTileSetSourceUpdate) {
renderSource->update(uninitialized.baseImpl, layers, true, true, test.tileParameters());
}

TEST(Source, VectorSourceSetTiles) {
SourceTest test;
test.styleObserver.sourceChanged = [&](Source& source) {
EXPECT_EQ(source.as<VectorSource>()->getTiles(), std::vector<std::string>{"new"});
test.end();
};

VectorSource source("source", Tileset{{"url"}});
source.setObserver(&test.styleObserver);
source.setTiles({"unused"}); // make sure early setTiles does not segfault
source.loadDescription(*test.fileSource);
EXPECT_EQ(source.as<VectorSource>()->getTiles(), std::vector<std::string>{"url"});
source.setTiles({"new"});
test.run();
}

TEST(Source, VectorSourceUrlSetTiles) {
SourceTest test;
test.styleObserver.sourceLoaded = [&](Source& source) {
EXPECT_EQ(source.as<VectorSource>()->getTiles(), std::vector<std::string>{"tiles"});
source.as<VectorSource>()->setTiles({"new"});
};
int count = 0;
test.styleObserver.sourceChanged = [&](Source& source) {
if (count++) {
EXPECT_EQ(source.as<VectorSource>()->getTiles(), std::vector<std::string>{"new"});
test.end();
}
};

test.fileSource->sourceResponse = [&](const Resource& resource) {
EXPECT_EQ("url", resource.url);
Response response;
response.data = std::make_unique<std::string>(R"({"tiles": ["tiles"]})");
return response;
};

VectorSource source("source", "url");
source.setObserver(&test.styleObserver);
source.setTiles({"unused"});
source.loadDescription(*test.fileSource);
test.run();
}

TEST(Source, GeoJSONSourceTilesAfterDataReset) {
SourceTest test;
GeoJSONSource source("source");
Expand Down

0 comments on commit fe728eb

Please sign in to comment.