1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include <mbgl/style/sources/geojson_source_impl.hpp>
#include <mbgl/style/source_observer.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/tile/geojson_tile.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/rapidjson.hpp>
#include <mapbox/geojsonvt.hpp>
#include <mapbox/geojsonvt/convert.hpp>
#include <rapidjson/error/en.h>
#include <sstream>
using namespace mapbox::geojsonvt;
namespace mbgl {
namespace style {
namespace conversion {
template <>
Result<GeoJSON> convertGeoJSON(const JSValue& value) {
Options options;
options.buffer = util::EXTENT / util::tileSize * 128;
options.extent = util::EXTENT;
try {
return GeoJSON { std::make_unique<GeoJSONVT>(Convert::convert(value, 0), options) };
} catch (const std::exception& ex) {
return Error { ex.what() };
}
}
} // namespace conversion
GeoJSONSource::Impl::Impl(std::string id_, Source& base_)
: Source::Impl(SourceType::GeoJSON, std::move(id_), base_) {
}
GeoJSONSource::Impl::~Impl() = default;
void GeoJSONSource::Impl::setURL(std::string url) {
urlOrGeoJSON = std::move(url);
}
void GeoJSONSource::Impl::setGeoJSON(GeoJSON&& geoJSON) {
urlOrGeoJSON = std::move(geoJSON);
}
void GeoJSONSource::Impl::load(FileSource& fileSource) {
if (urlOrGeoJSON.is<GeoJSON>()) {
loaded = true;
return;
}
if (req) {
return;
}
const std::string& url = urlOrGeoJSON.get<std::string>();
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)));
} else if (res.notModified) {
return;
} else if (res.noContent) {
observer->onSourceError(base, std::make_exception_ptr(std::runtime_error("unexpectedly empty GeoJSON")));
} else {
rapidjson::GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> d;
d.Parse<0>(res.data->c_str());
if (d.HasParseError()) {
std::stringstream message;
message << d.GetErrorOffset() << " - " << rapidjson::GetParseError_En(d.GetParseError());
observer->onSourceError(base, std::make_exception_ptr(std::runtime_error(message.str())));
return;
}
invalidateTiles();
conversion::Result<GeoJSON> geoJSON = conversion::convertGeoJSON<JSValue>(d);
if (!geoJSON) {
Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", geoJSON.error().message);
// Create an empty GeoJSON VT object to make sure we're not infinitely waiting for
// tiles to load.
urlOrGeoJSON = GeoJSON { std::make_unique<GeoJSONVT>(std::vector<ProjectedFeature>()) };
} else {
urlOrGeoJSON = std::move(*geoJSON);
}
loaded = true;
observer->onSourceLoaded(base);
}
});
}
Range<uint8_t> GeoJSONSource::Impl::getZoomRange() {
assert(loaded);
return { 0, urlOrGeoJSON.get<GeoJSON>().impl->options.maxZoom };
}
std::unique_ptr<Tile> GeoJSONSource::Impl::createTile(const OverscaledTileID& tileID,
const UpdateParameters& parameters) {
assert(loaded);
return std::make_unique<GeoJSONTile>(tileID, base.getID(), parameters, *urlOrGeoJSON.get<GeoJSON>().impl);
}
} // namespace style
} // namespace mbgl
|