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

Commit

Permalink
first cut of annotations source driving GeoJSONVT requests
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Feb 20, 2015
1 parent 57dd240 commit 988e64e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 36 deletions.
3 changes: 2 additions & 1 deletion include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class LineAtlas;

class Map : private util::noncopyable {
public:
explicit Map(View&, FileSource&, std::string = "");
explicit Map(View&, FileSource&);
~Map();

// Start the map render thread. It is asynchronous.
Expand Down Expand Up @@ -240,6 +240,7 @@ class Map : private util::noncopyable {
std::chrono::steady_clock::time_point animationTime = std::chrono::steady_clock::time_point::min();

std::set<util::ptr<StyleSource>> activeSources;
util::ptr<StyleSource> annotationsSource;
};

}
Expand Down
7 changes: 1 addition & 6 deletions macosx/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,7 @@ int main() {

mbgl::SQLiteCache cache(defaultCacheDatabase());
mbgl::DefaultFileSource fileSource(&cache);
NSString *json = [[NSString alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"threestates" ofType:@"geojson"]
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"loaded up feature JSON of %lu bytes", (unsigned long)json.length);
mbgl::Map map(view, fileSource, std::string([json UTF8String]));
mbgl::Map map(view, fileSource);

URLHandler *handler = [[URLHandler alloc] init];
[handler setMap:&map];
Expand Down
14 changes: 7 additions & 7 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <mbgl/util/string.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/geojsonvt.hpp>

#include <algorithm>
#include <iostream>
Expand Down Expand Up @@ -57,7 +56,7 @@ const static bool uvVersionCheck = []() {

using namespace mbgl;

Map::Map(View& view_, FileSource& fileSource_, std::string featureJSON_)
Map::Map(View& view_, FileSource& fileSource_)
: loop(util::make_unique<uv::loop>()),
view(view_),
#ifdef DEBUG
Expand All @@ -78,11 +77,6 @@ Map::Map(View& view_, FileSource& fileSource_, std::string featureJSON_)
isClean.clear();
isRendered.clear();
isSwapped.test_and_set();

if (featureJSON_.length()) {
using GeoJSONVT = mapbox::util::geojsonvt::GeoJSONVT;
GeoJSONVT vt = GeoJSONVT(featureJSON_);
}
}

Map::~Map() {
Expand Down Expand Up @@ -641,7 +635,13 @@ void Map::updateSources(const util::ptr<StyleLayerGroup> &group) {
if (layer->bucket && layer->bucket->style_source) {
(*activeSources.emplace(layer->bucket->style_source).first)->enabled = true;
}

}
if (!annotationsSource) {
annotationsSource = *(activeSources.emplace(std::make_shared<StyleSource>()).first);
annotationsSource->info.type = SourceType::Live;
}
annotationsSource->enabled = true;
}

void Map::updateTiles() {
Expand Down
67 changes: 46 additions & 21 deletions src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,47 @@ Source::Source(SourceInfo& info_)
// The reason this isn't part of the constructor is that calling shared_from_this() in
// the constructor fails.
void Source::load(Map& map, FileSource& fileSource) {
if (info.url.empty()) {
loaded = true;
return;
}

util::ptr<Source> source = shared_from_this();

const std::string url = util::mapbox::normalizeSourceURL(info.url, map.getAccessToken());
fileSource.request({ Resource::Kind::JSON, url }, **map.loop, [source, &map](const Response &res) {
if (res.status != Response::Successful) {
Log::Warning(Event::General, "Failed to load source TileJSON: %s", res.message.c_str());
if (info.type != SourceType::Live) {
if (info.url.empty()) {
loaded = true;
return;
}

rapidjson::Document d;
d.Parse<0>(res.data.c_str());
util::ptr<Source> source = shared_from_this();

if (d.HasParseError()) {
Log::Warning(Event::General, "Invalid source TileJSON; Parse Error at %d: %s", d.GetErrorOffset(), d.GetParseError());
return;
}
const std::string url = util::mapbox::normalizeSourceURL(info.url, map.getAccessToken());
fileSource.request({ Resource::Kind::JSON, url }, **map.loop, [source, &map](const Response &res) {
if (res.status != Response::Successful) {
Log::Warning(Event::General, "Failed to load source TileJSON: %s", res.message.c_str());
return;
}

source->info.parseTileJSONProperties(d);
source->loaded = true;
rapidjson::Document d;
d.Parse<0>(res.data.c_str());

map.update();
if (d.HasParseError()) {
Log::Warning(Event::General, "Invalid source TileJSON; Parse Error at %d: %s", d.GetErrorOffset(), d.GetParseError());
return;
}

});
source->info.parseTileJSONProperties(d);
source->loaded = true;

map.update();
});
} else {
util::ptr<Source> source = shared_from_this();

fileSource.request({ Resource::Kind::JSON, "asset://threestates.geojson" }, **map.loop, [source, &map](const Response &res) { // FIXME
assert(res.status == Response::Successful);
assert(res.data.length());

source->geojsonvt = std::make_shared<mapbox::util::geojsonvt::GeoJSONVT>(res.data);
source->loaded = true;

map.update();
});
}
}

void Source::updateClipIDs(const std::map<Tile::ID, ClipID> &mapping) {
Expand Down Expand Up @@ -193,6 +206,18 @@ TileData::State Source::addTile(Map& map, uv::worker& worker,
info, fileSource);
} else if (info.type == SourceType::Raster) {
new_tile.data = std::make_shared<RasterTileData>(normalized_id, texturePool, info, fileSource);
} else if (info.type == SourceType::Live) {

new_tile.data = std::make_shared<VectorTileData>(normalized_id, map.getMaxZoom(), style,
glyphAtlas, glyphStore,
spriteAtlas, sprite,
info, fileSource);

new_tile.data->request(geojsonvt);
tile_data.emplace(new_tile.data->id, new_tile.data);

return TileData::State::parsed;

} else {
throw std::runtime_error("source type not implemented");
}
Expand Down
5 changes: 5 additions & 0 deletions src/mbgl/map/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/mat4.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/util/geojsonvt.hpp>

#include <cstdint>
#include <forward_list>
Expand Down Expand Up @@ -35,6 +37,7 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop
Source(SourceInfo&);

void load(Map&, FileSource&);
// void load(std::string& url);
void update(Map&, uv::worker&,
util::ptr<Style>,
GlyphAtlas&, GlyphStore&,
Expand Down Expand Up @@ -79,6 +82,8 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop

std::map<Tile::ID, std::unique_ptr<Tile>> tiles;
std::map<Tile::ID, std::weak_ptr<TileData>> tile_data;

util::ptr<mapbox::util::geojsonvt::GeoJSONVT> geojsonvt;
};

}
Expand Down
11 changes: 11 additions & 0 deletions src/mbgl/map/tile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ void TileData::request(uv::worker &worker, uv_loop_t &loop,
});
}

