-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
90 lines (78 loc) · 2.81 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
#include "mapget/http-datasource/datasource-server.h"
#include "mapget/log.h"
using namespace mapget;
using json = nlohmann::json;
namespace fs = std::filesystem;
// Class to encapsulate all logic of our data source
class MyRemoteDataSource
{
private:
DataSourceServer ds;
int port = 0;
int servedTiles = 0;
public:
// The constructor
MyRemoteDataSource(const std::string& jsonFilename, int port)
: ds(loadDataSourceInfoFromJson(jsonFilename)), port(port)
{
// Handle tile requests
ds.onTileFeatureRequest(
[this](auto&& tile)
{
fill(tile);
});
}
void fill(TileFeatureLayer::Ptr const& tile)
{
// Add some ID parts that are shared by all features in the tile.
tile->setIdPrefix({{"areaId", "BestArea"}});
// Create a feature with line geometry
auto feature1 = tile->newFeature("Way", {{"wayId", 42}});
auto line = feature1->geom()->newGeometry(GeomType::Line, 2);
line->append({41., 10.});
line->append({43., 11.});
// Use high-level geometry API
feature1->addPoint(tile->tileId().center());
feature1->addPoints({tile->tileId().ne(), tile->tileId().sw()});
feature1->addLine({{41.5, 10.5, 0}, {41.6, 10.7}});
feature1->addMesh({{41.5, 10.5, 0}, {41.6, 10.7}, {41.5, 10.3}});
feature1->addPoly({{41.5, 10.5, 0}, {41.6, 10.7}, {41.5, 10.3}, {41.8, 10.9}});
// Add a fixed attribute
feature1->attributes()->addField("main_ingredient", "Pepper");
// Add an attribute layer
auto attrLayer = feature1->attributeLayers()->newLayer("cheese");
auto attr = attrLayer->newAttribute("mozzarella");
attr->validity()->newDirection(Validity::Direction::Positive);
attr->addField("smell", "neutral");
// Set some additional info on the tile
tile->setInfo("servedTiles", ++servedTiles);
}
// The function to load the DataSourceInfo from a JSON file
static DataSourceInfo loadDataSourceInfoFromJson(const std::string& filename)
{
std::string full_path = fs::current_path().string() + "/" + filename;
log().info("Reading info from {}", full_path);
std::ifstream i(full_path);
json j;
i >> j;
return DataSourceInfo::fromJson(j);
}
// The function to start the server
void run()
{
ds.go("0.0.0.0", port);
log().info("Running...");
ds.waitForSignal();
}
};
int main(int argc, char** argv)
{
int port = (argc > 1) ? std::stoi(argv[1]) : 0; // get port from command line argument
log().info("Running on port {}", port);
MyRemoteDataSource ds("sample_datasource_info.json", port);
ds.run();
return 0;
}