Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] parse GeoJSON source type
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer committed Dec 10, 2015
1 parent ece5452 commit 81715e6
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ TileData::State Source::addTile(const TileID& id, const StyleUpdateParameters& p
return state;
}

auto pos = tiles.emplace(id, std::make_unique<Tile>(id));

Tile& new_tile = *pos.first->second;
auto newTile = std::make_unique<Tile>(id);

// We couldn't find the tile in the list. Create a new one.
// Try to find the associated TileData object.
Expand All @@ -178,19 +176,19 @@ TileData::State Source::addTile(const TileID& id, const StyleUpdateParameters& p
auto it = tileDataMap.find(normalized_id);
if (it != tileDataMap.end()) {
// Create a shared_ptr handle. Note that this might be empty!
new_tile.data = it->second.lock();
newTile->data = it->second.lock();
}

if (new_tile.data && new_tile.data->getState() == TileData::State::obsolete) {
if (newTile->data && newTile->data->getState() == TileData::State::obsolete) {
// Do not consider the tile if it's already obsolete.
new_tile.data.reset();
newTile->data.reset();
}

if (!new_tile.data) {
new_tile.data = cache.get(normalized_id.to_uint64());
if (!newTile->data) {
newTile->data = cache.get(normalized_id.to_uint64());
}

if (!new_tile.data) {
if (!newTile->data) {
auto callback = std::bind(&Source::tileLoadingCompleteCallback, this, normalized_id, parameters.transformState, parameters.debugOptions & MapDebugOptions::Collision);

// If we don't find working tile data, we're just going to load it.
Expand All @@ -201,7 +199,7 @@ TileData::State Source::addTile(const TileID& id, const StyleUpdateParameters& p
parameters.worker);

tileData->request(parameters.pixelRatio, callback);
new_tile.data = tileData;
newTile->data = tileData;
} else {
std::unique_ptr<GeometryTileMonitor> monitor;

Expand All @@ -210,20 +208,23 @@ TileData::State Source::addTile(const TileID& id, const StyleUpdateParameters& p
} else if (info.type == SourceType::Annotations) {
monitor = std::make_unique<AnnotationTileMonitor>(normalized_id, parameters.data);
} else {
throw std::runtime_error("source type not implemented");
Log::Warning(Event::Style, "Source type '%s' is not implemented", SourceTypeClass(info.type).c_str());
return TileData::State::invalid;
}

new_tile.data = std::make_shared<VectorTileData>(normalized_id,
newTile->data = std::make_shared<VectorTileData>(normalized_id,
std::move(monitor),
info.source_id,
parameters.style,
callback);
}

tileDataMap.emplace(new_tile.data->id, new_tile.data);
tileDataMap.emplace(newTile->data->id, newTile->data);
}

return new_tile.data->getState();
const auto newState = newTile->data->getState();
tiles.emplace(id, std::move(newTile));
return newState;
}

double Source::getZoom(const TransformState& state) const {
Expand Down
31 changes: 31 additions & 0 deletions src/mbgl/style/style_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include <mbgl/platform/log.hpp>

#include <mapbox/geojsonvt.hpp>
#include <mapbox/geojsonvt/convert.hpp>

#include <algorithm>

namespace mbgl {
Expand Down Expand Up @@ -84,6 +87,11 @@ void StyleParser::parseSources(const JSVal& value) {
continue;
}
break;
case SourceType::GeoJSON:
if (!parseGeoJSONSource(*source, sourceVal)) {
continue;
}
break;
default:
Log::Warning(Event::ParseStyle, "source type %s is not supported", SourceTypeClass(source->info.type).c_str());
}
Expand Down Expand Up @@ -150,6 +158,29 @@ bool StyleParser::parseRasterSource(Source& source, const JSVal& sourceVal) {
return true;
}

bool StyleParser::parseGeoJSONSource(Source& source, const JSVal& sourceVal) {
if (!sourceVal.HasMember("data")) {
Log::Warning(Event::ParseStyle, "GeoJSON source must have a data value");
return false;
}

const JSVal& dataVal = sourceVal["data"];
if (dataVal.IsString()) {
// We need to load an external GeoJSON file
source.info.url = { dataVal.GetString(), dataVal.GetStringLength() };

} else if (dataVal.IsObject()) {
// We need to parse dataVal as a GeoJSON object
auto geojsonvt = std::make_unique<mapbox::geojsonvt::GeoJSONVT>(mapbox::geojsonvt::Convert::convert(dataVal, 0));
// TODO
} else {
Log::Warning(Event::ParseStyle, "GeoJSON data must be a URL or an object");
return false;
}

return true;
}

void StyleParser::parseLayers(const JSVal& value) {
std::vector<std::string> ids;

Expand Down
1 change: 1 addition & 0 deletions src/mbgl/style/style_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class StyleParser {
void parseSources(const JSVal&);
bool parseVectorSource(Source&, const JSVal&);
bool parseRasterSource(Source&, const JSVal&);
bool parseGeoJSONSource(Source&, const JSVal&);
void parseLayers(const JSVal&);
void parseLayer(const std::string& id, const JSVal&, std::unique_ptr<StyleLayer>&);
void parseVisibility(StyleLayer&, const JSVal& value);
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/style_parser/geojson-data-inline.info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"default": {
"log": [
]
}
}
9 changes: 9 additions & 0 deletions test/fixtures/style_parser/geojson-data-inline.style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 8,
"sources": {
"mapbox": {
"type": "geojson",
"data": { "type": "Feature", "geometry": { "type": "Point", "coordinates": [100.0, 0.0] } }
}
}
}
6 changes: 6 additions & 0 deletions test/fixtures/style_parser/geojson-data-url.info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"default": {
"log": [
]
}
}
9 changes: 9 additions & 0 deletions test/fixtures/style_parser/geojson-data-url.style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 8,
"sources": {
"mapbox": {
"type": "geojson",
"data": "asset://TEST_DATA/fixtures/geojson/point.json"
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/style_parser/geojson-invalid-data.info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"default": {
"log": [
[1, "WARNING", "ParseStyle", "GeoJSON data must be a URL or an object"]
]
}
}
9 changes: 9 additions & 0 deletions test/fixtures/style_parser/geojson-invalid-data.style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": 8,
"sources": {
"mapbox": {
"type": "geojson",
"data": 24
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/style_parser/geojson-missing-data.info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"default": {
"log": [
[1, "WARNING", "ParseStyle", "GeoJSON source must have a data value"]
]
}
}
8 changes: 8 additions & 0 deletions test/fixtures/style_parser/geojson-missing-data.style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": 8,
"sources": {
"mapbox": {
"type": "geojson"
}
}
}

0 comments on commit 81715e6

Please sign in to comment.