-
Notifications
You must be signed in to change notification settings - Fork 0
/
expiries2shp.cpp
187 lines (174 loc) · 6.27 KB
/
expiries2shp.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
/*
* expiries2shp.cpp
*
* Created on: 06.10.2016
* Author: michael
*/
#include <getopt.h>
#include <glob.h>
#include <vector>
#include <boost/algorithm/string.hpp>
#include "output_layer.hpp"
void read_expiry_file(std::string& filename, OutputLayer& layer, std::string sequence,
bool tile_ids, int min_zoom, int max_zoom, int metatile_size) {
if (sequence == "") {
sequence = filename.substr(0, filename.find_last_of("."));
}
std::ifstream expiryfile;
expiryfile.open(filename);
std::string line;
while (std::getline(expiryfile,line)) {
std::vector<std::string> elements;
// splitting the string
boost::split(elements, line, boost::is_any_of("/"));
int zoom = std::stoi(elements.at(0));
if (zoom < min_zoom || zoom > max_zoom) {
continue;
}
int x = std::stoi(elements.at(1));
int y = std::stoi(elements.at(2));
for (int offset_x = 0; offset_x < metatile_size; ++offset_x) {
for (int offset_y = 0; offset_y < metatile_size; ++offset_y) {
layer.write_tile(x + offset_x, y + offset_y, zoom, sequence, tile_ids);
}
}
}
expiryfile.close();
}
/**
* get vector with the names of all files matching a glob pattern
*/
std::vector<std::string> get_filenames_matching_glob(std::string& pattern) {
glob_t glob_result;
int glob_err = glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
switch (glob_err) {
case GLOB_NOSPACE :
std::cerr << "ERROR: Glob matching failed for pattern " << pattern << "due to lack of memory.\n";
exit(1);
case GLOB_ABORTED :
std::cerr << "ERROR: Glob matching failed for pattern " << pattern << "due to read error.\n";
exit(1);
case GLOB_NOMATCH :
std::cerr << "ERROR: Glob matching failed for pattern " << pattern << ". No matches found.\n";
exit(1);
}
std::vector<std::string> files;
for(unsigned int i=0; i<glob_result.gl_pathc; ++i) {
files.push_back(std::string(glob_result.gl_pathv[i]));
}
globfree(&glob_result);
return files;
}
void print_help() {
std::cerr << "USAGE:\nexpiries2shp [OPTIONS] [INFILES OUT_FILENAME]\n" \
<< "\nOptions:\n" \
<< " -f, --format output format (default 'ESRI Shapefile')\n" \
<< " -p, --projection Use other projection than EPSG:3857 for output.\n" \
<< " -s, --sequence Sequence number instead of filename without suffix\n" \
<< " -i, --ids Add columns with x and y index of the tile\n" \
<< " -m NUM, --metatile-size NUM Use if input list contains top left tiles of metatiles only.\n" \
<< " Specify meta tile size.\n" \
<< " -v, --verbose enable verbose output\n" \
<< " -z, --min-zoom Only export tiles with zoom equal or larger than this\n" \
<< " -Z, --max-zoom Only export tiles with zoom equal or smaller than this\n" \
<< " create this expiry list.\n";
}
std::string get_directory(std::string& path) {
size_t last_slash = path.find_last_of("/");
if (last_slash == std::string::npos) {
return ".";
} else {
return path.substr(0, last_slash+1);
}
}
std::string get_filename(std::string& path) {
size_t begin = path.find_last_of("/");
// look for .shp suffix
size_t end = path.rfind(".shp");
if (begin == std::string::npos) {
std::cerr << "ERROR: Output path is a directory but should be a file.\n";
exit(1);
} else {
return path.substr(begin+1, end);
}
}
int main(int argc, char* argv[]) {
// parse command line arguments
static struct option long_options[] = {
{"format", required_argument, 0, 'f'},
{"projection", required_argument, 0, 'p'},
{"sequence", required_argument, 0, 's'},
{"ids", no_argument, 0, 'i'},
{"metatile-size", required_argument, 0, 'm'},
{"verbose", no_argument, 0, 'v'},
{"min-zoom", required_argument, 0, 'z'},
{"max-zoom", required_argument, 0, 'Z'},
{0, 0, 0, 0}
};
int epsg = 3857;
std::string sequence = "";
std::string output_format = "ESRI Shapefile";
bool tile_ids = false;
int metatile_size = 1;
bool verbose = false;
int min_zoom = 0;
int max_zoom = 25;
while (true) {
int c = getopt_long(argc, argv, "f:p:s:im:fz:Z:v", long_options, 0);
if (c == -1) {
break;
}
switch (c) {
case 'f':
output_format = optarg;
break;
case 'p':
epsg = atoi(optarg);
break;
case 's':
sequence = optarg;
break;
case 'i':
tile_ids = true;
break;
case 'm':
metatile_size = atoi(optarg);
if (metatile_size < 1 || metatile_size > 8) {
std::cerr << "WARNING: Metatile size is abnormal. Expected values between 1 and 8.\n";
}
break;
case 'v':
verbose = true;
break;
case 'z':
min_zoom = atoi(optarg);
break;
case 'Z':
max_zoom = atoi(optarg);
break;
default:
print_help();
exit(1);
}
}
int remaining_args = argc - optind;
std::string input_files_pattern = "";
std::string output_file = "";
if (remaining_args != 2) {
print_help();
exit(1);
} else {
input_files_pattern = argv[optind];
output_file = argv[optind+1];
}
std::vector<std::string> input_filenames = get_filenames_matching_glob(input_files_pattern);
// set up output layer
OutputLayer output_layer (get_directory(output_file), get_filename(output_file), epsg, tile_ids, output_format);
// read expiry file and write shape file
for (std::string& filename : input_filenames) {
if (verbose) {
std::cout << "Reading file " << filename << "\n";
}
read_expiry_file(filename, output_layer, sequence, tile_ids, min_zoom, max_zoom, metatile_size);
}
}