[core] Change GeoJSONSource::getURL() to return an optional<std::string>

GeoJSON sources may have inline GeoJSON rather than a URL; returning an optional type ensures that consumers handle this case.
This commit is contained in:
John Firebaugh
2016-08-25 17:44:44 -07:00
parent 50ae6c738a
commit d2abb1b6d5
5 changed files with 22 additions and 23 deletions

View File

@@ -2,6 +2,7 @@
#include <mbgl/style/source.hpp>
#include <mbgl/util/geojson.hpp>
#include <mbgl/util/optional.hpp>
#include <mapbox/geojson.hpp>
@@ -42,7 +43,7 @@ public:
void setURL(const std::string& url);
void setGeoJSON(const GeoJSON&);
std::string getURL();
optional<std::string> getURL();
// Private implementation

View File

@@ -205,8 +205,8 @@ void OfflineDownload::activateDownload() {
style::GeoJSONSource::Impl* geojsonSource =
static_cast<style::GeoJSONSource::Impl*>(source->baseImpl.get());
if (!geojsonSource->loaded) {
ensureResource(Resource::source(geojsonSource->getURL()));
if (geojsonSource->getURL()) {
ensureResource(Resource::source(*geojsonSource->getURL()));
}
break;
}

View File

@@ -18,7 +18,7 @@ void GeoJSONSource::setGeoJSON(const mapbox::geojson::geojson& geoJSON) {
impl->setGeoJSON(geoJSON);
}
std::string GeoJSONSource::getURL() {
optional<std::string> GeoJSONSource::getURL() {
return impl->getURL();
}

View File

@@ -36,13 +36,12 @@ GeoJSONSource::Impl::Impl(std::string id_, Source& base_, const GeoJSONOptions o
GeoJSONSource::Impl::~Impl() = default;
void GeoJSONSource::Impl::setURL(std::string url) {
urlOrGeoJSON = std::move(url);
void GeoJSONSource::Impl::setURL(std::string url_) {
url = std::move(url_);
}
std::string GeoJSONSource::Impl::getURL() {
assert(urlOrGeoJSON.is<std::string>());
return urlOrGeoJSON.get<std::string>();
optional<std::string> GeoJSONSource::Impl::getURL() {
return url;
}
void GeoJSONSource::Impl::setGeoJSON(const GeoJSON& geoJSON) {
@@ -54,7 +53,7 @@ void GeoJSONSource::Impl::setGeoJSON(const GeoJSON& geoJSON) {
vtOptions.extent = util::EXTENT;
vtOptions.buffer = std::round(scale * options.buffer);
vtOptions.tolerance = scale * options.tolerance;
urlOrGeoJSON = std::make_unique<mapbox::geojsonvt::GeoJSONVT>(geoJSON, vtOptions);
geoJSONOrSupercluster = std::make_unique<mapbox::geojsonvt::GeoJSONVT>(geoJSON, vtOptions);
} else {
mapbox::supercluster::Options clusterOptions;
@@ -63,13 +62,13 @@ void GeoJSONSource::Impl::setGeoJSON(const GeoJSON& geoJSON) {
clusterOptions.radius = std::round(scale * options.clusterRadius);
const auto& features = geoJSON.get<mapbox::geometry::feature_collection<double>>();
urlOrGeoJSON =
geoJSONOrSupercluster =
std::make_unique<mapbox::supercluster::Supercluster>(features, clusterOptions);
}
}
void GeoJSONSource::Impl::load(FileSource& fileSource) {
if (!urlOrGeoJSON.is<std::string>()) {
if (!url) {
loaded = true;
return;
}
@@ -78,8 +77,7 @@ void GeoJSONSource::Impl::load(FileSource& fileSource) {
return;
}
const std::string& url = urlOrGeoJSON.get<std::string>();
req = fileSource.request(Resource::source(url), [this](Response res) {
req = fileSource.request(Resource::source(*url), [this](Response res) {
if (res.error) {
observer->onSourceError(
base, std::make_exception_ptr(std::runtime_error(res.error->message)));
@@ -128,13 +126,13 @@ Range<uint8_t> GeoJSONSource::Impl::getZoomRange() {
std::unique_ptr<Tile> GeoJSONSource::Impl::createTile(const OverscaledTileID& tileID,
const UpdateParameters& parameters) {
assert(loaded);
if (urlOrGeoJSON.is<GeoJSONVTPointer>()) {
if (geoJSONOrSupercluster.is<GeoJSONVTPointer>()) {
return std::make_unique<GeoJSONTile>(tileID, base.getID(), parameters,
*urlOrGeoJSON.get<GeoJSONVTPointer>());
*geoJSONOrSupercluster.get<GeoJSONVTPointer>());
} else {
assert(urlOrGeoJSON.is<SuperclusterPointer>());
assert(geoJSONOrSupercluster.is<SuperclusterPointer>());
return std::make_unique<GeoJSONTile>(tileID, base.getID(), parameters,
*urlOrGeoJSON.get<SuperclusterPointer>());
*geoJSONOrSupercluster.get<SuperclusterPointer>());
}
}

View File

@@ -16,9 +16,9 @@ public:
~Impl() final;
void setURL(std::string);
void setGeoJSON(const GeoJSON&);
optional<std::string> getURL();
std::string getURL();
void setGeoJSON(const GeoJSON&);
void load(FileSource&) final;
@@ -30,10 +30,10 @@ private:
Range<uint8_t> getZoomRange() final;
std::unique_ptr<Tile> createTile(const OverscaledTileID&, const UpdateParameters&) final;
variant<std::string, GeoJSONVTPointer, SuperclusterPointer> urlOrGeoJSON;
std::unique_ptr<AsyncRequest> req;
GeoJSONOptions options;
optional<std::string> url;
std::unique_ptr<AsyncRequest> req;
variant<GeoJSONVTPointer, SuperclusterPointer> geoJSONOrSupercluster;
};
} // namespace style