[core] Add ImageSource support to style parsers
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <mbgl/style/layer_impl.hpp>
|
||||
#include <mbgl/style/rapidjson_conversion.hpp>
|
||||
#include <mbgl/style/conversion.hpp>
|
||||
#include <mbgl/style/conversion/coordinate.hpp>
|
||||
#include <mbgl/style/conversion/source.hpp>
|
||||
#include <mbgl/style/conversion/layer.hpp>
|
||||
#include <mbgl/style/conversion/light.hpp>
|
||||
@@ -55,10 +56,12 @@ StyleParseResult Parser::parse(const std::string& json) {
|
||||
|
||||
if (document.HasMember("center")) {
|
||||
const JSValue& value = document["center"];
|
||||
if (value.IsArray() && value.Size() >= 2) {
|
||||
// Style spec uses lon/lat order
|
||||
latLng = LatLng(value[1].IsNumber() ? value[1].GetDouble() : 0,
|
||||
value[0].IsNumber() ? value[0].GetDouble() : 0);
|
||||
conversion::Error error;
|
||||
auto convertedLatLng = conversion::convert<LatLng>(value, error);
|
||||
if (convertedLatLng) {
|
||||
latLng = *convertedLatLng;
|
||||
} else {
|
||||
Log::Warning(Event::ParseStyle, "center coordinate must be a longitude, latitude pair");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
85
src/mbgl/style/sources/image_source.cpp
Normal file
85
src/mbgl/style/sources/image_source.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <mbgl/style/sources/image_source.hpp>
|
||||
#include <mbgl/style/sources/image_source_impl.hpp>
|
||||
#include <mbgl/util/geo.hpp>
|
||||
#include <mbgl/style/source_observer.hpp>
|
||||
#include <mbgl/util/premultiply.hpp>
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
ImageSource::ImageSource(std::string id, const std::vector<LatLng> coords_)
|
||||
: Source(makeMutable<Impl>(std::move(id), coords_)) {
|
||||
}
|
||||
|
||||
ImageSource::~ImageSource() = default;
|
||||
|
||||
const ImageSource::Impl& ImageSource::impl() const {
|
||||
return static_cast<const Impl&>(*baseImpl);
|
||||
}
|
||||
|
||||
void ImageSource::setCoordinates(const std::vector<LatLng>& coords_) {
|
||||
baseImpl = makeMutable<Impl>(impl(), coords_);
|
||||
observer->onSourceChanged(*this);
|
||||
}
|
||||
|
||||
std::vector<LatLng> ImageSource::getCoordinates() const {
|
||||
return impl().getCoordinates();
|
||||
}
|
||||
|
||||
void ImageSource::setURL(const std::string& url_) {
|
||||
url = std::move(url_);
|
||||
// Signal that the source description needs a reload
|
||||
if (loaded || req) {
|
||||
loaded = false;
|
||||
req.reset();
|
||||
observer->onSourceDescriptionChanged(*this);
|
||||
}
|
||||
}
|
||||
|
||||
void ImageSource::setImage(mbgl::UnassociatedImage image_) {
|
||||
url = {};
|
||||
if (req) {
|
||||
req.reset();
|
||||
}
|
||||
loaded = true;
|
||||
baseImpl = makeMutable<Impl>(impl(), std::move(image_));
|
||||
observer->onSourceChanged(*this);
|
||||
}
|
||||
|
||||
const std::string& ImageSource::getURL() const {
|
||||
return *url;
|
||||
}
|
||||
|
||||
void ImageSource::loadDescription(FileSource& fileSource) {
|
||||
if (!url) {
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
if (req || loaded) {
|
||||
return;
|
||||
}
|
||||
const Resource imageResource { Resource::Unknown, *url, {}, Resource::Necessity::Required };
|
||||
|
||||
req = fileSource.request(imageResource, [this](Response res) {
|
||||
if (res.error) {
|
||||
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message)));
|
||||
} else if (res.notModified) {
|
||||
return;
|
||||
} else if (res.noContent) {
|
||||
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error("unexpectedly empty image url")));
|
||||
} else {
|
||||
try {
|
||||
UnassociatedImage image = util::unpremultiply(decodeImage(*res.data));
|
||||
baseImpl = makeMutable<Impl>(impl(), std::move(image));
|
||||
} catch (...) {
|
||||
observer->onSourceError(*this, std::current_exception());
|
||||
}
|
||||
loaded = true;
|
||||
observer->onSourceLoaded(*this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
42
src/mbgl/style/sources/image_source_impl.cpp
Normal file
42
src/mbgl/style/sources/image_source_impl.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <mbgl/style/sources/image_source_impl.hpp>
|
||||
#include <mbgl/util/geo.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
namespace style {
|
||||
|
||||
ImageSource::Impl::Impl(std::string id_, std::vector<LatLng> coords_)
|
||||
: Source::Impl(SourceType::Image, std::move(id_)),
|
||||
coords(std::move(coords_)) {
|
||||
}
|
||||
|
||||
ImageSource::Impl::Impl(const Impl& other, std::vector<LatLng> coords_)
|
||||
: Source::Impl(other),
|
||||
coords(std::move(coords_)),
|
||||
image(other.image.clone()) {
|
||||
}
|
||||
|
||||
ImageSource::Impl::Impl(const Impl& rhs, UnassociatedImage image_)
|
||||
: Source::Impl(rhs),
|
||||
coords(rhs.coords),
|
||||
image(std::move(image_)) {
|
||||
}
|
||||
ImageSource::Impl::~Impl() = default;
|
||||
|
||||
void ImageSource::Impl::setImage(UnassociatedImage image_) {
|
||||
image = std::move(image_);
|
||||
}
|
||||
|
||||
const UnassociatedImage& ImageSource::Impl::getImage() const {
|
||||
return image;
|
||||
}
|
||||
|
||||
std::vector<LatLng> ImageSource::Impl::getCoordinates() const {
|
||||
return coords;
|
||||
}
|
||||
|
||||
optional<std::string> ImageSource::Impl::getAttribution() const {
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
31
src/mbgl/style/sources/image_source_impl.hpp
Normal file
31
src/mbgl/style/sources/image_source_impl.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/style/source_impl.hpp>
|
||||
#include <mbgl/style/sources/image_source.hpp>
|
||||
#include <mbgl/util/image.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
class LatLng;
|
||||
|
||||
namespace style {
|
||||
|
||||
class ImageSource::Impl : public Source::Impl {
|
||||
public:
|
||||
Impl(std::string id, std::vector<LatLng> coords);
|
||||
Impl(const Impl& rhs, std::vector<LatLng> coords);
|
||||
Impl(const Impl& rhs, UnassociatedImage image);
|
||||
|
||||
~Impl() final;
|
||||
|
||||
void setImage(UnassociatedImage );
|
||||
const mbgl::UnassociatedImage& getImage() const;
|
||||
std::vector<LatLng> getCoordinates() const;
|
||||
|
||||
optional<std::string> getAttribution() const final;
|
||||
private:
|
||||
std::vector<LatLng> coords;
|
||||
mbgl::UnassociatedImage image;
|
||||
};
|
||||
|
||||
} // namespace style
|
||||
} // namespace mbgl
|
||||
@@ -11,6 +11,7 @@ MBGL_DEFINE_ENUM(SourceType, {
|
||||
{ SourceType::GeoJSON, "geojson" },
|
||||
{ SourceType::Video, "video" },
|
||||
{ SourceType::Annotations, "annotations" },
|
||||
{ SourceType::Image, "image" },
|
||||
});
|
||||
|
||||
MBGL_DEFINE_ENUM(VisibilityType, {
|
||||
|
||||
Reference in New Issue
Block a user