/
GDALWriter.cpp
191 lines (164 loc) · 6.27 KB
/
GDALWriter.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
/******************************************************************************
* Copyright (c) 2016, Hobu Inc. <info@hobu.co>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include "GDALWriter.hpp"
#include <sstream>
#include <pdal/GDALUtils.hpp>
#include <pdal/PointView.hpp>
#include <pdal/pdal_macros.hpp>
namespace pdal
{
static PluginInfo const s_info = PluginInfo(
"writers.gdal",
"Write a point cloud as a GDAL raster.",
"http://pdal.io/stages/writers.gdal.html");
CREATE_STATIC_PLUGIN(1, 0, GDALWriter, Writer, s_info);
std::string GDALWriter::getName() const
{
return s_info.name;
}
void GDALWriter::addArgs(ProgramArgs& args)
{
args.add("filename", "Output filename", m_filename).setPositional();
args.add("resolution", "Cell edge size, in units of X/Y",
m_edgeLength).setPositional();
args.add("radius", "Radius from cell center to use to locate influencing "
"points", m_radius).setPositional();
args.add("gdaldriver", "GDAL writer driver name", m_drivername, "GTiff");
args.add("gdalopts", "GDAL driver options (name=value,name=value...)",
m_options);
args.add("output_type", "Statistics produced ('min', 'max', 'mean', "
"'idw', 'count', 'stdev' or 'all')", m_outputTypeString, {"all"} );
args.add("window_size", "Cell distance for fallback interpolation",
m_windowSize);
args.add("nodata", "No data value", m_noData, -9999.0);
args.add("dimension", "Dimension to use", m_interpDimString, "Z");
}
void GDALWriter::initialize()
{
for (auto& ts : m_outputTypeString)
{
Utils::trim(ts);
if (ts == "all")
{
m_outputTypes = ~0;
break;
}
if (ts == "min")
m_outputTypes |= GDALGrid::statMin;
else if (ts == "max")
m_outputTypes |= GDALGrid::statMax;
else if (ts == "count")
m_outputTypes |= GDALGrid::statCount;
else if (ts == "mean")
m_outputTypes |= GDALGrid::statMean;
else if (ts == "idw")
m_outputTypes |= GDALGrid::statIdw;
else if (ts == "stdev")
m_outputTypes |= GDALGrid::statStdDev;
else
{
std::ostringstream oss;
oss << "Invalid writers.gdal output type: '" << ts << "'.";
throw pdal_error(oss.str());
}
}
gdal::registerDrivers();
m_interpDim = Dimension::id(m_interpDimString);
if (m_interpDim == Dimension::Id::Unknown)
throw pdal_error("Dimension name is not known!");
}
void GDALWriter::ready(PointTableRef table)
{
if (!table.spatialReferenceUnique())
{
std::ostringstream oss;
oss << getName() << ": Can't write output with multiple spatial "
"references.";
throw pdal_error(oss.str());
}
}
void GDALWriter::write(const PointViewPtr view)
{
view->calculateBounds(m_bounds);
size_t width = ((m_bounds.maxx - m_bounds.minx) / m_edgeLength) + 1;
size_t height = ((m_bounds.maxy - m_bounds.miny) / m_edgeLength) + 1;
m_grid.reset(new GDALGrid(width, height, m_edgeLength, m_radius, m_noData,
m_outputTypes, m_windowSize));
for (PointId idx = 0; idx < view->size(); ++idx)
{
double x = view->getFieldAs<double>(Dimension::Id::X, idx) -
m_bounds.minx;
double y = view->getFieldAs<double>(Dimension::Id::Y, idx) -
m_bounds.miny;
double z = view->getFieldAs<double>(m_interpDim, idx);
m_grid->addPoint(x, y, z);
}
}
void GDALWriter::done(PointTableRef table)
{
std::array<double, 6> pixelToPos;
pixelToPos[0] = m_bounds.minx;
pixelToPos[1] = m_edgeLength;
pixelToPos[2] = 0;
pixelToPos[3] = m_bounds.miny + (m_edgeLength * m_grid->height());
pixelToPos[4] = 0;
pixelToPos[5] = -m_edgeLength;
gdal::Raster raster(m_filename, m_drivername, table.spatialReference(),
pixelToPos);
m_grid->finalize();
gdal::GDALError err = raster.open(m_grid->width(), m_grid->height(),
m_grid->numBands(), Dimension::Type::Double, m_grid->noData());
if (err != gdal::GDALError::None)
throw pdal_error(raster.errorMsg());
int bandNum = 1;
uint8_t *buf;
buf = m_grid->data("min");
if (buf)
raster.writeBand(buf, bandNum++, "min");
buf = m_grid->data("max");
if (buf)
raster.writeBand(buf, bandNum++, "max");
buf = m_grid->data("mean");
if (buf)
raster.writeBand(buf, bandNum++, "mean");
buf = m_grid->data("idw");
if (buf)
raster.writeBand(buf, bandNum++, "idw");
buf = m_grid->data("count");
if (buf)
raster.writeBand(buf, bandNum++, "count");
buf = m_grid->data("stdev");
if (buf)
raster.writeBand(buf, bandNum++, "stdev");
}
} // namespace pdal