Skip to content

WIP: style json parser tests #720

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
maxZoom = sourceDesc->maxZoom;
}
}
for (const auto &[identifier, source] : geojsonSources) {
if (identifier == val["source"]) {
minZoom = source->getMinZoom();
maxZoom = source->getMaxZoom();
}
}

float selectionSizeFactor = 1.0;
if (!val["metadata"].is_null()) {
Expand Down Expand Up @@ -490,6 +496,12 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
maxZoom = sourceDesc->maxZoom;
}
}
for (const auto &[identifier, source] : geojsonSources) {
if (identifier == val["source"]) {
minZoom = source->getMinZoom();
maxZoom = source->getMaxZoom();
}
}

auto layerDesc = std::make_shared<SymbolVectorLayerDescription>(val["id"],
val["source"],
Expand Down Expand Up @@ -523,6 +535,12 @@ Tiled2dMapVectorLayerParserResult Tiled2dMapVectorLayerParserHelper::parseStyleJ
maxZoom = sourceDesc->maxZoom;
}
}
for (const auto &[identifier, source] : geojsonSources) {
if (identifier == val["source"]) {
minZoom = source->getMinZoom();
maxZoom = source->getMaxZoom();
}
}

auto layerDesc = std::make_shared<PolygonVectorLayerDescription>(val["id"],
val["source"],
Expand Down
2 changes: 2 additions & 0 deletions shared/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ add_executable(tests
"TestGeometryHandler.cpp"
"TestValueEvaluate.cpp"
"TestVectorSet.cpp"
"TestStyleParser.cpp"
"helper/TestData.cpp"
"helper/TestLocalDataProvider.h"
)
# Use mapscore _private_ include to allow testing functionality declared in internal headers.
target_include_directories(tests PRIVATE
Expand Down
78 changes: 78 additions & 0 deletions shared/test/TestStyleParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "DataRef.hpp"
#include "GeoJsonTypes.h"
#include "Tiled2dMapVectorLayerParserHelper.h"
#include "VectorLayerDescription.h"
#include "geojsonvt.hpp"
#include "helper/TestData.h"
#include "helper/TestLocalDataProvider.h"
#include "helper/TestScheduler.h"

#include <catch2/benchmark/catch_benchmark.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <random>

class TestGeoJSONTileDelegate : public GeoJSONTileDelegate, public ActorObject {
public:
bool didLoadCalled = false;
bool failedToLoadCalled = false;

void didLoad(uint8_t maxZoom) override {
didLoadCalled = true;
}

void failedToLoad() override {
failedToLoadCalled = true;
}
};


TEST_CASE("TestStyleParser", "[GeoJson inline]") {
auto jsonString = TestData::readFileToString("style/geojson_style_inline.json");
auto result = Tiled2dMapVectorLayerParserHelper::parseStyleJsonFromString("test", jsonString, nullptr, {}, {});
REQUIRE(result.mapDescription != nullptr);
REQUIRE(!result.mapDescription->geoJsonSources.empty());

std::shared_ptr<GeoJSONVTInterface> geojsonSource = result.mapDescription->geoJsonSources.begin()->second;
REQUIRE(geojsonSource->getMinZoom() == 0);
REQUIRE(geojsonSource->getMaxZoom() == 0);

REQUIRE(result.mapDescription->layers[0]->sourceMinZoom == 0);
REQUIRE(result.mapDescription->layers[0]->sourceMaxZoom == 0);
REQUIRE_NOTHROW(geojsonSource->getTile(0,0,0));
REQUIRE_THROWS(geojsonSource->getTile(6,33,22));
}

TEST_CASE("TestStyleParser", "[GeoJson local provider]") {
auto jsonString = TestData::readFileToString("style/geojson_style_provider.json");
auto provider = std::make_shared<TestLocalDataProvider>(std::unordered_map<std::string, std::string>{
{"wsource", "geojson.geojson"}
});
auto result = Tiled2dMapVectorLayerParserHelper::parseStyleJsonFromString("test", jsonString, provider, {}, {});
REQUIRE(result.mapDescription != nullptr);
REQUIRE(!result.mapDescription->geoJsonSources.empty());

std::shared_ptr<GeoJSONVTInterface> geojsonSource = result.mapDescription->geoJsonSources.begin()->second;

auto scheduler = std::make_shared<TestScheduler>();
auto delegate = Actor<TestGeoJSONTileDelegate>(std::make_shared<Mailbox>(scheduler), std::make_shared<TestGeoJSONTileDelegate>());

geojsonSource->setDelegate(delegate.weakActor<GeoJSONTileDelegate>());

REQUIRE(!delegate.unsafe()->didLoadCalled);

auto promise = std::make_shared<::djinni::Promise<std::shared_ptr<DataLoaderResult>>>();
geojsonSource->waitIfNotLoaded(promise);
promise->getFuture().wait();

scheduler->drain();

REQUIRE(delegate.unsafe()->didLoadCalled);

REQUIRE(geojsonSource->getMinZoom() == 0);
REQUIRE(geojsonSource->getMaxZoom() == 25);

const auto &tile = geojsonSource->getTile(6,33,22);

REQUIRE(!tile.getFeatures().empty());
}
1 change: 1 addition & 0 deletions shared/test/data/style/geojson.geojson

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions shared/test/data/style/geojson_style_inline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"version": 8,
"name": "s",
"sources": {
"wsource": {
"type": "geojson",
"minzoom": 0,
"maxzoom": 25,
"data": {
"type": "Point",
"coordinates": [102.0, 0.5]
}
}
},
"sprite": "localdata-sprites",
"layers": [
{
"id": "w",
"type": "fill",
"metadata": {
"blend-mode": "multiply"
},
"minzoom": 0,
"maxzoom": 6,
"source": "wsource",
"paint": {
"fill-color": "rgb(202, 154, 255)"
}
}
]
}
28 changes: 28 additions & 0 deletions shared/test/data/style/geojson_style_provider.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"version": 8,
"name": "s",
"sources": {
"wsource": {
"type": "geojson",
"minzoom": 0,
"maxzoom": 25,
"data": "localdata-url"
}
},
"sprite": "localdata-sprites",
"layers": [
{
"id": "w",
"type": "fill",
"metadata": {
"blend-mode": "multiply"
},
"minzoom": 0,
"maxzoom": 6,
"source": "wsource",
"paint": {
"fill-color": "rgb(202, 154, 255)"
}
}
]
}
1 change: 1 addition & 0 deletions shared/test/helper/TestData.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: MPL-2.0
*/

