-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclip.cpp
228 lines (185 loc) · 6.96 KB
/
clip.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
/*****************************************************************************
* 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 <iostream>
#include <filesystem>
#include <thread>
#include <pdal/PipelineManager.hpp>
#include <pdal/Stage.hpp>
#include <pdal/util/ProgramArgs.hpp>
#include <pdal/pdal_types.hpp>
#include <pdal/Polygon.hpp>
#include <gdal_utils.h>
#include "utils.hpp"
#include "alg.hpp"
#include "vpc.hpp"
using namespace pdal;
namespace fs = std::filesystem;
void Clip::addArgs()
{
argOutput = &programArgs.add("output,o", "Output point cloud file", outputFile);
argOutputFormat = &programArgs.add("output-format", "Output format (las/laz/copc)", outputFormat);
argPolygon = &programArgs.add("polygon,p", "Input polygon vector file", polygonFile);
}
bool Clip::checkArgs()
{
if (!argOutput->set())
{
std::cerr << "missing output" << std::endl;
return false;
}
if (!argPolygon->set())
{
std::cerr << "missing polygon" << std::endl;
return false;
}
if (argOutputFormat->set())
{
if (outputFormat != "las" && outputFormat != "laz")
{
std::cerr << "unknown output format: " << outputFormat << std::endl;
return false;
}
}
else
outputFormat = "las"; // uncompressed by default
return true;
}
// populate polygons into filters.crop options
bool loadPolygons(const std::string &polygonFile, pdal::Options& crop_opts, BOX2D& bbox)
{
GDALAllRegister();
GDALDatasetH hDS = GDALOpenEx( polygonFile.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL );
if( hDS == NULL )
{
std::cerr << "Could not open input polygon file: " << polygonFile << std::endl;
return false;
}
// TODO: reproject polygons to the CRS of the point cloud if they are not the same
OGRLayerH hLayer = GDALDatasetGetLayer(hDS, 0);
//hLayer = GDALDatasetGetLayerByName( hDS, "point" );
OGREnvelope fullEnvelope;
OGR_L_ResetReading(hLayer);
OGRFeatureH hFeature;
while( (hFeature = OGR_L_GetNextFeature(hLayer)) != NULL )
{
OGRGeometryH hGeometry = OGR_F_GetGeometryRef(hFeature);
if ( hGeometry != NULL )
{
OGREnvelope envelope;
OGR_G_GetEnvelope(hGeometry, &envelope);
if (!fullEnvelope.IsInit())
fullEnvelope = envelope;
else
fullEnvelope.Merge(envelope);
crop_opts.add(pdal::Option("polygon", pdal::Polygon(hGeometry)));
}
OGR_F_Destroy( hFeature );
}
GDALClose( hDS );
bbox = BOX2D(fullEnvelope.MinX, fullEnvelope.MinY, fullEnvelope.MaxX, fullEnvelope.MaxY);
return true;
}
static std::unique_ptr<PipelineManager> pipeline(ParallelJobInfo *tile, const pdal::Options &crop_opts)
{
assert(tile);
std::unique_ptr<PipelineManager> manager( new PipelineManager );
Stage& r = manager->makeReader( tile->inputFilenames[0], "");
Stage *last = &r;
// filtering
if (!tile->filterBounds.empty())
{
Options filter_opts;
filter_opts.add(pdal::Option("bounds", tile->filterBounds));
if (readerSupportsBounds(r))
{
// Reader of the format can do the filtering - use that whenever possible!
r.addOptions(filter_opts);
}
else
{
// Reader can't do the filtering - do it with a filter
last = &manager->makeFilter( "filters.crop", *last, filter_opts);
}
}
if (!tile->filterExpression.empty())
{
Options filter_opts;
filter_opts.add(pdal::Option("expression", tile->filterExpression));
last = &manager->makeFilter( "filters.expression", *last, filter_opts);
}
last = &manager->makeFilter( "filters.crop", *last, crop_opts );
pdal::Options writer_opts;
writer_opts.add(pdal::Option("forward", "all"));
manager->makeWriter( tile->outputFilename, "", *last, writer_opts);
return manager;
}
void Clip::preparePipelines(std::vector<std::unique_ptr<PipelineManager>>& pipelines)
{
pdal::Options crop_opts;
BOX2D bbox;
if (!loadPolygons(polygonFile, crop_opts, bbox))
return;
if (ends_with(inputFile, ".vpc"))
{
if (!ends_with(outputFile, ".vpc"))
{
std::cerr << "If input file is a VPC, output should be VPC too." << std::endl;
return;
}
// for /tmp/hello.vpc we will use /tmp/hello dir for all results
fs::path outputParentDir = fs::path(outputFile).parent_path();
fs::path outputSubdir = outputParentDir / fs::path(outputFile).stem();
fs::create_directories(outputSubdir);
// VPC handling
VirtualPointCloud vpc;
if (!vpc.read(inputFile))
return;
for (const VirtualPointCloud::File& f : vpc.files)
{
if (!bbox.overlaps(f.bbox.to2d()))
{
totalPoints -= f.count;
continue; // we can safely skip
}
if (verbose)
{
std::cout << "using " << f.filename << std::endl;
}
ParallelJobInfo tile(ParallelJobInfo::FileBased, BOX2D(), filterExpression, filterBounds);
tile.inputFilenames.push_back(f.filename);
// for input file /x/y/z.las that goes to /tmp/hello.vpc,
// individual output file will be called /tmp/hello/z.las
fs::path inputBasename = fs::path(f.filename).stem();
tile.outputFilename = (outputSubdir / inputBasename).string() + "." + outputFormat;
tileOutputFiles.push_back(tile.outputFilename);
pipelines.push_back(pipeline(&tile, crop_opts));
}
}
else
{
ParallelJobInfo tile(ParallelJobInfo::Single, BOX2D(), filterExpression, filterBounds);
tile.inputFilenames.push_back(inputFile);
tile.outputFilename = outputFile;
pipelines.push_back(pipeline(&tile, crop_opts));
}
}
void Clip::finalize(std::vector<std::unique_ptr<PipelineManager>>&)
{
if (tileOutputFiles.empty())
return;
// now build a new output VPC
std::vector<std::string> args;
args.push_back("--output=" + outputFile);
for (std::string f : tileOutputFiles)
args.push_back(f);
buildVpc(args);
}