Skip to content

Commit

Permalink
GDALRasterWrapper: apply scale and offset to packed values
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Dec 20, 2023
1 parent ea5bbc1 commit 6f93462
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions src/gdal_raster_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ GDALRasterWrapper::cartesian() const
return srs == nullptr || !OSRIsGeographic(srs);
}

static void
apply_scale_and_offset(double* px, std::size_t size, double scale, double offset)
{
double* end = px + size;
while (px < end) {
*px = *px * scale + offset;
px++;
}
}

RasterVariant
GDALRasterWrapper::read_box(const Box& box)
{
Expand All @@ -65,16 +75,46 @@ GDALRasterWrapper::read_box(const Box& box)
void* buffer;
GDALDataType read_type;

if (band_type == GDT_Int32) {
auto rast = make_raster<std::int32_t>(cropped_grid);
buffer = rast->data().data();
ret = std::move(rast);
read_type = GDT_Int32;
} else {
int has_scale = 0;
double scale = 1;
int has_offset = 0;
double offset = 0;

scale = GDALGetRasterScale(m_band, &has_scale);
offset = GDALGetRasterOffset(m_band, &has_offset);

if (has_scale || has_offset) {
auto rast = make_raster<double>(cropped_grid);
buffer = rast->data().data();
ret = std::move(rast);
read_type = GDT_Float64;
} else {
if (band_type == GDT_Byte) {
auto rast = make_raster<std::int8_t>(cropped_grid);
buffer = rast->data().data();
ret = std::move(rast);
read_type = GDT_Byte;
} else if (band_type == GDT_Int16) {
auto rast = make_raster<std::int16_t>(cropped_grid);
buffer = rast->data().data();
ret = std::move(rast);
read_type = GDT_Int16;
} else if (band_type == GDT_Int32) {
auto rast = make_raster<std::int32_t>(cropped_grid);
buffer = rast->data().data();
ret = std::move(rast);
read_type = GDT_Int32;
} else if (band_type == GDT_Float32) {
auto rast = make_raster<float>(cropped_grid);
buffer = rast->data().data();
ret = std::move(rast);
read_type = GDT_Float32;
} else {
auto rast = make_raster<double>(cropped_grid);
buffer = rast->data().data();
ret = std::move(rast);
read_type = GDT_Float64;
}
}

auto error = GDALRasterIO(m_band,
Expand All @@ -94,6 +134,10 @@ GDALRasterWrapper::read_box(const Box& box)
throw std::runtime_error("Error reading from raster.");
}

if (has_scale || has_offset) {
apply_scale_and_offset(static_cast<double*>(buffer), cropped_grid.size(), scale, offset);
}

return ret;
}

Expand Down

0 comments on commit 6f93462

Please sign in to comment.