diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/style/parser.cpp | 11 | ||||
-rw-r--r-- | src/mbgl/style/sources/image_source.cpp | 85 | ||||
-rw-r--r-- | src/mbgl/style/sources/image_source_impl.cpp | 42 | ||||
-rw-r--r-- | src/mbgl/style/sources/image_source_impl.hpp | 31 | ||||
-rw-r--r-- | src/mbgl/style/types.cpp | 1 |
5 files changed, 166 insertions, 4 deletions
diff --git a/src/mbgl/style/parser.cpp b/src/mbgl/style/parser.cpp index ea96bda502..725feb3fc2 100644 --- a/src/mbgl/style/parser.cpp +++ b/src/mbgl/style/parser.cpp @@ -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"); } } diff --git a/src/mbgl/style/sources/image_source.cpp b/src/mbgl/style/sources/image_source.cpp new file mode 100644 index 0000000000..4f86befd0b --- /dev/null +++ b/src/mbgl/style/sources/image_source.cpp @@ -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 diff --git a/src/mbgl/style/sources/image_source_impl.cpp b/src/mbgl/style/sources/image_source_impl.cpp new file mode 100644 index 0000000000..e16b33b4ca --- /dev/null +++ b/src/mbgl/style/sources/image_source_impl.cpp @@ -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 diff --git a/src/mbgl/style/sources/image_source_impl.hpp b/src/mbgl/style/sources/image_source_impl.hpp new file mode 100644 index 0000000000..439db4fa64 --- /dev/null +++ b/src/mbgl/style/sources/image_source_impl.hpp @@ -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 diff --git a/src/mbgl/style/types.cpp b/src/mbgl/style/types.cpp index b37e73ffb1..4fbf767e11 100644 --- a/src/mbgl/style/types.cpp +++ b/src/mbgl/style/types.cpp @@ -11,6 +11,7 @@ MBGL_DEFINE_ENUM(SourceType, { { SourceType::GeoJSON, "geojson" }, { SourceType::Video, "video" }, { SourceType::Annotations, "annotations" }, + { SourceType::Image, "image" }, }); MBGL_DEFINE_ENUM(VisibilityType, { |