Skip to content

Commit

Permalink
Merge pull request #568 from openmobilemaps/bugfix/geojson-tiling
Browse files Browse the repository at this point in the history
min/max for geojson tiling
  • Loading branch information
maurhofer-ubique authored Jan 22, 2024
2 parents 1d1fbee + 6146851 commit 123c264
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
1 change: 1 addition & 0 deletions shared/public/GeoJsonTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class GeoJSONVTInterface {
virtual const GeoJSONTileInterface& getTile(const uint8_t z, const uint32_t x_, const uint32_t y) = 0;
virtual bool isLoaded() = 0;
virtual void waitIfNotLoaded(std::shared_ptr<::djinni::Promise<std::shared_ptr<DataLoaderResult>>> promise) = 0;
virtual uint8_t getMinZoom() = 0;
virtual uint8_t getMaxZoom() = 0;
virtual void reload(const std::vector<std::shared_ptr<::LoaderInterface>> &loaders) = 0;
virtual void reload(const std::shared_ptr<GeoJson> &geoJson) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,19 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
tileJsons[key] = val;
} else if (type == "geojson") {
nlohmann::json geojson;
Options options;

if (val["minzoom"].is_number_integer()) {
options.minZoom = val["minzoom"].get<uint8_t>();
}
if (val["maxzoom"].is_number_integer()) {
options.maxZoom = val["maxzoom"].get<uint8_t>();
}
if (val["data"].is_string()) {
geojsonSources[key] = GeoJsonVTFactory::getGeoJsonVt(key, replaceUrlParams(val["data"].get<std::string>(), sourceUrlParams), loaders, localDataProvider);
geojsonSources[key] = GeoJsonVTFactory::getGeoJsonVt(key, replaceUrlParams(val["data"].get<std::string>(), sourceUrlParams), loaders, localDataProvider, options);
} else {
assert(val["data"].is_object());
geojsonSources[key] = GeoJsonVTFactory::getGeoJsonVt(GeoJsonParser::getGeoJson(val["data"]));
geojsonSources[key] = GeoJsonVTFactory::getGeoJsonVt(GeoJsonParser::getGeoJson(val["data"]), options);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions shared/src/map/layers/tiled/vector/geojson/GeoJsonVTFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

class GeoJsonVTFactory {
public:
static std::shared_ptr<GeoJSONVTInterface> getGeoJsonVt(const std::shared_ptr<GeoJson> &geoJson) {
return std::static_pointer_cast<GeoJSONVTInterface>(std::make_shared<GeoJSONVT>(geoJson));
static std::shared_ptr<GeoJSONVTInterface> getGeoJsonVt(const std::shared_ptr<GeoJson> &geoJson,
const Options& options = Options()) {
return std::static_pointer_cast<GeoJSONVTInterface>(std::make_shared<GeoJSONVT>(geoJson, options));
}

static std::shared_ptr<GeoJSONVTInterface> getGeoJsonVt(const std::string &sourceName, const std::string &geoJsonUrl, const std::vector<std::shared_ptr<::LoaderInterface>> &loaders, const std::shared_ptr<Tiled2dMapVectorLayerLocalDataProviderInterface> &localDataProvider) {
std::shared_ptr<GeoJSONVT> vt = std::make_shared<GeoJSONVT>(sourceName, geoJsonUrl, loaders, localDataProvider);
static std::shared_ptr<GeoJSONVTInterface> getGeoJsonVt(const std::string &sourceName, const std::string &geoJsonUrl, const std::vector<std::shared_ptr<::LoaderInterface>> &loaders, const std::shared_ptr<Tiled2dMapVectorLayerLocalDataProviderInterface> &localDataProvider,
const Options& options = Options()) {
std::shared_ptr<GeoJSONVT> vt = std::make_shared<GeoJSONVT>(sourceName, geoJsonUrl, loaders, localDataProvider, options);
vt->load();
return vt;
}
Expand Down
24 changes: 19 additions & 5 deletions shared/src/map/layers/tiled/vector/geojson/geojsonvt/geojsonvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ struct TileOptions {
};

struct Options : TileOptions {
// min zoom to will be visible
uint8_t minZoom = 0;

// max zoom to preserve detail on
uint8_t maxZoom = 18;

Expand Down Expand Up @@ -53,7 +56,7 @@ class GeoJSONVT: public GeoJSONVTInterface, public std::enable_shared_from_this<
// If the GeoJSON contains only points, there is no need to split it into smaller tiles,
// as there are no opportunities for simplification, merging, or meaningful point reduction.
if (geoJson->hasOnlyPoints) {
options.maxZoom = 0;
options.maxZoom = options.minZoom;
}


Expand Down Expand Up @@ -114,7 +117,7 @@ class GeoJSONVT: public GeoJSONVTInterface, public std::enable_shared_from_this<
// If the GeoJSON contains only points, there is no need to split it into smaller tiles,
// as there are no opportunities for simplification, merging, or meaningful point reduction.
if (geoJson->hasOnlyPoints) {
self->options.maxZoom = 0;
self->options.maxZoom = self->options.minZoom;
} else {
self->options.maxZoom = 18;
}
Expand Down Expand Up @@ -150,6 +153,10 @@ class GeoJSONVT: public GeoJSONVTInterface, public std::enable_shared_from_this<
}
}

uint8_t getMinZoom() override {
return options.minZoom;
}

uint8_t getMaxZoom() override {
return options.maxZoom;
}
Expand All @@ -167,7 +174,7 @@ class GeoJSONVT: public GeoJSONVTInterface, public std::enable_shared_from_this<
// If the GeoJSON contains only points, there is no need to split it into smaller tiles,
// as there are no opportunities for simplification, merging, or meaningful point reduction.
if (geoJson->hasOnlyPoints) {
options.maxZoom = 0;
options.maxZoom = options.minZoom;
} else {
options.maxZoom = 18;
}
Expand Down Expand Up @@ -271,9 +278,11 @@ class GeoJSONVT: public GeoJSONVTInterface, public std::enable_shared_from_this<

auto& tile = it->second;

if (geometries.empty())
if (geometries.empty()) {
tiles.erase(it);
return;

}

//if it's the first-pass tiling
if (cz == 0u) {
// stop tiling if we reached max zoom, or if the tile is too simple
Expand Down Expand Up @@ -317,6 +326,11 @@ class GeoJSONVT: public GeoJSONVTInterface, public std::enable_shared_from_this<

// if we sliced further down, no need to keep source geometry
tile.source_features.clear();

if (z < options.minZoom) {
// if z smaller than min zoom, no need to keep tile
tiles.erase(it);
}
}

void resolveAllWaitingPromises() {
Expand Down

0 comments on commit 123c264

Please sign in to comment.