Skip to content

Commit

Permalink
added networkdatasource abstraction to datasource and rearranged data…
Browse files Browse the repository at this point in the history
…source/networkdatasource/mapzenvectortilejson functions based on the new networkdatasource abstraction
  • Loading branch information
tallytalwar committed Oct 23, 2014
1 parent 5c31fc9 commit cd01d9a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 80 deletions.
92 changes: 49 additions & 43 deletions core/dataSource/dataSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@ static const int MAX_FETCH_TRY = 3;
*/
static std::regex regObj;

void DataSource::ClearGeoRoots() {
for (auto& mapValue : m_JsonRoots) {
mapValue.second->clear();
}
m_JsonRoots.clear();
}

size_t DataSource::JsonRootSize() {
return m_JsonRoots.size();
}


//----Curl Helper Functions----

Expand Down Expand Up @@ -59,26 +48,46 @@ static void curlInit(CURLM *_curlMulti, std::string _url, std::stringstream *_ou
}


//---- MapzenVectorTileJson Implementation----
//---- DataSource Implementation----

MapzenVectorTileJson::MapzenVectorTileJson() {
m_urlTemplate = "http://vector.mapzen.com/osm/all/[z]/[x]/[y].json";

// check if template is good
std::string regex_str = "([a-z\\./:0-9]+)/(\\[z\\])/(\\[x\\])/(\\[y\\])([a-z\\.]+)";
regObj.assign(regex_str, std::regex_constants::icase);
std::sregex_iterator it(m_urlTemplate.begin(), m_urlTemplate.end(), regObj);
if(m_urlTemplate.compare((*it).str()) == 0) {
logMsg("\n***urlTemplate for MapzenVectorTileJson datasource is good.\n");
void DataSource::clearGeoRoots() {
for (auto& mapValue : m_JsonRoots) {
mapValue.second->clear();
}
m_JsonRoots.clear();
}

size_t DataSource::jsonRootSize() {
return m_JsonRoots.size();
}

std::unique_ptr<std::string> MapzenVectorTileJson::constructURL(const TileID& _tileCoord) {
bool DataSource::checkDataExists(const TileID& _tileID) {
if(m_JsonRoots.find(_tileID) != m_JsonRoots.end()) {
return true;
}
else {
return false;
}
}

std::shared_ptr<Json::Value> DataSource::getData(const TileID& _tileID) {
if(checkDataExists(_tileID)) {
return m_JsonRoots[_tileID];
}
else {
return nullptr;
}
}


//---- NetworkDataSource Implementation----

std::unique_ptr<std::string> NetworkDataSource::constructURL(const TileID& _tileCoord) {
std::unique_ptr<std::string> pTileUrl(nullptr);
std::string xVal(std::to_string(_tileCoord.x));
std::string yVal(std::to_string(_tileCoord.y));
std::string zVal(std::to_string(_tileCoord.z));

std::string tileURL = m_urlTemplate;
size_t pos = 0;
if( (pos = tileURL.find("[x]", pos)) != std::string::npos) {
Expand Down Expand Up @@ -106,6 +115,22 @@ std::unique_ptr<std::string> MapzenVectorTileJson::constructURL(const TileID& _t
return std::move(pTileUrl);
}

//---- MapzenVectorTileJson Implementation----

MapzenVectorTileJson::MapzenVectorTileJson() {

// Override NetworkDataSource::m_urlTemplate
m_urlTemplate = "http://vector.mapzen.com/osm/all/[z]/[x]/[y].json";

// check if template is good
std::string regex_str = "([a-z\\./:0-9]+)/(\\[z\\])/(\\[x\\])/(\\[y\\])([a-z\\.]+)";
regObj.assign(regex_str, std::regex_constants::icase);
std::sregex_iterator it(m_urlTemplate.begin(), m_urlTemplate.end(), regObj);
if(m_urlTemplate.compare((*it).str()) == 0) {
logMsg("\n***urlTemplate for MapzenVectorTileJson datasource is good.\n");
}
}

//TODO: Figure out a way to get tileIDs from curlEasy Handle!(Specifically if we are using curl multi handle).
//Use of this helper method "might" be avoided if we go curleasy and c++11 async route.
TileID MapzenVectorTileJson::extractIDFromUrl(const std::string& _url) {
Expand All @@ -129,7 +154,7 @@ TileID MapzenVectorTileJson::extractIDFromUrl(const std::string& _url) {
return TileID(xVal, yVal, zVal);
}

bool MapzenVectorTileJson::LoadTile(const std::vector<TileID>& _tileCoords) {
bool MapzenVectorTileJson::loadTile(const std::vector<TileID>& _tileCoords) {
std::vector<std::unique_ptr<std::string>> urls;

if(_tileCoords.size() == 0) {
Expand Down Expand Up @@ -314,22 +339,3 @@ bool MapzenVectorTileJson::LoadTile(const std::vector<TileID>& _tileCoords) {
urls.clear();
return true;
}

bool MapzenVectorTileJson::CheckDataExists(const TileID& _tileID) {
if(m_JsonRoots.find(_tileID) != m_JsonRoots.end()) {
return true;
}
else {
return false;
}
}

std::shared_ptr<Json::Value> MapzenVectorTileJson::GetData(const TileID& _tileID) {
if(CheckDataExists(_tileID)) {
return m_JsonRoots[_tileID];
}
else {
return nullptr;
}
}

92 changes: 57 additions & 35 deletions core/dataSource/dataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,88 @@ class TileData {
};


// TODO: divide DataSource into network and non-network dataSources.
// Same has been done on the webgl tangram. Follow the same pattern.
class DataSource {
protected:
// map of tileIDs to json data for that tile
std::map< TileID, std::shared_ptr<Json::Value> > m_JsonRoots;

/* m_urlTemplate needs to be defined for every network dataSource */
std::string m_urlTemplate;


public:
DataSource() {}

/*
* Does all the curl network calls to load the tile data and fills the data associated with a tileID
*/
virtual bool LoadTile(const std::vector<TileID>& _tileCoords) = 0;

/* Returns the data corresponding to a tileID */
virtual std::shared_ptr<Json::Value> GetData(const TileID& _tileID) = 0;

/* Checks if data exists for a specific tileID */
virtual bool CheckDataExists(const TileID& _tileID) = 0;

/*
* constructs the URL for a tile based on tile coordinates/IDs.
* Used by LoadTile to construct URL
virtual bool loadTile(const std::vector<TileID>& _tileCoords) = 0;

/*
* Returns the data corresponding to a tileID
*/
virtual std::unique_ptr<std::string> constructURL(const TileID& _tileCoord) = 0;

/*
* extracts tileIDs from a url
* Used by LoadTile to extract tileIDs from curl url.
virtual std::shared_ptr<Json::Value> getData(const TileID& _tileID);

/*
* Checks if data exists for a specific tileID
*/
virtual TileID extractIDFromUrl(const std::string& _url) = 0;
virtual bool checkDataExists(const TileID& _tileID);

/*
* clears all data associated with this dataSource
*/
void ClearGeoRoots();

void clearGeoRoots();
/*
* returns the number of tiles having data wrt this datasource
*/
size_t JsonRootSize();

DataSource() {}
size_t jsonRootSize();

virtual ~DataSource() {
m_JsonRoots.clear();
}
};

//Extends DataSource class to read MapzenVectorTileJsons.
class MapzenVectorTileJson: public DataSource {
class NetworkDataSource : public DataSource {
protected:
/*
* m_urlTemplate needs to be defined for every network dataSource
*/
std::string m_urlTemplate;

/*
* constructs the URL for a tile based on tile coordinates/IDs.
* Used by LoadTile to construct URL
*/
virtual std::unique_ptr<std::string> constructURL(const TileID& _tileID);

/*
* extracts tileIDs from a url
* Used by LoadTile to extract tileIDs from curl url.
* NOTE: every tile source will implement its own extractID as order of x,y and z can be different
* example: mapzen vector tile has z/x/y in its url
*/
virtual TileID extractIDFromUrl(const std::string& _url) = 0;

public:
MapzenVectorTileJson();
virtual std::unique_ptr<std::string> constructURL(const TileID& _tileCoord) override;
NetworkDataSource() {};
virtual ~NetworkDataSource() {
m_urlTemplate.clear();
}
};

//Extends NetworkDataSource class to read MapzenVectorTileJsons.
class MapzenVectorTileJson : public NetworkDataSource {
private:
virtual TileID extractIDFromUrl(const std::string& _url) override;
virtual bool LoadTile(const std::vector<TileID>& _tileCoords) override;
virtual std::shared_ptr<Json::Value> GetData(const TileID& _tileID) override;
virtual bool CheckDataExists(const TileID& _tileID) override;
public:
MapzenVectorTileJson();
virtual bool loadTile(const std::vector<TileID>& _tileCoords) override;
virtual ~MapzenVectorTileJson() {}
};

class TopoJsonNetSrc : public NetworkDataSource {
};

class MapboxFormatNetSrc : public NetworkDataSource {
};

// --Support for tiled geoJson but no network (basically supports a geojson on the filesystem)
class GeoJsonFileSrc : public DataSource {
};
4 changes: 2 additions & 2 deletions core/tileManager/tileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ bool TileManager::updateTileSet() {

for (const auto& source : m_dataSources) {
logMsg("Loading tiles...\n");
newTileLoadSuccess = source->LoadTile(m_tilesToAdd);
newTileLoadSuccess = source->loadTile(m_tilesToAdd);
}

if(!newTileLoadSuccess) {
Expand All @@ -96,7 +96,7 @@ bool TileManager::updateTileSet() {
std::unique_ptr<MapTile> tile(new MapTile(tileID, m_viewModule->getMapProjection()));
for (const auto& source : m_dataSources) {
// Get the previously fetched tile data
std::shared_ptr<Json::Value> json = source->GetData(tileID);
std::shared_ptr<Json::Value> json = source->getData(tileID);
logMsg(" Retrieved JSON\n");
if(!json) {
logMsg(" ***json root is null, tile was not read properly\n");
Expand Down

0 comments on commit cd01d9a

Please sign in to comment.