OGS
Raster2PointCloud.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include <tclap/CmdLine.h>
5#include <vtkPolyDataAlgorithm.h>
6#include <vtkSmartPointer.h>
7#include <vtkXMLPolyDataWriter.h>
8
9#include <algorithm>
10#include <memory>
11#include <string>
12
15#include "BaseLib/FileTools.h"
16#include "BaseLib/Logging.h"
17#include "BaseLib/MPI.h"
20#include "InfoLib/GitInfo.h"
21
22int main(int argc, char* argv[])
23{
24 TCLAP::CmdLine cmd(
25 "Batch conversion of raster files into VTK point cloud data.\n"
26 "A series of raster files is converted into point clouds by creating a "
27 "number of random points based on the intensity value in each pixel "
28 "(the higher the value, the more points. The [x,y]-extend of these "
29 "random points is limited by the extent of the pixel they represent, "
30 "the elevation is limited by the max-elevation parameter. Likewise, "
31 "the "
32 "maximum number of points per pixel is limited by the max-points "
33 "parameter. The increase of the number of points in relation to the "
34 "pixel value is indicated by gamma, where 1 is a linear increase and "
35 "values smaller or larger than 1 indicate a logarithmic or exponential "
36 "increase, respectively.\n\n"
37 "OpenGeoSys-6 software, version " +
39 ".\n"
40 "Copyright (c) 2012-2026, OpenGeoSys Community "
41 "(http://www.opengeosys.org)",
43 TCLAP::ValueArg<double> gamma_arg(
44 "g", "gamma",
45 "Exponental increase coefficient. A value of '1' indicates a linear "
46 "increase, smaller or larger values indicate a logarithmic or "
47 "exponential increase, respectively (default: 1).",
48 false, 0.0, "exponential increase");
49 cmd.add(gamma_arg);
50 TCLAP::ValueArg<std::size_t> max_points_arg(
51 "p", "max-points",
52 "Maximum number of points per pixel (default: 1000), "
53 "(min = 0).",
54 false, 0, "maximum points per pixel");
55 cmd.add(max_points_arg);
56 TCLAP::ValueArg<double> max_height_arg(
57 "e", "max-elevation",
58 "Maximum elevation of randomly created points "
59 "(default: 5000).",
60 false, 0.0, "maximum point elevation");
61 cmd.add(max_height_arg);
62 TCLAP::ValueArg<std::string> output_arg(
63 "o", "output",
64 "Output (.vtp). Path to output polydata files. "
65 "Specifying 'file.vtp' will "
66 "make the programme write a series of files called 'file0.vtp', "
67 "'file1.vtp', etc.",
68 true, "", "OUTPUT_FILE");
69 cmd.add(output_arg);
70 TCLAP::ValueArg<std::string> input_arg(
71 "i", "input",
72 "Input (.asc). Path to input raster files. "
73 "Specifying 'file.asc' will make "
74 "the programme look for a series of files called 'file0.asc', "
75 "'file1.asc', etc.",
76 true, "", "INPUT_FILE");
77 cmd.add(input_arg);
78 auto log_level_arg = BaseLib::makeLogLevelArg();
79 cmd.add(log_level_arg);
80 cmd.parse(argc, argv);
81
82 BaseLib::MPI::Setup mpi_setup(argc, argv);
83 BaseLib::initOGSLogger(log_level_arg.getValue());
84
85 std::string const input_name = input_arg.getValue().c_str();
86 std::string const output_name = output_arg.getValue().c_str();
87
88 double const max_height =
89 (max_height_arg.isSet()) ? max_height_arg.getValue() : 5000;
90 std::size_t const max_points =
91 (max_points_arg.isSet()) ? max_points_arg.getValue() : 1000;
92 double const gamma = (gamma_arg.isSet()) ? gamma_arg.getValue() : 1;
93
94 std::string const ibase_name = BaseLib::dropFileExtension(input_name);
95 std::string const ifile_ext = BaseLib::getFileExtension(input_name);
96
97 std::string const obase_name = BaseLib::dropFileExtension(output_name);
98 std::string const ofile_ext = BaseLib::getFileExtension(output_name);
99
100 // Rainevents using point cloud filter
101 std::cout << "Starting export... ";
102 double global_min = std::numeric_limits<double>::max();
103 double global_max = 0;
104
105 std::size_t n_rasters = 0;
106 for (std::size_t i = 0;; ++i)
107 {
108 std::string const file_name =
109 ibase_name + std::to_string(i) + ifile_ext;
110 std::unique_ptr<GeoLib::Raster> r(
112 if (r == nullptr)
113 {
114 n_rasters = i;
115 break;
116 }
117
118 for (auto it = r->begin(); it != r->end(); ++it)
119 {
120 if (*it != r->getHeader().no_data)
121 {
122 global_min = std::min(*it, global_min);
123 global_max = std::max(*it, global_max);
124 }
125 }
126 }
127
128 for (std::size_t i = 0; i < n_rasters; ++i)
129 {
130 std::string const file_name =
131 ibase_name + std::to_string(i) + ifile_ext;
132 vtkSmartPointer<VtkGeoImageSource> geo_image =
133 vtkSmartPointer<VtkGeoImageSource>::New();
134 geo_image->readImage(QString::fromStdString(file_name));
135 VtkImageDataToPointCloudFilter* const point_cloud_filter =
137 point_cloud_filter->SetInputConnection(geo_image->GetOutputPort());
138 geo_image->Update();
139 point_cloud_filter->SetMinValueRange(global_min);
140 point_cloud_filter->SetMaxValueRange(global_max);
141 point_cloud_filter->SetMinHeight(0);
142 point_cloud_filter->SetMaxHeight(max_height);
143 point_cloud_filter->SetIsLinear(false);
144 point_cloud_filter->SetMinNumberOfPointsPerCell(0);
145 point_cloud_filter->SetMaxNumberOfPointsPerCell(max_points);
146 point_cloud_filter->SetGamma(gamma);
147 point_cloud_filter->Update();
148 vtkSmartPointer<vtkPolyDataAlgorithm> const pd_alg =
149 dynamic_cast<vtkPolyDataAlgorithm*>(point_cloud_filter);
150 if (pd_alg)
151 {
152 std::string const output =
153 obase_name + std::to_string(i) + ofile_ext;
154 vtkSmartPointer<vtkXMLPolyDataWriter> pd_writer =
155 vtkSmartPointer<vtkXMLPolyDataWriter>::New();
156 pd_writer->SetInputData(pd_alg->GetOutputDataObject(0));
157 pd_writer->SetFileName(output.c_str());
158 pd_writer->Write();
159 }
160 }
161 std::cout << "done." << std::endl;
162 return EXIT_SUCCESS;
163}
int main(int argc, char *argv[])
static GeoLib::Raster * getRasterFromASCFile(std::string const &fname)
Reads an ArcGis ASC raster file.
static VtkImageDataToPointCloudFilter * New()
Create a new objects (required because of VTKs reference counting)
TCLAP::ValueArg< std::string > makeLogLevelArg()
void initOGSLogger(std::string const &log_level)
Definition Logging.cpp:56
std::string getFileExtension(const std::string &path)
std::string dropFileExtension(std::string const &filename)
GITINFOLIB_EXPORT const std::string ogs_version