-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.cpp
More file actions
138 lines (113 loc) · 4.5 KB
/
main.cpp
File metadata and controls
138 lines (113 loc) · 4.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
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include <limits>
#include <cmath>
#include "bmp.hpp"
struct Node {
double lat;
double lon;
};
std::map<std::string, Node> nodes;
std::vector<std::vector<std::string>> ways;
constexpr int WIDTH = 5000;
constexpr int HEIGHT = 5000;
// Draw a line using Bresenham's algorithm
void draw_line(BMP& bmp, int x1, int y1, int x2, int y2, const color& c) {
int dx = std::abs(x2 - x1);
int dy = -std::abs(y2 - y1);
int sx = x1 < x2 ? 1 : -1;
int sy = y1 < y2 ? 1 : -1;
int err = dx + dy;
while (true) {
bmp.set_pixel(x1, y1, c.r, c.g, c.b);
if (x1 == x2 && y1 == y2) break;
int e2 = 2 * err;
if (e2 >= dy) { err += dy; x1 += sx; }
if (e2 <= dx) { err += dx; y1 += sy; }
}
}
// Convert lat/lon to x/y using calculated scale
void latlon_to_xy(double lat, double lon, int& x, int& y,
double min_lat, double min_lon,
double scale_x, double scale_y) {
x = static_cast<int>((lon - min_lon) * scale_x);
y = HEIGHT - static_cast<int>((lat - min_lat) * scale_y); // invert Y for BMP
}
void parse_osm(const std::string& filename,
double& min_lat, double& max_lat,
double& min_lon, double& max_lon) {
std::ifstream in(filename);
std::string line;
min_lat = std::numeric_limits<double>::max();
max_lat = std::numeric_limits<double>::lowest();
min_lon = std::numeric_limits<double>::max();
max_lon = std::numeric_limits<double>::lowest();
std::vector<std::string> current_way;
while (std::getline(in, line)) {
if (line.find("<node") != std::string::npos && line.find("/>") != std::string::npos) {
size_t id_pos = line.find("id=\"");
size_t lat_pos = line.find("lat=\"");
size_t lon_pos = line.find("lon=\"");
if (id_pos == std::string::npos || lat_pos == std::string::npos || lon_pos == std::string::npos) continue;
std::string id = line.substr(id_pos + 4, line.find("\"", id_pos + 4) - (id_pos + 4));
double lat = std::stod(line.substr(lat_pos + 5, line.find("\"", lat_pos + 5) - (lat_pos + 5)));
double lon = std::stod(line.substr(lon_pos + 5, line.find("\"", lon_pos + 5) - (lon_pos + 5)));
min_lat = std::min(min_lat, lat);
max_lat = std::max(max_lat, lat);
min_lon = std::min(min_lon, lon);
max_lon = std::max(max_lon, lon);
nodes[id] = {lat, lon};
} else if (line.find("<way") != std::string::npos) {
current_way.clear();
} else if (line.find("<nd") != std::string::npos) {
size_t ref_pos = line.find("ref=\"");
if (ref_pos != std::string::npos) {
std::string ref = line.substr(ref_pos + 5, line.find("\"", ref_pos + 5) - (ref_pos + 5));
current_way.push_back(ref);
}
} else if (line.find("</way>") != std::string::npos) {
if (current_way.size() >= 2) {
ways.push_back(current_way);
}
}
}
}
int main(int argc, char* argv[]) {
std::string input_file;
std::string output_file;
if (argc != 3) {
std::cout << "Usage: ./main <input> <ouput> \n" ;
return -1;
}
input_file =argv[1];
output_file = argv[2];
std::cout << "Using input file: " << input_file << std::endl;
std::cout << "Using output file: " << output_file << std::endl;
double min_lat, max_lat, min_lon, max_lon;
parse_osm(input_file, min_lat, max_lat, min_lon, max_lon);
double lon_range = max_lon - min_lon;
double lat_range = max_lat - min_lat;
if (lon_range == 0) lon_range = 1e-6;
if (lat_range == 0) lat_range = 1e-6;
double scale_x = WIDTH / lon_range;
double scale_y = HEIGHT / lat_range;
BMP bmp(WIDTH, HEIGHT);
color black(0, 0, 0);
for (const auto& way : ways) {
for (size_t i = 1; i < way.size(); ++i) {
if (!nodes.count(way[i - 1]) || !nodes.count(way[i])) continue;
const Node& n1 = nodes[way[i - 1]];
const Node& n2 = nodes[way[i]];
int x1, y1, x2, y2;
latlon_to_xy(n1.lat, n1.lon, x1, y1, min_lat, min_lon, scale_x, scale_y);
latlon_to_xy(n2.lat, n2.lon, x2, y2, min_lat, min_lon, scale_x, scale_y);
draw_line(bmp, x1, y1, x2, y2, black);
}
}
bmp.write(output_file);
std::cout << "Map saved to " << output_file << std::endl;
return 0;
}