forked from systemed/tilemaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_data.cpp
235 lines (207 loc) · 9.56 KB
/
shared_data.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "shared_data.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
SharedData::SharedData(Config &configIn, const class LayerDefinition &layers)
: layers(layers), config(configIn) {
sqlite=false;
mergeSqlite=false;
}
SharedData::~SharedData() { }
// *****************************************************************
// Define a layer (as read from the .json file)
uint LayerDefinition::addLayer(string name, uint minzoom, uint maxzoom,
uint simplifyBelow, double simplifyLevel, double simplifyLength, double simplifyRatio,
uint filterBelow, double filterArea, uint combinePolygonsBelow, bool sortZOrderAscending,
const std::string &source,
const std::vector<std::string> &sourceColumns,
bool allSourceColumns,
bool indexed,
const std::string &indexName,
const std::string &writeTo) {
bool isWriteTo = !writeTo.empty();
LayerDef layer = { name, minzoom, maxzoom, simplifyBelow, simplifyLevel, simplifyLength, simplifyRatio,
filterBelow, filterArea, combinePolygonsBelow, sortZOrderAscending,
source, sourceColumns, allSourceColumns, indexed, indexName,
std::map<std::string,uint>(), isWriteTo };
layers.push_back(layer);
uint layerNum = layers.size()-1;
layerMap[name] = layerNum;
if (writeTo.empty()) {
vector<uint> r = { layerNum };
layerOrder.push_back(r);
} else {
if (layerMap.count(writeTo) == 0) {
cerr << "ERROR: addLayer(): the layer to write, named as \"" + writeTo + "\", doesn't exist." << endl;
exit (EXIT_FAILURE);
}
uint lookingFor = layerMap[writeTo];
for (auto it = layerOrder.begin(); it!= layerOrder.end(); ++it) {
if (it->at(0)==lookingFor) {
it->push_back(layerNum);
}
}
}
return layerNum;
}
std::vector<bool> LayerDefinition::getSortOrders() {
std::vector<bool> orders;
for (auto &layer : layers) { orders.emplace_back(layer.sortZOrderAscending); }
return orders;
}
Value LayerDefinition::serialiseToJSONValue(rapidjson::Document::AllocatorType &allocator) const {
Value layerArray(kArrayType);
for (auto it = layers.begin(); it != layers.end(); ++it) {
if (it->writeTo) {
continue;
}
Value fieldObj(kObjectType);
for (auto jt = it->attributeMap.begin(); jt != it->attributeMap.end(); ++jt) {
Value k(jt->first.c_str(), allocator);
switch (jt->second) {
case 0: fieldObj.AddMember(k, "String" , allocator); break;
case 1: fieldObj.AddMember(k, "Number" , allocator); break;
case 2: fieldObj.AddMember(k, "Boolean", allocator); break;
}
}
Value layerObj(kObjectType);
Value name(it->name.c_str(), allocator);
layerObj.AddMember("id", name, allocator);
layerObj.AddMember("fields", fieldObj, allocator);
layerObj.AddMember("minzoom", it->minzoom, allocator);
layerObj.AddMember("maxzoom", it->maxzoom, allocator);
layerArray.PushBack(layerObj, allocator);
}
return layerArray;
}
std::string LayerDefinition::serialiseToJSON() const {
Document document;
document.SetObject();
Document::AllocatorType& allocator = document.GetAllocator();
document.AddMember("vector_layers", serialiseToJSONValue(allocator), allocator);
StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
document.Accept(writer);
string json(buffer.GetString(), buffer.GetSize());
return json;
}
// *****************************************************************
Config::Config() {
includeID = false, compress = true, gzip = true, highResolution = false;
clippingBoxFromJSON = false;
baseZoom = 0;
combineBelow = 0;
}
Config::~Config() { }
// ---- Enlarge existing bounding box
void Config::enlargeBbox(double cMinLon, double cMaxLon, double cMinLat, double cMaxLat) {
minLon = std::min(minLon, cMinLon);
maxLon = std::max(maxLon, cMaxLon);
minLat = std::min(minLat, cMinLat);
maxLat = std::max(maxLat, cMaxLat);
}
// ---- Read all config details from JSON file
void Config::readConfig(rapidjson::Document &jsonConfig, bool &hasClippingBox, Box &clippingBox) {
baseZoom = jsonConfig["settings"]["basezoom"].GetUint();
startZoom = jsonConfig["settings"]["minzoom" ].GetUint();
endZoom = jsonConfig["settings"]["maxzoom" ].GetUint();
includeID = jsonConfig["settings"]["include_ids"].GetBool();
highResolution = jsonConfig["settings"].HasMember("high_resolution") && jsonConfig["settings"]["high_resolution"].GetBool();
if (! jsonConfig["settings"]["compress"].IsString()) {
cerr << "\"compress\" should be any of \"gzip\",\"deflate\",\"none\" in JSON file." << endl;
exit (EXIT_FAILURE);
}
if (endZoom>15) {
cout << "**** WARNING ****" << endl;
cout << "You're generating tiles up to z" << endZoom << ". You probably don't want to do that." << endl;
cout << "Standard practice is to generate vector tiles up to z14. Your renderer will 'overzoom' the z14 tiles for higher resolutions." << endl;
cout << "tilemaker may have excessive memory, time, and space requirements at higher zooms. You can find more information in the docs/ folder." << endl;
cout << "**** WARNING ****" << endl;
}
#ifndef FAT_TILE_INDEX
if (endZoom>16) {
cerr << "Compile tilemaker with -DFAT_TILE_INDEX to enable tile output at zoom level 17 or greater" << endl;
exit (EXIT_FAILURE);
}
#endif
compressOpt = jsonConfig["settings"]["compress"].GetString();
combineBelow = jsonConfig["settings"].HasMember("combine_below") ? jsonConfig["settings"]["combine_below"].GetUint() : 0;
mvtVersion = jsonConfig["settings"].HasMember("mvt_version") ? jsonConfig["settings"]["mvt_version"].GetUint() : 2;
projectName = jsonConfig["settings"]["name"].GetString();
projectVersion = jsonConfig["settings"]["version"].GetString();
projectDesc = jsonConfig["settings"]["description"].GetString();
if (jsonConfig["settings"].HasMember("bounding_box")) {
clippingBoxFromJSON = true;
hasClippingBox = true;
minLon = jsonConfig["settings"]["bounding_box"][0].GetDouble();
minLat = jsonConfig["settings"]["bounding_box"][1].GetDouble();
maxLon = jsonConfig["settings"]["bounding_box"][2].GetDouble();
maxLat = jsonConfig["settings"]["bounding_box"][3].GetDouble();
clippingBox = Box(geom::make<Point>(minLon, lat2latp(minLat)),
geom::make<Point>(maxLon, lat2latp(maxLat)));
} else if (hasClippingBox) {
minLon = clippingBox.min_corner().x();
maxLon = clippingBox.max_corner().x();
minLat = latp2lat(clippingBox.min_corner().y());
maxLat = latp2lat(clippingBox.max_corner().y());
}
if (jsonConfig["settings"].HasMember("default_view")) {
defaultView = to_string(jsonConfig["settings"]["default_view"][0].GetDouble()) + "," +
to_string(jsonConfig["settings"]["default_view"][1].GetDouble()) + "," +
to_string(jsonConfig["settings"]["default_view"][2].GetInt());
}
// Check config is valid
if (endZoom > baseZoom) { cerr << "maxzoom must be the same or smaller than basezoom." << endl; exit (EXIT_FAILURE); }
if (! compressOpt.empty()) {
if (compressOpt == "gzip" ) { gzip = true; }
else if (compressOpt == "deflate") { gzip = false; }
else if (compressOpt == "none" ) { compress = false; }
else {
cerr << "\"compress\" should be any of \"gzip\",\"deflate\",\"none\" in JSON file." << endl;
exit (EXIT_FAILURE);
}
}
// Layers
rapidjson::Value& layerHash = jsonConfig["layers"];
for (rapidjson::Value::MemberIterator it = layerHash.MemberBegin(); it != layerHash.MemberEnd(); ++it) {
// Basic layer settings
string layerName = it->name.GetString();
int minZoom = it->value["minzoom"].GetInt();
int maxZoom = it->value["maxzoom"].GetInt();
string writeTo = it->value.HasMember("write_to") ? it->value["write_to"].GetString() : "";
int simplifyBelow = it->value.HasMember("simplify_below" ) ? it->value["simplify_below" ].GetInt() : 0;
double simplifyLevel = it->value.HasMember("simplify_level" ) ? it->value["simplify_level" ].GetDouble() : 0.01;
double simplifyLength = it->value.HasMember("simplify_length") ? it->value["simplify_length"].GetDouble() : 0.0;
double simplifyRatio = it->value.HasMember("simplify_ratio" ) ? it->value["simplify_ratio" ].GetDouble() : 2.0;
int filterBelow = it->value.HasMember("filter_below" ) ? it->value["filter_below" ].GetInt() : 0;
double filterArea = it->value.HasMember("filter_area" ) ? it->value["filter_area" ].GetDouble() : 0.5;
int combinePolyBelow=it->value.HasMember("combine_polygons_below") ? it->value["combine_polygons_below"].GetInt() : 0;
bool sortZOrderAscending = it->value.HasMember("z_order_ascending") ? it->value["z_order_ascending"].GetBool() : true;
string source = it->value.HasMember("source") ? it->value["source"].GetString() : "";
vector<string> sourceColumns;
bool allSourceColumns = false;
if (it->value.HasMember("source_columns")) {
if (it->value["source_columns"].IsTrue()) {
allSourceColumns = true;
} else {
for (uint i=0; i<it->value["source_columns"].Size(); i++) {
sourceColumns.push_back(it->value["source_columns"][i].GetString());
}
}
}
bool indexed=false; if (it->value.HasMember("index")) {
indexed=it->value["index"].GetBool();
}
string indexName = it->value.HasMember("index_column") ? it->value["index_column"].GetString() : "";
layers.addLayer(layerName, minZoom, maxZoom,
simplifyBelow, simplifyLevel, simplifyLength, simplifyRatio,
filterBelow, filterArea, combinePolyBelow, sortZOrderAscending,
source, sourceColumns, allSourceColumns, indexed, indexName,
writeTo);
cout << "Layer " << layerName << " (z" << minZoom << "-" << maxZoom << ")";
if (it->value.HasMember("write_to")) { cout << " -> " << it->value["write_to"].GetString(); }
cout << endl;
}
}