-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
291 lines (253 loc) · 12.5 KB
/
Copy pathmain.cpp
File metadata and controls
291 lines (253 loc) · 12.5 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <pqxx/pqxx>
#include <vector>
#include <string>
#include <cstdint>
#include <cmath>
#include <filesystem>
#include "wkb/ByteWrapper.h"
#include "wkb/WKBObject.h"
#include "wkb/WKBPoint.h"
#include "wkb/WKBMultiLineString.h"
#include "wkb/WKBPolygon.h"
#include "wkb/WKBMultiPoint.h"
#include "wkb/WKBMultiLineString.h"
#include "wkb/WKBMultiPolygon.h"
#include "wkb/TileUtils.h"
#include <fstream>
#include "simdjson/simdjson.h"
#include "genoa.h"
#include <cairo.h>
using namespace std;
using namespace simdjson;
enum RenderLayer { // Layer order is bottom to top btw, not in same order as Genoa Colours!!!
LAYER_BASE_BACKGROUND,
LAYER_SAND,
LAYER_OPEN_PUBLIC_AREA,
LAYER_OPEN_PRIVATE_AREA,
LAYER_ATHLETIC_FIELD,
LAYER_FOREST,
LAYER_WATER,
LAYER_PIER,
LAYER_MOUNTAIN,
LAYER_BUILDING,
LAYER_FOOTPATH,
LAYER_CYCLE_PATH,
LAYER_HIGHWAY_SERVICE,
LAYER_HIGHWAY_MINOR,
LAYER_HIGHWAY_MAJOR,
LAYER_RESTRICTED_AREA,
LAYER_NONE // MUST ALWAYS BE LAST!
};
// Map layers with their JSON string versions
std::unordered_map<std::string, RenderLayer> LayerStringMapping {
{ "RESTRICTED_AREA", LAYER_RESTRICTED_AREA },
{ "HIGHWAY_MAJOR", LAYER_HIGHWAY_MAJOR },
{ "HIGHWAY_MINOR", LAYER_HIGHWAY_MINOR },
{ "HIGHWAY_SERVICE", LAYER_HIGHWAY_SERVICE },
{ "CYCLE_PATH", LAYER_CYCLE_PATH },
{ "MOUNTAIN", LAYER_MOUNTAIN },
{ "SAND", LAYER_SAND },
{ "PIER", LAYER_PIER },
{ "FOOTPATH", LAYER_FOOTPATH },
{ "WATER", LAYER_WATER },
{ "ATHLETIC_FIELD", LAYER_ATHLETIC_FIELD },
{ "OPEN_PRIVATE_AREA", LAYER_OPEN_PRIVATE_AREA },
{ "OPEN_PUBLIC_AREA", LAYER_OPEN_PUBLIC_AREA },
{ "FOREST", LAYER_FOREST },
{ "BUILDING", LAYER_BUILDING },
{ "BASE_BACKGROUND", LAYER_BASE_BACKGROUND },
{ "_NO_RENDER", LAYER_NONE }
};
// Map layers to their colours (normalised from 0-1)
std::unordered_map<int, double> LayerColourMapping {
{ LAYER_BASE_BACKGROUND, (double)BASE_BACKGROUND/0xFF },
{ LAYER_OPEN_PUBLIC_AREA, (double)OPEN_PUBLIC_AREA/0xFF },
{ LAYER_OPEN_PRIVATE_AREA, (double)OPEN_PRIVATE_AREA/0xFF },
{ LAYER_ATHLETIC_FIELD, (double)ATHLETIC_FIELD/0xFF },
{ LAYER_SAND, (double)SAND/0xFF },
{ LAYER_FOREST, (double)FOREST/0xFF },
{ LAYER_WATER, (double)WATER/0xFF },
{ LAYER_PIER, (double)PIER/0xFF },
{ LAYER_MOUNTAIN, (double)MOUNTAIN/0xFF },
{ LAYER_BUILDING, (double)BUILDING/0xFF },
{ LAYER_FOOTPATH, (double)FOOTPATH/0xFF },
{ LAYER_CYCLE_PATH, (double)CYCLE_PATH/0xFF },
{ LAYER_HIGHWAY_SERVICE, (double)HIGHWAY_SERVICE/0xFF },
{ LAYER_HIGHWAY_MINOR, (double)HIGHWAY_MINOR/0xFF },
{ LAYER_HIGHWAY_MAJOR, (double)HIGHWAY_SERVICE/0xFF },
{ LAYER_RESTRICTED_AREA, (double)RESTRICTED_AREA/0xFF },
{ LAYER_NONE, (double)BASE_BACKGROUND/0xFF }
};
int main(int argc, char *argv[])
{
printf("%i arguments passed\n", argc);
for (int i=0; i<argc; i++) {
printf("argument %i - %s\n", i, argv[i]);
}
if (argc < 5) {
printf("ERROR: Too few arguments, expected:\n");
printf("GenoaTileRenderer.exe <PSQLConnectionString> <tileX> <tileY> <tileZ> [path to tile]");
printf("\n\nGenoaTileRenderer.exe \"dbname=gis user=postgres password=postgres host=127.0.0.1 port=5433\" 1284 129 16 ./tile.png");
return 1;
}
// Store arguments
std::filesystem::path executablePath(argv[0]);
std::string psqlConnectionString(argv[1]);
double tile_x = std::stod(argv[2]);
double tile_y = std::stod(argv[3]);
int tile_z = std::stoi(argv[4]); // Realistically this should just be 16
std::string outputPath(argv[5]);
Tile tile = Tile(Point(tile_x, tile_y), tile_z, 128);
printf("\n\nLoading JSON...");
std::vector<std::string> tags;
std::unordered_map<std::string, std::unordered_map<std::string, RenderLayer>> tagMap; // This map stores a C++ representation of the JSON tagMap file
try {
ondemand::parser parser;
padded_string json = padded_string::load((executablePath.parent_path() / "tagMap.json").string());
ondemand::document doc = parser.iterate(json);
ondemand::object layerTagMap = doc.get_object();
printf("\nLoaded Tags:\n");
// Iterate through every key in the JSON file
for(simdjson::simdjson_result<simdjson::fallback::ondemand::field> field : layerTagMap) {
// parses and writes out the key, after unescaping it,
// to a string buffer. It causes a performance penalty.
std::string tagName(field.unescaped_key().value());
std::cout << "- " << tagName << std::endl;
// Add OSM tag key to tagMap and tags
tags.push_back(tagName); // Stores list of tags to check for
tagMap.insert({tagName, {}}); // Stores the actual tag value to render layer mappings
// Map values of tag (iterates through keys in the object)
ondemand::object valueMap = field.value().get_object();
for(simdjson::simdjson_result<simdjson::fallback::ondemand::field> valueField : valueMap) {
std::string tagValue(valueField.unescaped_key().value());
std::string tagMapping = "_NO_RENDER"; // Do not render by default
if (!valueField.value().is_null()) {
tagMapping = std::string(valueField.value().get_string().value());
}
std::cout << " - " << tagValue << " : " << tagMapping << std::endl;
tagMap.at(tagName).insert({tagValue, LayerStringMapping.at(tagMapping)}); // Add item to tagmap (with string name mapped to the RenderLayer enum)
}
}
} catch (simdjson::simdjson_error &e) {
printf("\n\nERROR: Could not read file './tagMap.json' - simdjson_error\n");
return 1;
} catch (int e) {
printf("\n\nERROR: Could not read file './tagMap.json' - %i\n", e);
return 1;
}
printf("\n\n");
printf("\n\nGenoa Tile Renderer\n");
printf("Connecting to postGIS\n");
try {
// @TODO: Add arguments for this
pqxx::connection pConn(psqlConnectionString);
pqxx::work transaction(pConn);
printf("Connected!\n");
// Initialise rendererwhil
printf("Initialising Cairo\n");
cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 128, 128);
cairo_t *cr = cairo_create(surface);
cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE); // IMPORTANT for Genoa
// Set background colour
cairo_set_source_rgb(cr, LayerColourMapping.at(LAYER_NONE), LayerColourMapping.at(LAYER_NONE), LayerColourMapping.at(LAYER_NONE));
cairo_rectangle(cr, 0, 0, 128, 128);
cairo_fill(cr);
// Get the objects within the tile from the DB (and the attributes)
pqxx::result result = transaction.exec_params( "SELECT aeroway, amenity, barrier, building, highway, landuse, leisure, military, \"natural\", railway, waterway, ST_AsBinary(way) FROM planet_osm_polygon WHERE way && ST_TileEnvelope($1, $2, $3) AND boundary isnull\n"
"UNION\n"
"SELECT aeroway, amenity, barrier, building, highway, landuse, leisure, military, \"natural\", railway, waterway, ST_AsBinary(way) FROM planet_osm_line WHERE way && ST_TileEnvelope($1, $2, $3) AND boundary isnull AND route isnull AND NOT (railway isnull AND highway isnull) AND (railway isnull OR railway != 'subway')"
";",
tile_z,
tile_y,
tile_x
);
transaction.commit();
// Prepare data to render
std::vector<WKBObject*> emptyLayer(0);
std::vector<std::vector<WKBObject*>> layers(LAYER_NONE+1, emptyLayer); // +1 because LAYER_NONE itself needs to be available as an empty layer despite it not actually being rendered in the end
// Iterate through rows (every element)
printf("Found %i rows\n", result.size());
for (int rowIndex=0; rowIndex < result.size(); rowIndex++) {
if (result[rowIndex]["ST_AsBinary"].is_null()) { // Way is empty for whatever reason
continue;
} else {
// Store the layer to render to
RenderLayer targetLayer = LAYER_NONE; // Default to not rendering
/**
* Iterate through all tags returned
* Determines the layer to render to by first tag with valid value that has an assigned render layer
* (tagMap.json is basically highest-priority to lowest-priority)
*/
for (uint32_t i=0; i < tags.size(); i++) {
std::string tagName = tags[i];
if (result[rowIndex][tagName].is_null()) {
continue; // Ignore null values
}
std::string tagValue = result[rowIndex][tagName].as<std::string>(); // The value of this particular tag
// Get the render layer from the tagmap
if (tagMap.at(tagName).count(tagValue) > 0) { // Check tag values
targetLayer = tagMap.at(tagName).at(tagValue);
break; // Tag has been selected
} else if (tagMap.at(tagName).count("*") > 0) { // Handle "default" case
targetLayer = tagMap.at(tagName).at("*");
break; // Tag has been selected
}
}
// Load the WKB data into a CURSED ByteWrapper object
// TODO: This is really hacky (works tho)
std::basic_string<std::byte> buffer = result[rowIndex]["ST_AsBinary"].as<std::basic_string<std::byte>>();
// Create a reader
ByteWrapper byteWrapper(buffer);
// Read endianness and the type that this is
//bool littleEndian = byteWrapper.read() == std::byte(1); // TODO: Should this be checked?
byteWrapper.seek(1); // Skip the LE byte since we don't use it
uint32_t wkbType = byteWrapper.readType<uint32_t>();
byteWrapper.seek(0, true); // WKB classes read from 0
// Handle the type of element
switch (wkbType) {
case wkbGeometryTypePoint:
case wkbGeometryTypeMultiPoint:
// We do not render points
break;
case wkbGeometryTypeLineString:
layers.at(targetLayer).push_back(new WKBLineString(&byteWrapper));
break;
case wkbGeometryTypePolygon:
layers.at(targetLayer).push_back(new WKBPolygon(&byteWrapper));
break;
case wkbGeometryTypeMultiLineString:
layers.at(targetLayer).push_back(new WKBMultiLineString(&byteWrapper));
break;
case wkbGeometryTypeMultiPolygon:
layers.at(targetLayer).push_back(new WKBMultiPolygon(&byteWrapper));
break;
default:
printf("ERROR!!!\nUNKNOWN TYPE: %u\nFound in row: %i\n", wkbType, rowIndex);
break;
}
}
}
// Rendering stage (look through elements of every layer in order and render them):
printf("Rendering image...\n");
for (int renderLayer=0; renderLayer < LAYER_NONE; renderLayer++ ) { // Get layers (ignores LAYER_NONE so it is not rendered)
for (long unsigned int j=0; j < layers.at(renderLayer).size(); j++ ) { // Get objects in layer
layers.at(renderLayer).at(j)->render(cr, &tile, LayerColourMapping.at(renderLayer), LayerColourMapping.at(renderLayer), LayerColourMapping.at(renderLayer), 2); // Render layer
}
}
printf("Writing png...\n");
cairo_destroy (cr);
cairo_surface_write_to_png (surface, outputPath.c_str());
cairo_surface_destroy (surface);
printf("\nDone!\n\n");
printf("Disconnecting from postGIS...\n");
pConn.close();
return 0;
} catch (std::exception const &e) { // This is ugly but it's how you're supposed to do it apparently, I'm not sorry!
std::cerr << e.what() << std::endl;
return 1;
}
}