Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading rasters that do not contain coordinate (0, 0) #63

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/src/exactextract/raster_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ def nodata_value(self):
return self.ds.rio.nodata

def read_window(self, x0, y0, nx, ny):
if nx == 0 or ny == 0:
return np.array([[]], dtype=self.ds.dtype)

lats = self.ds[self.ds.rio.y_dim]
flipped = bool(len(lats) > 1 and lats[1] > lats[0])

Expand Down
44 changes: 20 additions & 24 deletions python/src/pybindings/raster_source_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,27 @@ class PyRasterSourceBase : public RasterSource
{
auto cropped_grid = grid().crop(box);

if (!cropped_grid.empty()) {
auto x0 = cropped_grid.col_offset(grid());
auto y0 = cropped_grid.row_offset(grid());
auto nx = cropped_grid.cols();
auto ny = cropped_grid.rows();

py::array rast_values = read_window(x0, y0, nx, ny);
py::object nodata = nodata_value();

if (py::isinstance<py::array_t<std::int8_t>>(rast_values)) {
return make_raster<std::int8_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<std::int16_t>>(rast_values)) {
return make_raster<std::int16_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<std::int32_t>>(rast_values)) {
return make_raster<std::int32_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<std::int64_t>>(rast_values)) {
return make_raster<std::int64_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<float>>(rast_values)) {
return make_raster<float>(cropped_grid, rast_values, nodata);
} else {
return make_raster<double>(cropped_grid, rast_values, nodata);
}
auto x0 = cropped_grid.col_offset(grid());
auto y0 = cropped_grid.row_offset(grid());
auto nx = cropped_grid.cols();
auto ny = cropped_grid.rows();

py::array rast_values = read_window(x0, y0, nx, ny);
py::object nodata = nodata_value();

if (py::isinstance<py::array_t<std::int8_t>>(rast_values)) {
return make_raster<std::int8_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<std::int16_t>>(rast_values)) {
return make_raster<std::int16_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<std::int32_t>>(rast_values)) {
return make_raster<std::int32_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<std::int64_t>>(rast_values)) {
return make_raster<std::int64_t>(cropped_grid, rast_values, nodata);
} else if (py::isinstance<py::array_t<float>>(rast_values)) {
return make_raster<float>(cropped_grid, rast_values, nodata);
} else {
return make_raster<double>(cropped_grid, rast_values, nodata);
}

return std::make_unique<Raster<double>>(Raster<double>::make_empty());
}

const Grid<bounded_extent>& grid() const override
Expand Down
38 changes: 21 additions & 17 deletions src/gdal_raster_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

namespace exactextract {

GDALRasterWrapper::GDALRasterWrapper(const std::string& filename, int bandnum)
GDALRasterWrapper::
GDALRasterWrapper(const std::string& filename, int bandnum)
: m_grid{ Grid<bounded_extent>::make_empty() }
{
auto rast = GDALOpen(filename.c_str(), GA_ReadOnly);
Expand Down Expand Up @@ -117,21 +118,23 @@
}
}

auto error = GDALRasterIO(m_band,
GF_Read,
(int)cropped_grid.col_offset(m_grid),
(int)cropped_grid.row_offset(m_grid),
(int)cropped_grid.cols(),
(int)cropped_grid.rows(),
buffer,
(int)cropped_grid.cols(),
(int)cropped_grid.rows(),
read_type,
0,
0);

if (error) {
throw std::runtime_error("Error reading from raster.");
if (!cropped_grid.empty()) {
auto error = GDALRasterIO(m_band,
GF_Read,
(int)cropped_grid.col_offset(m_grid),
(int)cropped_grid.row_offset(m_grid),
(int)cropped_grid.cols(),
(int)cropped_grid.rows(),
buffer,
(int)cropped_grid.cols(),
(int)cropped_grid.rows(),
read_type,
0,
0);

if (error) {
throw std::runtime_error("Error reading from raster.");

Check warning on line 136 in src/gdal_raster_wrapper.cpp

View check run for this annotation

Codecov / codecov/patch

src/gdal_raster_wrapper.cpp#L136

Added line #L136 was not covered by tests
}
}

if (has_scale || has_offset) {
Expand Down Expand Up @@ -165,7 +168,8 @@
m_grid = { box, dx, dy };
}

GDALRasterWrapper::GDALRasterWrapper(exactextract::GDALRasterWrapper&& src) noexcept
GDALRasterWrapper::
GDALRasterWrapper(exactextract::GDALRasterWrapper&& src) noexcept
: m_rast{ src.m_rast }
, m_band{ src.m_band }
, m_nodata_value{ src.m_nodata_value }
Expand Down
4 changes: 4 additions & 0 deletions src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ class Grid

Grid<extent_tag> shrink_to_fit(const Box& b) const
{
if (b.empty()) {
return make_empty();
}

if (b.xmin < m_extent.xmin || b.ymin < m_extent.ymin || b.xmax > m_extent.xmax || b.ymax > m_extent.ymax) {
throw std::range_error("Cannot shrink extent to bounds larger than original.");
}
Expand Down
11 changes: 11 additions & 0 deletions test/test_grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@ TEST_CASE("Shrink robustness (2)", "[grid]")
CHECK(reduced.ymax <= grid2.ymax());
}

TEST_CASE("Shrink to empty box", "[grid]")
{
Grid<bounded_extent> grid{ { 10, 10, 20, 20 }, 1, 1 };

Box empty = Box::make_empty();

auto reduced = grid.shrink_to_fit(empty);

CHECK(reduced.empty());
}

TEST_CASE("Cropping", "[grid]")
{
Grid<bounded_extent> grid{ { 0, 0, 10, 10 }, 0.5, 0.5 };
Expand Down
Loading