summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-06-14 10:46:35 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-06-14 11:25:45 -0700
commit615a6685a47609310930890203f41815a5f19d48 (patch)
tree2f777fbe6d5e4d33453dbcb7d95d2f295bafa83c
parent2257ebf1c2297754960b4a1f1d5c3d25717c17db (diff)
downloadqtlocation-mapboxgl-615a6685a47609310930890203f41815a5f19d48.tar.gz
[core] Move GeoJSON-specific parsing into geojson_source.cpp
-rw-r--r--src/mbgl/style/parser.cpp86
-rw-r--r--src/mbgl/style/parser.hpp8
-rw-r--r--src/mbgl/style/sources/geojson_source.cpp52
-rw-r--r--src/mbgl/style/sources/geojson_source.hpp6
4 files changed, 76 insertions, 76 deletions
diff --git a/src/mbgl/style/parser.cpp b/src/mbgl/style/parser.cpp
index 670a5dbd92..8a677d29c8 100644
--- a/src/mbgl/style/parser.cpp
+++ b/src/mbgl/style/parser.cpp
@@ -12,9 +12,6 @@
#include <mbgl/platform/log.hpp>
-#include <mapbox/geojsonvt.hpp>
-#include <mapbox/geojsonvt/convert.hpp>
-
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/enum.hpp>
@@ -175,19 +172,15 @@ void Parser::parseSources(const JSValue& value) {
continue;
}
- // Sources can have URLs, either because they reference an external TileJSON file, or
- // because reference a GeoJSON file. They don't have to have one though when all source
- // parameters are specified inline.
- std::string url;
-
- uint16_t tileSize = util::tileSize;
+ const std::string id { nameVal.GetString(), nameVal.GetStringLength() };
std::unique_ptr<Tileset> tileset;
- std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> geojsonvt;
-
- const std::string id { nameVal.GetString(), nameVal.GetStringLength() };
std::unique_ptr<Source> source;
+ // Sources can have URLs, either because they reference an external TileJSON file, or
+ // because reference a GeoJSON file. They don't have to have one though when all source
+ // parameters are specified inline.
+ std::string url;
if (sourceVal.HasMember("url")) {
const JSValue& urlVal = sourceVal["url"];
if (urlVal.IsString()) {
@@ -200,18 +193,19 @@ void Parser::parseSources(const JSValue& value) {
tileset = parseTileJSON(sourceVal);
}
- switch (*type) {
- case SourceType::Raster:
- if (sourceVal.HasMember("tileSize")) {
- const JSValue& tileSizeVal = sourceVal["tileSize"];
- if (tileSizeVal.IsNumber() && tileSizeVal.GetUint64() <= std::numeric_limits<uint16_t>::max()) {
- tileSize = tileSizeVal.GetUint64();
- } else {
- Log::Error(Event::ParseStyle, "invalid tileSize");
- continue;
- }
+ uint16_t tileSize = util::tileSize;
+ if (sourceVal.HasMember("tileSize")) {
+ const JSValue& tileSizeVal = sourceVal["tileSize"];
+ if (tileSizeVal.IsNumber() && tileSizeVal.GetUint64() <= std::numeric_limits<uint16_t>::max()) {
+ tileSize = tileSizeVal.GetUint64();
+ } else {
+ Log::Error(Event::ParseStyle, "invalid tileSize");
+ continue;
}
+ }
+ switch (*type) {
+ case SourceType::Raster:
source = std::make_unique<RasterSource>(id, url, tileSize, std::move(tileset));
break;
@@ -220,30 +214,7 @@ void Parser::parseSources(const JSValue& value) {
break;
case SourceType::GeoJSON:
- tileset = std::make_unique<Tileset>();
-
- // We should probably split this up to have URLs in the url property, and actual data
- // in the data property. Until then, we're going to detect the content based on the
- // object type.
- if (sourceVal.HasMember("data")) {
- const JSValue& dataVal = sourceVal["data"];
- if (dataVal.IsString()) {
- // We need to load an external GeoJSON file
- url = { dataVal.GetString(), dataVal.GetStringLength() };
- } else if (dataVal.IsObject()) {
- // We need to parse dataVal as a GeoJSON object
- geojsonvt = parseGeoJSON(dataVal);
- tileset->maxZoom = geojsonvt->options.maxZoom;
- } else {
- Log::Error(Event::ParseStyle, "GeoJSON data must be a URL or an object");
- continue;
- }
- } else {
- Log::Error(Event::ParseStyle, "GeoJSON source must have a data value");
- continue;
- }
-
- source = std::make_unique<GeoJSONSource>(id, url, tileSize, std::move(tileset), std::move(geojsonvt));
+ source = GeoJSONSource::parse(id, sourceVal);
break;
default:
@@ -251,25 +222,10 @@ void Parser::parseSources(const JSValue& value) {
continue;
}
- sourcesMap.emplace(id, source.get());
- sources.emplace_back(std::move(source));
- }
-}
-
-std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> parseGeoJSON(const JSValue& value) {
- using namespace mapbox::geojsonvt;
-
- Options options;
- options.buffer = util::EXTENT / util::tileSize * 128;
- options.extent = util::EXTENT;
-
- try {
- return std::make_unique<GeoJSONVT>(Convert::convert(value, 0), options);
- } catch (const std::exception& ex) {
- Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", ex.what());
- // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for
- // tiles to load.
- return std::make_unique<GeoJSONVT>(std::vector<ProjectedFeature>{}, options);
+ if (source) {
+ sourcesMap.emplace(id, source.get());
+ sources.emplace_back(std::move(source));
+ }
}
}
diff --git a/src/mbgl/style/parser.hpp b/src/mbgl/style/parser.hpp
index 09e47a9997..e60a33b93f 100644
--- a/src/mbgl/style/parser.hpp
+++ b/src/mbgl/style/parser.hpp
@@ -13,20 +13,12 @@
#include <unordered_map>
#include <forward_list>
-namespace mapbox {
-namespace geojsonvt {
-class GeoJSONVT;
-} // namespace geojsonvt
-} // namespace mapbox
-
namespace mbgl {
namespace style {
std::unique_ptr<Tileset> parseTileJSON(const std::string& json, const std::string& sourceURL, SourceType, uint16_t tileSize);
std::unique_ptr<Tileset> parseTileJSON(const JSValue&);
-std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> parseGeoJSON(const JSValue&);
-
Filter parseFilter(const JSValue&);
class Parser {
diff --git a/src/mbgl/style/sources/geojson_source.cpp b/src/mbgl/style/sources/geojson_source.cpp
index af537ff6ce..d0a11ec0bc 100644
--- a/src/mbgl/style/sources/geojson_source.cpp
+++ b/src/mbgl/style/sources/geojson_source.cpp
@@ -3,8 +3,10 @@
#include <mbgl/style/parser.hpp>
#include <mbgl/tile/geojson_tile.hpp>
#include <mbgl/storage/file_source.hpp>
+#include <mbgl/platform/log.hpp>
#include <mapbox/geojsonvt.hpp>
+#include <mapbox/geojsonvt/convert.hpp>
#include <rapidjson/error/en.h>
@@ -13,12 +15,58 @@
namespace mbgl {
namespace style {
+std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> parseGeoJSON(const JSValue& value) {
+ using namespace mapbox::geojsonvt;
+
+ Options options;
+ options.buffer = util::EXTENT / util::tileSize * 128;
+ options.extent = util::EXTENT;
+
+ try {
+ return std::make_unique<GeoJSONVT>(Convert::convert(value, 0), options);
+ } catch (const std::exception& ex) {
+ Log::Error(Event::ParseStyle, "Failed to parse GeoJSON data: %s", ex.what());
+ // Create an empty GeoJSON VT object to make sure we're not infinitely waiting for
+ // tiles to load.
+ return std::make_unique<GeoJSONVT>(std::vector<ProjectedFeature>{}, options);
+ }
+}
+
+std::unique_ptr<GeoJSONSource> GeoJSONSource::parse(const std::string& id,
+ const JSValue& value) {
+ auto tileset = std::make_unique<Tileset>();
+ std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> geojsonvt;
+ std::string url;
+
+ // We should probably split this up to have URLs in the url property, and actual data
+ // in the data property. Until then, we're going to detect the content based on the
+ // object type.
+ if (!value.HasMember("data")) {
+ Log::Error(Event::ParseStyle, "GeoJSON source must have a data value");
+ return nullptr;
+ }
+
+ const JSValue& dataVal = value["data"];
+ if (dataVal.IsString()) {
+ // We need to load an external GeoJSON file
+ url = { dataVal.GetString(), dataVal.GetStringLength() };
+ } else if (dataVal.IsObject()) {
+ // We need to parse dataVal as a GeoJSON object
+ geojsonvt = parseGeoJSON(dataVal);
+ tileset->maxZoom = geojsonvt->options.maxZoom;
+ } else {
+ Log::Error(Event::ParseStyle, "GeoJSON data must be a URL or an object");
+ return nullptr;
+ }
+
+ return std::make_unique<GeoJSONSource>(id, url, std::move(tileset), std::move(geojsonvt));
+}
+
GeoJSONSource::GeoJSONSource(std::string id_,
std::string url_,
- uint16_t tileSize_,
std::unique_ptr<Tileset>&& tileset_,
std::unique_ptr<mapbox::geojsonvt::GeoJSONVT>&& geojsonvt_)
- : Source(SourceType::GeoJSON, std::move(id_), std::move(url_), tileSize_, std::move(tileset_)),
+ : Source(SourceType::GeoJSON, std::move(id_), std::move(url_), util::tileSize, std::move(tileset_)),
geojsonvt(std::move(geojsonvt_)) {
}
diff --git a/src/mbgl/style/sources/geojson_source.hpp b/src/mbgl/style/sources/geojson_source.hpp
index 4c5bbf63df..08ccf84760 100644
--- a/src/mbgl/style/sources/geojson_source.hpp
+++ b/src/mbgl/style/sources/geojson_source.hpp
@@ -2,6 +2,8 @@
#include <mbgl/style/source.hpp>
+#include <mbgl/util/rapidjson.hpp>
+
namespace mapbox {
namespace geojsonvt {
class GeoJSONVT;
@@ -16,9 +18,11 @@ namespace style {
class GeoJSONSource : public Source {
public:
+ static std::unique_ptr<GeoJSONSource> parse(const std::string& id,
+ const JSValue&);
+
GeoJSONSource(std::string id,
std::string url,
- uint16_t tileSize,
std::unique_ptr<Tileset>&&,
std::unique_ptr<mapbox::geojsonvt::GeoJSONVT>&&);
~GeoJSONSource() final;