/
utils.cpp
264 lines (214 loc) · 7.62 KB
/
utils.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
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
/*****************************************************************************
* Copyright (c) 2023, Lutra Consulting Ltd. and Hobu, Inc. *
* *
* All rights reserved. *
* *
* 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. *
* *
****************************************************************************/
#include "utils.hpp"
#include <filesystem>
#include <iostream>
#include <chrono>
#include <pdal/PipelineManager.hpp>
#include <pdal/Stage.hpp>
#include <pdal/StageFactory.hpp>
#include <pdal/util/ThreadPool.hpp>
#include <gdal_utils.h>
using namespace pdal;
static ProgressBar sProgressBar;
// Table subclass that also takes care of updating progress in streaming pipelines
class MyTable : public FixedPointTable
{
public:
MyTable(point_count_t capacity) : FixedPointTable(capacity) {}
protected:
virtual void reset()
{
sProgressBar.add();
FixedPointTable::reset();
}
};
std::string box_to_pdal_bounds(const BOX2D &box)
{
std::ostringstream oss;
oss << std::fixed << "([" << box.minx << "," << box.maxx << "],[" << box.miny << "," << box.maxy << "])";
return oss.str(); // "([xmin, xmax], [ymin, ymax])"
}
QuickInfo getQuickInfo(std::string inputFile)
{
// TODO: handle the case when driver is not inferred
std::string driver = StageFactory::inferReaderDriver(inputFile);
if (driver.empty())
{
std::cerr << "Could not infer driver for input file: " << inputFile << std::endl;
return QuickInfo();
}
StageFactory factory;
Stage *reader = factory.createStage(driver); // reader is owned by the factory
pdal::Options opts;
opts.add("filename", inputFile);
reader->setOptions(opts);
return reader->preview();
// PipelineManager m;
// Stage &r = m.makeReader(inputFile, "");
// return r.preview().m_pointCount;
}
MetadataNode getReaderMetadata(std::string inputFile, MetadataNode *pointLayoutMeta)
{
// compared to quickinfo / preview, this provides more info...
PipelineManager m;
Stage &r = m.makeReader(inputFile, "");
FixedPointTable table(10000);
r.prepare(table);
if (pointLayoutMeta)
{
*pointLayoutMeta = table.layout()->toMetadata();
}
return r.getMetadata();
}
void runPipelineParallel(point_count_t totalPoints, bool isStreaming, std::vector<std::unique_ptr<PipelineManager>>& pipelines, int max_threads, bool verbose)
{
const int CHUNK_SIZE = 100'000;
int num_chunks = totalPoints / CHUNK_SIZE;
if (verbose)
{
std::cout << "total points: " << (float)totalPoints / 1'000'000 << "M" << std::endl;
std::cout << "jobs " << pipelines.size() << std::endl;
std::cout << "max threads " << max_threads << std::endl;
if (!isStreaming)
std::cout << "running in non-streaming mode!" << std::endl;
}
auto start = std::chrono::high_resolution_clock::now();
sProgressBar.init(isStreaming ? num_chunks : pipelines.size());
int nThreads = std::min( (int)pipelines.size(), max_threads );
ThreadPool p(nThreads);
for (size_t i = 0; i < pipelines.size(); ++i)
{
PipelineManager* pipeline = pipelines[i].get();
if (isStreaming)
{
p.add([pipeline, CHUNK_SIZE]() {
MyTable table(CHUNK_SIZE);
pipeline->executeStream(table);
});
}
else
{
p.add([pipeline, &pipelines, i]() {
pipeline->execute();
pipelines[i].reset(); // to free the point table and views (meshes, rasters)
sProgressBar.add();
});
}
}
//std::cout << "starting to wait" << std::endl;
// while (p.tasksInQueue() + p.tasksInProgress())
// {
// //std::cout << "progress: " << p.tasksInQueue() << " " << p.tasksInProgress() << " cnt " << cntPnt/1'000 << std::endl;
// std::this_thread::sleep_for(500ms);
// }
p.join();
sProgressBar.done();
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
if (verbose)
{
std::cout << "time " << duration.count()/1000. << " s" << std::endl;
}
}
static GDALDatasetH rasterTilesToVrt(const std::vector<std::string> &inputFiles, const std::string &outputVrtFile)
{
// build a VRT so that all tiles can be handled as a single data source
std::vector<const char*> dsNames;
for ( const std::string &t : inputFiles )
{
dsNames.push_back(t.c_str());
}
// https://gdal.org/api/gdal_utils.html
GDALDatasetH ds = GDALBuildVRT(outputVrtFile.c_str(), (int)dsNames.size(), nullptr, dsNames.data(), nullptr, nullptr);
return ds;
}
static bool rasterVrtToCog(GDALDatasetH ds, const std::string &outputFile)
{
const char* args[] = { "-of", "COG", "-co", "COMPRESS=DEFLATE", NULL };
GDALTranslateOptions* psOptions = GDALTranslateOptionsNew((char**)args, NULL);
GDALDatasetH dsFinal = GDALTranslate(outputFile.c_str(), ds, psOptions, nullptr);
GDALTranslateOptionsFree(psOptions);
if (!dsFinal)
return false;
GDALClose(dsFinal);
return true;
}
bool rasterTilesToCog(const std::vector<std::string> &inputFiles, const std::string &outputFile)
{
std::string outputVrt = outputFile;
assert(ends_with(outputVrt, ".tif"));
outputVrt.erase(outputVrt.rfind(".tif"), 4);
outputVrt += ".vrt";
GDALDatasetH ds = rasterTilesToVrt(inputFiles, outputVrt);
if (!ds)
return false;
rasterVrtToCog(ds, outputFile);
GDALClose(ds);
std::filesystem::remove(outputVrt);
return true;
}
bool readerSupportsBounds(Stage &reader)
{
// these readers support "bounds" option with a 2D/3D bounding box, and based
// on it, they will do very efficient reading of data and only return what's
// in the given bounding box
return reader.getName() == "readers.copc" || reader.getName() == "readers.ept";
}
bool allReadersSupportBounds(const std::vector<Stage *> &readers)
{
for (Stage *r : readers)
{
if (!readerSupportsBounds(*r))
return false;
}
return true;
}
pdal::Bounds parseBounds(const std::string &boundsStr)
{
// if the input string is not a correct 2D/3D PDAL bounds then parse()
// will throw an exception
pdal::Bounds b;
std::string::size_type pos(0);
b.parse(boundsStr, pos);
return b;
}
BOX2D intersectionBox2D(const BOX2D &b1, const BOX2D &b2)
{
BOX2D b;
b.minx = b1.minx > b2.minx ? b1.minx : b2.minx;
b.miny = b1.miny > b2.miny ? b1.miny : b2.miny;
b.maxx = b1.maxx < b2.maxx ? b1.maxx : b2.maxx;
b.maxy = b1.maxy < b2.maxy ? b1.maxy : b2.maxy;
if (b.minx > b.maxx || b.miny > b.maxy)
return BOX2D();
return b;
}
BOX2D intersectTileBoxWithFilterBox(const BOX2D &tileBox, const BOX2D &filterBox)
{
if (tileBox.valid() && filterBox.valid())
{
return intersectionBox2D(tileBox, filterBox);
}
else if (tileBox.valid())
{
return tileBox;
}
else if (filterBox.valid())
{
return filterBox;
}
else
{
return BOX2D(); // invalid box
}
}