-
Notifications
You must be signed in to change notification settings - Fork 3
/
clustering-vt.cpp
204 lines (161 loc) · 7.16 KB
/
clustering-vt.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <mapbox/geojson.hpp>
#include <mapbox/geojson_impl.hpp>
#include <rapidjson/document.h>
#include "rapidjson/istreamwrapper.h"
#include <vtzero/builder.hpp>
#include <vtzero/index.hpp>
#define DEBUG_TIMER false
#include <supercluster.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <filesystem>
const int maxZoom = 13;
struct properties_to_point_feature_builder {
vtzero::point_feature_builder& featureBuilder;
std::string key = "";
void operator()(mapbox::feature::null_value_t) {
featureBuilder.add_property(key, NULL);
}
void operator()(bool val) {
featureBuilder.add_property(key, val);
}
void operator()(int64_t val) {
featureBuilder.add_property(key, val);
}
void operator()(uint64_t val) {
featureBuilder.add_property(key, val);
}
void operator()(double val) {
featureBuilder.add_property(key, val);
}
void operator()(const std::string& val) {
featureBuilder.add_property(key, val);
}
void operator()(const std::vector<mapbox::feature::value>& array) {
return;
}
void operator()(const std::unordered_map<std::string, mapbox::feature::value>& map) {
for (const auto& property : map) {
key.assign(property.first.data());
mapbox::feature::value::visit(property.second, *this);
}
}
};
void write_data_to_file(const std::string& buffer, const std::string& filename) {
std::ofstream stream{filename, std::ios_base::out | std::ios_base::binary};
if (!stream) {
throw std::runtime_error{std::string{"Can not open file '"} + filename + "'"};
}
stream.exceptions(std::ifstream::failbit);
stream.write(buffer.data(), static_cast<std::streamsize>(buffer.size()));
stream.close();
}
/**
* Recursively generates all tiles.
*
* The firstTileX and firstTileY parameters are the coordinates of
* the top left tile of the group of subtiles that we want to generate.
* See https://docs.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system
*
* For each of the four tiles of the group, if a tile contains features and contains at least
* one cluster, all its child tiles (four tiles again) are generated for the next zoom level
* by the same function recursively.
*
* The recursion stop when we read the max zoom level + 1 or if there is no tiles left to generate because
* there are no clusters left.
*/
void generate_tiles(const short zoom,
const int firstTileX,
const int firstTileY,
const std::string& outputFolder,
mapbox::supercluster::Supercluster& superclusterIndex) {
for (int x = firstTileX; x < firstTileX + 2; x++) {
for (int y = firstTileY; y < firstTileY + 2; y++) {
mapbox::feature::feature_collection<std::int16_t> tile = superclusterIndex.getTile(zoom, x, y);
if (tile.size() != 0) {
vtzero::tile_builder tileBuilder;
vtzero::layer_builder layerBuilder{tileBuilder, "clustersLayer", 1, 256};
vtzero::key_index<std::unordered_map> idx{layerBuilder};
bool hasCluster = false;
for (auto &f : tile) {
const mapbox::geometry::point<std::int16_t> featurePoint = f.geometry.get<mapbox::geometry::point<std::int16_t>>();
{
vtzero::point_feature_builder featureBuilder{layerBuilder};
featureBuilder.add_point(featurePoint.x, featurePoint.y);
properties_to_point_feature_builder {featureBuilder}(f.properties);
//Add the clusterExpansionZoom to a property if the feature is a cluster.
const auto iterator = f.properties.find("cluster_id");
if (iterator != f.properties.end() && iterator->second.get<uint64_t>()) {
hasCluster = true;
uint8_t expansionZoom = superclusterIndex.getClusterExpansionZoom(f.properties["cluster_id"].get<uint64_t>());
featureBuilder.add_property("clusterExpansionZoom", expansionZoom);
}
featureBuilder.commit();
}
}
const auto data = tileBuilder.serialize();
std::ostringstream folderPath;
folderPath << outputFolder << "/" << zoom << "/" << x;
std::ostringstream pbfPath;
pbfPath << outputFolder << "/" << zoom << "/" << x << "/" << y << ".pbf";
std::filesystem::create_directories(folderPath.str());
write_data_to_file(data, pbfPath.str());
//If there is features and clusters inside the tile, we need to also generate its child tiles.
//Generate until maxZoom + 1 to also add features where there is no clusters left.
if (hasCluster && zoom + 1 <= maxZoom + 1) {
generate_tiles(
zoom + 1,
2*x, //To understand how we get the subtiles coordinates, check the "subtiles" section: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
2*y, //We only call the generate_tiles for the subtile in the top left corner of the four subtiles.
outputFolder,
superclusterIndex
);
}
}
//At zoom level = 0 there is only 1 one tile, so we should return after it has been generated.
if (zoom == 0 && firstTileX == 0 && firstTileY == 0) {
return;
}
}
}
}
int main(int argc, char *argv[]) {
if (argc == 1) {
std::cerr << "The output folder is missing!\n";
return 1;
}
if (argc == 2) {
std::cerr << "The radius is missing!\n";
return 1;
}
//Read GeoJSON data from stdin
std::cin.sync_with_stdio(false);
if (std::cin.rdbuf()->in_avail()) {
mapbox::supercluster::Timer timer;
rapidjson::IStreamWrapper isw(std::cin);
mapbox::geojson::rapidjson_document d;
d.ParseStream<rapidjson::IStreamWrapper>(isw);
timer("parse JSON");
const auto &json_features = d["features"];
mapbox::feature::feature_collection<double> features;
features.reserve(json_features.Size());
for (auto itr = json_features.Begin(); itr != json_features.End(); ++itr) {
mapbox::feature::feature<double> feature = mapbox::geojson::convert<mapbox::feature::feature<double>>(*itr);
features.push_back(feature);
}
timer("convert to geometry.hpp");
mapbox::supercluster::Options options;
options.maxZoom = maxZoom;
options.radius = std::atoi(argv[2]);
options.extent = 256;
mapbox::supercluster::Supercluster index(features, options);
timer("total supercluster time");
generate_tiles(0, 0, 0, argv[1], index);
timer("total tiles generation time");
}else {
std::cerr << "GeoJSON is needed in stdin!\n";
return 1;
}
}