#pragma once
#include <vector>
#include <string>

Expand Down
41 changes: 41 additions & 0 deletions shared/test/helper/TestLocalDataProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include "TestData.h"
#include "Tiled2dMapVectorLayerLocalDataProviderInterface.h"

#include <string>
#include <utility>

class TestLocalDataProvider: public Tiled2dMapVectorLayerLocalDataProviderInterface {
public:
explicit TestLocalDataProvider(const std::unordered_map<std::string, std::string> &&geojsonMapping)
: geojsonMapping(geojsonMapping) {}

djinni::Future<DataLoaderResult> loadGeojson(const std::string &sourceName, const std::string &url) override {
auto promise = ::djinni::Promise<DataLoaderResult>();
auto it = geojsonMapping.find(sourceName);
if (it != geojsonMapping.end()) {
auto data = TestData::readFileToBuffer(("style/" + it->second).c_str());
std::vector<uint8_t> uint8Vec(data.begin(), data.end());
promise.setValue(DataLoaderResult(::djinni::DataRef(uint8Vec), std::nullopt, LoaderStatus::OK, std::nullopt));
} else {
throw std::runtime_error("Failed to load geojson from file");
}
return promise.getFuture();
}
std::optional<std::string> getStyleJson() override {
return std::nullopt;
}
djinni::Future<TextureLoaderResult> loadSpriteAsync(int32_t scale) override {
auto promise = ::djinni::Promise<TextureLoaderResult>();
promise.setValue(TextureLoaderResult(nullptr, std::nullopt, LoaderStatus::ERROR_404, std::nullopt));
return promise.getFuture();
}
djinni::Future<DataLoaderResult> loadSpriteJsonAsync(int32_t scale) override {
auto promise = ::djinni::Promise<DataLoaderResult>();
promise.setValue(DataLoaderResult(std::nullopt, std::nullopt, LoaderStatus::ERROR_404, std::nullopt));
return promise.getFuture();
}

private:
std::unordered_map<std::string, std::string> geojsonMapping;
};
Loading