-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles2bsb.cpp
147 lines (121 loc) · 4.12 KB
/
tiles2bsb.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
// ---------------------------------------------------------------------------
// This software is in the public domain, furnished "as is", without technical
// support, and with no warranty, express or implied, as to its usefulness for
// any purpose.
//
// TILES2BSB.CC
// Main program to transform a range of tiles from a tileserver into a BSB
// formatted .cap/.kap file for usage in nautical charts navigation software
//
// Author: Thomas Brüggemann
// ---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <utility>
#include "tiles.hpp"
#include "fetch.hpp"
#include "image.hpp"
#include "bsb.hpp"
#include "polygon.hpp"
#include "geojson.hpp"
#include "boundingbox.hpp"
#include "coordinate.hpp"
#include "elapsed.hpp"
#include "crayons.hpp"
#include "config.hpp"
Crayon GREY(FG_LIGHT_GRAY);
Crayon DEFAULT(FG_DEFAULT);
Crayon BLUE(FG_LIGHT_BLUE);
Crayon GREEN(FG_GREEN);
Crayon YELLOW(FG_YELLOW);
Crayon RED(FG_RED);
int main(int argc, char* argv[])
{
std::cout << "=== TILES-2-BSB v1.0 ===" << std::endl;
if (argc < 2)
{
std::cout << RED << "Error: Missing Parameters!" << DEFAULT << std::endl;
std::cout << "Usage: ./tiles2bsb path/to/config.json" << std::endl;
exit(1);
}
// load config from json file
std::string confFile(argv[1]);
Config conf(confFile);
Elapsed total;
total.Start();
int zoom = conf.zoom;
// read geojson from input string
Tiles tiles;
Geojson gjson;
Fetch f;
std::vector<Polygon> polygons = gjson.parseFile(conf.geojsonPath);
std::cout << BLUE << "Polygons found: " << polygons.size() << DEFAULT << std::endl;
// loop all the available polygons
int j = 1;
for (auto polyg : polygons)
{
Elapsed e; e.Start();
std::cout << "========================" << std::endl;
std::cout << "Start processing of Polygon!" << std::endl;
std::vector<TilesResult> tileResults = tiles.fromPolygon(zoom, polyg, 1);
std::cout << BLUE << "Rectangles found: " << tileResults.size() << DEFAULT << std::endl;
if(tileResults.size() == 0)
{
std::cout << YELLOW << "Keep in mind that only convex geojson polygon shapes are supported yet!" << DEFAULT << std::endl;
}
// loop all tile mapping results
for(auto tiles : tileResults)
{
std::cout << "------------------------" << std::endl;
// loop all xy coordinates to download
for(XY xy : tiles.coordinates)
{
// download each of these tiles
f.saveFromUrl(conf.tileServerUrl, zoom, xy.x, xy.y, "tiles/{z}_{x}_{y}.png");
}
std::cout << GREEN << "Tile download ... OK" << DEFAULT << std::endl;
// display stats
double downloadMs = e.End();
std::cout << GREY << "Download of " << tiles.coordinates.size() << " tiles ran " << downloadMs << "ms" << DEFAULT << std::endl;
e.Start();
// stitch images together
try
{
Image i;
bool stitchResult = i.stitchTogether(tiles, zoom, "tiles/map_" + conf.name + "_" + patches::to_string(j) + ".png");
if(stitchResult == true)
{
std::cout << GREEN << "Image stitching ... OK" << DEFAULT << std::endl;
}
else
{
std::cout << RED << "Image stitching ... ERROR" << DEFAULT << std::endl;
}
// display stats
double stitchingMs = e.End();
std::cout << GREY << "Stitching of " << tiles.coordinates.size() << " images ran " << stitchingMs << "ms" << DEFAULT << std::endl;
e.Start();
BSB b;
bool bsbResult = b.fromPNG("tiles/map_" + conf.name + "_" + patches::to_string(j) + ".png", "maps/map_" + conf.name + "_" + patches::to_string(j) + ".kap", tiles.topLeftEdge, tiles.bottomRightEdge);
if(bsbResult == true)
{
std::cout << GREEN << "BSB conversion ... OK" << DEFAULT << std::endl;
}
else
{
std::cout << RED << "BSB conversion ... ERROR" << DEFAULT << std::endl;
}
double bsbMs = e.End();
std::cout << GREY << "Converting to .kap file took " << bsbMs << "ms" << DEFAULT << std::endl;
}
catch (std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
j++;
}
}
double totalMs = total.End();
std::cout << GREY << "Total runtime of tiles2bsb " << totalMs << "ms" << DEFAULT << std::endl;
return 0;
}