Skip to content

Commit

Permalink
misc and add BingMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 16, 2024
1 parent 16e0763 commit 7d2e03b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 9 deletions.
1 change: 1 addition & 0 deletions examples/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ if (CURL_FOUND)
"${CMAKE_CURRENT_SOURCE_DIR}/geo/geometries/MapNodeGeometry.hpp"

"${CMAKE_CURRENT_SOURCE_DIR}/geo/providers/MapProvider.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/geo/providers/BingMapsProvider.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/geo/providers/OpenStreetMapsProvider.hpp"

"${CMAKE_CURRENT_SOURCE_DIR}/geo/utils/UnitUtils.hpp"
Expand Down
101 changes: 101 additions & 0 deletions examples/libs/geo/providers/BingMapsProvider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// https://github.com/tentone/geo-three/blob/master/source/providers/BingMapsProvider.ts

#ifndef THREEPP_BINGMAPSPROVIDER_HPP
#define THREEPP_BINGMAPSPROVIDER_HPP

#include "../../utility/URLFetcher.hpp"
#include "../providers/MapProvider.hpp"

#include "threepp/loaders/ImageLoader.hpp"

#include <fstream>
#include <sstream>


namespace threepp {

class BingMapProvider: public MapProvider {

public:
enum class Type {
ARIAL,
ROAD
};

explicit BingMapProvider(Type type = Type::ROAD): type(type) {

this->maxZoom = 19;
this->minZoom = 1;
}

Image fetchTile(int zoom, int x, int y) override {

std::stringstream ss;
ss << "http://ecn." << subDomain << ".tiles.virtualearth.net/tiles/" << getMapKey() << quadKey(zoom, x, y) << ".jpeg?g=1173";
const auto url = ss.str();

std::vector<unsigned char> data;
std::string cacheDir{".cache/bingmaps/" + getMapKey() + "/"};
std::string cacheFilePath = cacheDir + std::to_string(zoom) + "_" + std::to_string(x) + "_" + std::to_string(y) + ".jpeg";

if (std::filesystem::exists(cacheFilePath)) {
// Load from cache file
std::ifstream file(cacheFilePath, std::ios::binary);
data = std::vector<unsigned char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
} else if (urlFetcher.fetch(url, data)) {
// Save to cache file
std::filesystem::create_directories(cacheDir);
std::ofstream file(cacheFilePath, std::ios::binary);
file.write(reinterpret_cast<const char*>(data.data()), data.size());
}

return *loader.load(data, 4, true);
}

private:
Type type;
std::string subDomain{"t1"};

ImageLoader loader;
utils::UrlFetcher urlFetcher;

std::string getMapKey() {
switch (type) {
case Type::ARIAL:
return "a";
case Type::ROAD:
return "r";
}
return "";
}

/**
* Convert x, y, zoom quadtree to a bing maps specific quadkey.
*
* Adapted from original C# code at https://msdn.microsoft.com/en-us/library/bb259689.aspx.
*/
static std::string quadKey(int zoom, int x, int y) {
std::string quad;

for (int i = zoom; i > 0; i--) {
const int mask = 1 << (i - 1);
int cell = 0;

if ((x & mask) != 0) {
cell++;
}

if ((y & mask) != 0) {
cell += 2;
}

quad += std::to_string(cell);
}

return quad;
}
};

}// namespace threepp

#endif//THREEPP_BINGMAPSPROVIDER_HPP
16 changes: 7 additions & 9 deletions examples/libs/geo/providers/OpenStreetMapsProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ namespace threepp {
class OpenStreetMapProvider: public MapProvider {

public:
explicit OpenStreetMapProvider(std::string address = "https://a.tile.openstreetmap.org/")
: address(std::move(address)) {
explicit OpenStreetMapProvider() {

this->maxZoom = 19;
}
Expand All @@ -30,30 +29,29 @@ namespace threepp {
const auto url = ss.str();

std::vector<unsigned char> data;
std::string cacheFilePath = ".cache/openstreetmaps/" + std::to_string(zoom) + "_" + std::to_string(x) + "_" + std::to_string(y) + "." + format;
std::string cacheFilePath = cacheDir + std::to_string(zoom) + "_" + std::to_string(x) + "_" + std::to_string(y) + "." + format;

if (std::filesystem::exists(cacheFilePath)) {
// Load from cache file
std::ifstream file(cacheFilePath, std::ios::binary);
data = std::vector<unsigned char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
} else if (urlFetcher.fetch(url, data)) {
// Save to cache file
std::filesystem::create_directories(".cache/openstreetmaps/");
std::filesystem::create_directories(cacheDir);
std::ofstream file(cacheFilePath, std::ios::binary);
file.write(reinterpret_cast<const char*>(data.data()), data.size());
}

return *loader.load(data, format == "png" ? 4 : 3, true);
return *loader.load(data, 4, true);
}

private:
std::string address;
std::string format = "png";

ImageLoader loader;
utils::UrlFetcher urlFetcher;

std::unordered_map<std::string, std::vector<unsigned char>> cache_{};
std::string format{"png"};
std::string cacheDir{".cache/openstreetmaps/"};
std::string address{"https://a.tile.openstreetmap.org/"};
};

}// namespace threepp
Expand Down
1 change: 1 addition & 0 deletions examples/projects/MapView/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "geo/MapView.hpp"
#include "geo/providers/OpenStreetMapsProvider.hpp"
#include "geo/providers/BingMapsProvider.hpp"
#include "geo/utils/UnitUtils.hpp"
#include "geo/lod/LODRaycast.hpp"
#include "geo/lod/LODRadial.hpp"
Expand Down

0 comments on commit 7d2e03b

Please sign in to comment.