void TileData::request(util::ptr<mapbox::util::geojsonvt::GeoJSONVT> geojsonvt) {

printf("live loading %i,%i,%i\n", id.z, id.x, id.y);

mapbox::util::geojsonvt::Tile tile = geojsonvt->getTile(id.z, id.x, id.y);

// translate geojsonvt tile features into true vector tile here

// state = State::parsed;
}

void TileData::cancel() {
if (state != State::obsolete) {
state = State::obsolete;
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/map/tile_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/geojsonvt.hpp>

#include <atomic>
#include <exception>
Expand Down Expand Up @@ -52,6 +53,7 @@ class TileData : public std::enable_shared_from_this<TileData>,
~TileData();

void request(uv::worker&, uv_loop_t&, float pixelRatio, std::function<void ()> callback);
void request(util::ptr<mapbox::util::geojsonvt::GeoJSONVT> geojsonvt);
void reparse(uv::worker&, std::function<void ()> callback);
void cancel();
const std::string toString() const;
Expand Down
4 changes: 3 additions & 1 deletion src/mbgl/style/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ enum class SourceType : uint8_t {
Vector,
Raster,
GeoJSON,
Video
Video,
Live
};

MBGL_DEFINE_ENUM_CLASS(SourceTypeClass, SourceType, {
{ SourceType::Vector, "vector" },
{ SourceType::Raster, "raster" },
{ SourceType::GeoJSON, "geojson" },
{ SourceType::Video, "video" },
{ SourceType::Live, "live" },
});

// -------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 988e64e

Please sign in to comment.