[core] add support for mapzen terrarium (#11154)
* add support for mapzen terrarium * Encoding --> DEMEncoding, avoid if statement when unpacking elevation values * add Terrarium test * update submodule * remove redundant checks
This commit is contained in:
@@ -14,22 +14,28 @@ namespace mbgl {
|
||||
class Tileset {
|
||||
public:
|
||||
enum class Scheme : bool { XYZ, TMS };
|
||||
enum class DEMEncoding : bool { Mapbox, Terrarium };
|
||||
|
||||
std::vector<std::string> tiles;
|
||||
Range<uint8_t> zoomRange;
|
||||
std::string attribution;
|
||||
Scheme scheme;
|
||||
// DEMEncoding is not supported by the TileJSON spec
|
||||
DEMEncoding encoding;
|
||||
optional<LatLngBounds> bounds;
|
||||
|
||||
|
||||
Tileset(std::vector<std::string> tiles_ = std::vector<std::string>(),
|
||||
Range<uint8_t> zoomRange_ = { 0, util::DEFAULT_MAX_ZOOM },
|
||||
std::string attribution_ = {},
|
||||
Scheme scheme_ = Scheme::XYZ)
|
||||
Scheme scheme_ = Scheme::XYZ,
|
||||
DEMEncoding encoding_ = DEMEncoding::Mapbox)
|
||||
: tiles(std::move(tiles_)),
|
||||
zoomRange(std::move(zoomRange_)),
|
||||
attribution(std::move(attribution_)),
|
||||
scheme(scheme_),
|
||||
bounds() {}
|
||||
encoding(encoding_),
|
||||
bounds() {};
|
||||
|
||||
// TileJSON also includes center and zoom but they are not used by mbgl.
|
||||
|
||||
|
||||
Submodule mapbox-gl-js updated: 920de235a7...f13c86ea35
@@ -11,7 +11,7 @@ import static com.mapbox.mapboxsdk.utils.ColorUtils.rgbaToColor;
|
||||
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
|
||||
|
||||
/**
|
||||
* Client-side hillshading visualization based on DEM data. Currently, the implementation only supports Mapbox Terrain RGB tiles
|
||||
* Client-side hillshading visualization based on DEM data. Currently, the implementation only supports Mapbox Terrain RGB and Mapzen Terrarium tiles.
|
||||
*
|
||||
* @see <a href="https://www.mapbox.com/mapbox-gl-style-spec/#layers-hillshade">The online documentation</a>
|
||||
*/
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
DEMData::DEMData(const PremultipliedImage& _image):
|
||||
DEMData::DEMData(const PremultipliedImage& _image, Tileset::DEMEncoding encoding):
|
||||
dim(_image.size.height),
|
||||
border(std::max<int32_t>(std::ceil(_image.size.height / 2), 1)),
|
||||
stride(dim + 2 * border),
|
||||
@@ -13,13 +13,25 @@ DEMData::DEMData(const PremultipliedImage& _image):
|
||||
throw std::runtime_error("raster-dem tiles must be square.");
|
||||
}
|
||||
|
||||
auto decodeMapbox = [] (const uint8_t r, const uint8_t g, const uint8_t b){
|
||||
// https://www.mapbox.com/help/access-elevation-data/#mapbox-terrain-rgb
|
||||
return (r * 256 * 256 + g * 256 + b)/10 - 10000;
|
||||
};
|
||||
|
||||
auto decodeTerrarium = [] (const uint8_t r, const uint8_t g, const uint8_t b){
|
||||
// https://aws.amazon.com/public-datasets/terrain/
|
||||
return ((r * 256 + g + b / 256) - 32768);
|
||||
};
|
||||
|
||||
auto decodeRGB = encoding == Tileset::DEMEncoding::Terrarium ? decodeTerrarium : decodeMapbox;
|
||||
|
||||
std::memset(image.data.get(), 0, image.bytes());
|
||||
|
||||
for (int32_t y = 0; y < dim; y++) {
|
||||
for (int32_t x = 0; x < dim; x++) {
|
||||
const int32_t i = y * dim + x;
|
||||
const int32_t j = i * 4;
|
||||
set(x, y, (_image.data[j] * 256 * 256 + _image.data[j+1] * 256 + _image.data[j+2])/10 - 10000);
|
||||
set(x, y, decodeRGB(_image.data[j], _image.data[j+1], _image.data[j+2]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <mbgl/math/clamp.hpp>
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/util/tileset.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <array>
|
||||
@@ -12,7 +13,7 @@ namespace mbgl {
|
||||
|
||||
class DEMData {
|
||||
public:
|
||||
DEMData(const PremultipliedImage& image);
|
||||
DEMData(const PremultipliedImage& image, Tileset::DEMEncoding encoding);
|
||||
void backfillBorder(const DEMData& borderTileData, int8_t dx, int8_t dy);
|
||||
|
||||
void set(const int32_t x, const int32_t y, const int32_t value) {
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace mbgl {
|
||||
|
||||
using namespace style;
|
||||
|
||||
HillshadeBucket::HillshadeBucket(PremultipliedImage&& image_): demdata(image_) {
|
||||
HillshadeBucket::HillshadeBucket(PremultipliedImage&& image_, Tileset::DEMEncoding encoding): demdata(image_, encoding) {
|
||||
}
|
||||
|
||||
HillshadeBucket::HillshadeBucket(DEMData&& demdata_) : demdata(std::move(demdata_)) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <mbgl/renderer/bucket.hpp>
|
||||
#include <mbgl/renderer/tile_mask.hpp>
|
||||
#include <mbgl/geometry/dem_data.hpp>
|
||||
#include <mbgl/util/tileset.hpp>
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/util/mat4.hpp>
|
||||
#include <mbgl/util/optional.hpp>
|
||||
@@ -16,8 +17,8 @@ namespace mbgl {
|
||||
|
||||
class HillshadeBucket : public Bucket {
|
||||
public:
|
||||
HillshadeBucket(PremultipliedImage&&);
|
||||
HillshadeBucket(std::shared_ptr<PremultipliedImage>);
|
||||
HillshadeBucket(PremultipliedImage&&, Tileset::DEMEncoding encoding);
|
||||
HillshadeBucket(std::shared_ptr<PremultipliedImage>, Tileset::DEMEncoding encoding);
|
||||
HillshadeBucket(DEMData&&);
|
||||
|
||||
|
||||
|
||||
@@ -40,6 +40,16 @@ optional<Tileset> Converter<Tileset>::operator()(const Convertible& value, Error
|
||||
}
|
||||
}
|
||||
|
||||
auto encodingValue = objectMember(value, "encoding");
|
||||
if (encodingValue) {
|
||||
optional<std::string> encoding = toString(*encodingValue);
|
||||
if (encoding && *encoding == "terrarium") {
|
||||
result.encoding = Tileset::DEMEncoding::Terrarium;
|
||||
} else if (encoding && *encoding != "mapbox") {
|
||||
error = { "invalid raster-dem encoding type - valid types are 'mapbox' and 'terrarium' " };
|
||||
}
|
||||
}
|
||||
|
||||
auto minzoomValue = objectMember(value, "minzoom");
|
||||
if (minzoomValue) {
|
||||
optional<float> minzoom = toNumber(*minzoomValue);
|
||||
|
||||
@@ -21,6 +21,7 @@ RasterDEMTile::RasterDEMTile(const OverscaledTileID& id_,
|
||||
worker(parameters.workerScheduler,
|
||||
ActorRef<RasterDEMTile>(*this, mailbox)) {
|
||||
|
||||
encoding = tileset.encoding;
|
||||
if ( id.canonical.y == 0 ){
|
||||
// this tile doesn't have upper neighboring tiles so marked those as backfilled
|
||||
neighboringTiles = neighboringTiles | DEMTileNeighbors::NoUpper;
|
||||
@@ -47,7 +48,7 @@ void RasterDEMTile::setMetadata(optional<Timestamp> modified_, optional<Timestam
|
||||
void RasterDEMTile::setData(std::shared_ptr<const std::string> data) {
|
||||
pending = true;
|
||||
++correlationID;
|
||||
worker.invoke(&RasterDEMTileWorker::parse, data, correlationID);
|
||||
worker.invoke(&RasterDEMTileWorker::parse, data, correlationID, encoding);
|
||||
}
|
||||
|
||||
void RasterDEMTile::onParsed(std::unique_ptr<HillshadeBucket> result, const uint64_t resultCorrelationID) {
|
||||
|
||||
@@ -94,6 +94,7 @@ private:
|
||||
Actor<RasterDEMTileWorker> worker;
|
||||
|
||||
uint64_t correlationID = 0;
|
||||
Tileset::DEMEncoding encoding;
|
||||
|
||||
// Contains the Bucket object for the tile. Buckets are render
|
||||
// objects and they get added by tile parsing operations.
|
||||
|
||||
@@ -10,14 +10,14 @@ RasterDEMTileWorker::RasterDEMTileWorker(ActorRef<RasterDEMTileWorker>, ActorRef
|
||||
: parent(std::move(parent_)) {
|
||||
}
|
||||
|
||||
void RasterDEMTileWorker::parse(std::shared_ptr<const std::string> data, uint64_t correlationID) {
|
||||
void RasterDEMTileWorker::parse(std::shared_ptr<const std::string> data, uint64_t correlationID, Tileset::DEMEncoding encoding) {
|
||||
if (!data) {
|
||||
parent.invoke(&RasterDEMTile::onParsed, nullptr, correlationID); // No data; empty tile.
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
auto bucket = std::make_unique<HillshadeBucket>(decodeImage(*data));
|
||||
auto bucket = std::make_unique<HillshadeBucket>(decodeImage(*data), encoding);
|
||||
parent.invoke(&RasterDEMTile::onParsed, std::move(bucket), correlationID);
|
||||
} catch (...) {
|
||||
parent.invoke(&RasterDEMTile::onError, std::current_exception(), correlationID);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/actor/actor_ref.hpp>
|
||||
#include <mbgl/util/tileset.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
@@ -13,7 +14,7 @@ class RasterDEMTileWorker {
|
||||
public:
|
||||
RasterDEMTileWorker(ActorRef<RasterDEMTileWorker>, ActorRef<RasterDEMTile>);
|
||||
|
||||
void parse(std::shared_ptr<const std::string> data, uint64_t correlationID);
|
||||
void parse(std::shared_ptr<const std::string> data, uint64_t correlationID, Tileset::DEMEncoding encoding);
|
||||
|
||||
private:
|
||||
ActorRef<RasterDEMTile> parent;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/util/image.hpp>
|
||||
#include <mbgl/util/tileset.hpp>
|
||||
#include <mbgl/geometry/dem_data.hpp>
|
||||
|
||||
using namespace mbgl;
|
||||
@@ -14,30 +15,38 @@ auto fakeImage = [](Size s) {
|
||||
return img;
|
||||
};
|
||||
|
||||
TEST(DEMData, Constructor) {
|
||||
TEST(DEMData, ConstructorMapbox) {
|
||||
PremultipliedImage image = fakeImage({16, 16});
|
||||
DEMData pyramid(image);
|
||||
DEMData demdata(image, Tileset::DEMEncoding::Mapbox);
|
||||
|
||||
EXPECT_EQ(pyramid.dim, 16);
|
||||
EXPECT_EQ(pyramid.border, 8);
|
||||
EXPECT_EQ(pyramid.stride, 32);
|
||||
EXPECT_EQ(pyramid.getImage()->bytes(), size_t(32*32*4));
|
||||
EXPECT_EQ(pyramid.dim, 16);
|
||||
EXPECT_EQ(pyramid.border, 8);
|
||||
EXPECT_EQ(demdata.dim, 16);
|
||||
EXPECT_EQ(demdata.border, 8);
|
||||
EXPECT_EQ(demdata.stride, 32);
|
||||
EXPECT_EQ(demdata.getImage()->bytes(), size_t(32*32*4));
|
||||
};
|
||||
|
||||
TEST(DEMData, ConstructorTerrarium) {
|
||||
PremultipliedImage image = fakeImage({16, 16});
|
||||
DEMData demdata(image, Tileset::DEMEncoding::Terrarium);
|
||||
|
||||
EXPECT_EQ(demdata.dim, 16);
|
||||
EXPECT_EQ(demdata.border, 8);
|
||||
EXPECT_EQ(demdata.stride, 32);
|
||||
EXPECT_EQ(demdata.getImage()->bytes(), size_t(32*32*4));
|
||||
};
|
||||
|
||||
TEST(DEMData, RoundTrip) {
|
||||
PremultipliedImage image = fakeImage({16, 16});
|
||||
DEMData pyramid(image);
|
||||
DEMData demdata(image, Tileset::DEMEncoding::Mapbox);
|
||||
|
||||
pyramid.set(4, 6, 255);
|
||||
EXPECT_EQ(pyramid.get(4, 6), 255);
|
||||
demdata.set(4, 6, 255);
|
||||
EXPECT_EQ(demdata.get(4, 6), 255);
|
||||
}
|
||||
|
||||
TEST(DEMData, InitialBackfill) {
|
||||
|
||||
PremultipliedImage image1 = fakeImage({4, 4});
|
||||
DEMData dem1(image1);
|
||||
DEMData dem1(image1, Tileset::DEMEncoding::Mapbox);
|
||||
|
||||
bool nonempty = true;
|
||||
// checking that a 1 px border around the fake image has been populated
|
||||
@@ -91,10 +100,10 @@ TEST(DEMData, InitialBackfill) {
|
||||
|
||||
TEST(DEMData, BackfillNeighbor) {
|
||||
PremultipliedImage image1 = fakeImage({4, 4});
|
||||
DEMData dem0(image1);
|
||||
DEMData dem0(image1, Tileset::DEMEncoding::Mapbox);
|
||||
|
||||
PremultipliedImage image2 = fakeImage({4, 4});
|
||||
DEMData dem1(image2);
|
||||
DEMData dem1(image2, Tileset::DEMEncoding::Mapbox);
|
||||
|
||||
dem0.backfillBorder(dem1, -1, 0);
|
||||
for (int y = 0; y < 4; y++) {
|
||||
|
||||
@@ -62,7 +62,7 @@ TEST(RasterDEMTile, onError) {
|
||||
TEST(RasterDEMTile, onParsed) {
|
||||
RasterDEMTileTest test;
|
||||
RasterDEMTile tile(OverscaledTileID(0, 0, 0), test.tileParameters, test.tileset);
|
||||
tile.onParsed(std::make_unique<HillshadeBucket>(PremultipliedImage({16, 16})), 0);
|
||||
tile.onParsed(std::make_unique<HillshadeBucket>(PremultipliedImage({16, 16}), Tileset::DEMEncoding::Mapbox), 0);
|
||||
EXPECT_TRUE(tile.isRenderable());
|
||||
EXPECT_TRUE(tile.isLoaded());
|
||||
EXPECT_TRUE(tile.isComplete());
|
||||
|
||||
Reference in New Issue
Block a user