forked from osmcode/osmium-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_time_filter.cpp
176 lines (141 loc) · 5.56 KB
/
command_time_filter.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
/*
Osmium -- OpenStreetMap data manipulation command line tool
https://osmcode.org/osmium-tool/
Copyright (C) 2013-2025 Jochen Topf <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "command_time_filter.hpp"
#include "exception.hpp"
#include "util.hpp"
#include <osmium/diff_iterator.hpp>
#include <osmium/io/file.hpp>
#include <osmium/io/header.hpp>
#include <osmium/io/input_iterator.hpp>
#include <osmium/io/output_iterator.hpp>
#include <osmium/io/reader_with_progress_bar.hpp>
#include <osmium/io/writer.hpp>
#include <osmium/osm/diff_object.hpp>
#include <osmium/osm/entity_bits.hpp>
#include <osmium/osm/object.hpp>
#include <osmium/osm/timestamp.hpp>
#include <osmium/util/verbose_output.hpp>
#include <boost/program_options.hpp>
#include <algorithm>
#include <ctime>
#include <stdexcept>
#include <string>
#include <vector>
bool CommandTimeFilter::setup(const std::vector<std::string>& arguments) {
const po::options_description opts_common{add_common_options()};
const po::options_description opts_input{add_single_input_options()};
const po::options_description opts_output{add_output_options()};
po::options_description hidden;
hidden.add_options()
("input-filename", po::value<std::string>(), "OSM input file")
("time-from", po::value<std::string>(), "Start of time range")
("time-to", po::value<std::string>(), "End of time range")
;
po::options_description desc;
desc.add(opts_common).add(opts_input).add(opts_output);
po::options_description parsed_options;
parsed_options.add(desc).add(hidden);
po::positional_options_description positional;
positional.add("input-filename", 1);
positional.add("time-from", 1);
positional.add("time-to", 1);
po::variables_map vm;
po::store(po::command_line_parser(arguments).options(parsed_options).positional(positional).run(), vm);
po::notify(vm);
if (!setup_common(vm, desc)) {
return false;
}
setup_progress(vm);
setup_input_file(vm);
setup_output_file(vm);
m_from = osmium::Timestamp{std::time(nullptr)};
m_to = m_from;
if (vm.count("time-from")) {
const auto ts = vm["time-from"].as<std::string>();
try {
m_from = osmium::Timestamp{ts};
} catch (const std::invalid_argument&) {
throw argument_error{"Wrong format for (first) timestamp (use YYYY-MM-DDThh:mm:ssZ)."};
}
m_to = m_from;
}
if (vm.count("time-to")) {
const auto ts = vm["time-to"].as<std::string>();
try {
m_to = osmium::Timestamp{ts};
} catch (const std::invalid_argument&) {
throw argument_error{"Wrong format for second timestamp (use YYYY-MM-DDThh:mm:ssZ)."};
}
}
if (m_from > m_to) {
throw argument_error{"Second timestamp is before first one."};
}
if (m_from == m_to) { // point in time
if (m_output_file.has_multiple_object_versions()) {
warning("You are writing to a file marked as having multiple object versions,\n"
"but there will be only a single version of each object.\n");
}
} else { // time range
if (!m_output_file.has_multiple_object_versions()) {
warning("You are writing to a file marked as having a single object version,\n"
"but there might be multiple versions of each object.\n");
}
}
return true;
}
void CommandTimeFilter::show_arguments() {
show_single_input_arguments(m_vout);
show_output_arguments(m_vout);
m_vout << " other options:\n";
m_vout << " Filtering from time " << m_from.to_iso() << " to " << m_to.to_iso() << "\n";
}
bool CommandTimeFilter::run() {
m_vout << "Opening input file...\n";
osmium::io::ReaderWithProgressBar reader{display_progress(), m_input_file, osmium::osm_entity_bits::object};
m_vout << "Opening output file...\n";
osmium::io::Header header{reader.header()};
setup_header(header);
osmium::io::Writer writer{m_output_file, header, m_output_overwrite, m_fsync};
m_vout << "Filter data while copying it from input to output...\n";
auto input = osmium::io::make_input_iterator_range<osmium::OSMObject>(reader);
auto diff_begin = osmium::make_diff_iterator(input.begin(), input.end());
auto diff_end = osmium::make_diff_iterator(input.end(), input.end());
auto out = osmium::io::make_output_iterator(writer);
if (m_from == m_to) {
std::copy_if(
diff_begin,
diff_end,
out,
[this](const osmium::DiffObject& d) {
return d.is_visible_at(m_from);
});
} else {
std::copy_if(
diff_begin,
diff_end,
out,
[this](const osmium::DiffObject& d) {
return d.is_between(m_from, m_to);
});
}
m_vout << "Closing output file...\n";
writer.close();
m_vout << "Closing input file...\n";
reader.close();
show_memory_used();
m_vout << "Done.\n";
return true;
}