Skip to content

Commit

Permalink
Refactor, fix C/I
Browse files Browse the repository at this point in the history
  • Loading branch information
kordejong committed Feb 5, 2024
1 parent a5db900 commit c6e48a5
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 36 deletions.
15 changes: 15 additions & 0 deletions source/data_model/gdal/include/configure.hpp.in
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
#pragma once
#include <gdal_priv.h>


#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#define LUE_GDAL_SUPPORTS_64BIT_INTEGERS 1
#else
#define LUE_GDAL_SUPPORTS_64BIT_INTEGERS 0
#endif


namespace lue::gdal {

static constexpr bool supports_64bit_integers{LUE_GDAL_SUPPORTS_64BIT_INTEGERS};

} // namespace lue::gdal
1 change: 1 addition & 0 deletions source/data_model/gdal/include/lue/gdal.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "lue/gdal/configure.hpp"
#include "lue/gdal/dataset.hpp"
#include "lue/gdal/driver.hpp"
#include "lue/gdal/raster.hpp"
Expand Down
6 changes: 6 additions & 0 deletions source/data_model/gdal/include/lue/gdal/raster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ namespace lue::gdal {

[[nodiscard]] auto data_type() const -> GDALDataType;

template<typename Element>
[[nodiscard]] auto no_data_value() const -> std::tuple<Element, bool>
{
return gdal::no_data_value<Element>(*_band);
}

[[nodiscard]] auto blocks() const -> Blocks;

auto read_block(Offset const& block_offset, void* buffer) const -> void;
Expand Down
4 changes: 2 additions & 2 deletions source/data_model/gdal/include/lue/gdal/raster_band.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace lue::gdal {
}


#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
// "An explicit specialization of a function template is inline only if it is declared
// with the inline specifier (or defined as deleted), it doesn't matter if the primary
// template is inline."
Expand All @@ -56,7 +56,7 @@ namespace lue::gdal {
}


#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
// "An explicit specialization of a function template is inline only if it is declared
// with the inline specifier (or defined as deleted), it doesn't matter if the primary
// template is inline."
Expand Down
2 changes: 1 addition & 1 deletion source/data_model/gdal/include/lue/gdal/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace lue::gdal {
};


#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
template<>
class TypeTraits<std::uint64_t>
{
Expand Down
15 changes: 14 additions & 1 deletion source/data_model/gdal/src/dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,20 @@ namespace lue::gdal {

if (band_ptr == nullptr)
{
throw std::runtime_error(fmt::format("Band {} cannot be obtained", band_nr));
Count const nr_bands{nr_raster_bands(dataset)};

if (band_nr < 0 || band_nr > nr_bands)
{
throw std::runtime_error(fmt::format(
"Requested raster band ({}) is outside the valid range ([{} - {}]",
band_nr,
1,
nr_bands));
}
else
{
throw std::runtime_error(fmt::format("Band {} cannot be obtained", band_nr));
}
}

return band_ptr;
Expand Down
1 change: 1 addition & 0 deletions source/data_model/gdal/src/driver.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "lue/gdal/driver.hpp"
#include <stdexcept>


namespace lue::gdal {
Expand Down
4 changes: 1 addition & 3 deletions source/data_model/gdal/src/raster.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "lue/gdal/raster.hpp"
#include <cassert>
#include <stdexcept>


namespace lue::gdal {
Expand Down Expand Up @@ -211,9 +212,6 @@ namespace lue::gdal {

auto Raster::band(Count const band_nr) const -> Band
{
assert(band_nr > 0);
assert(band_nr <= nr_bands());

return Raster::Band{raster_band(*_dataset_ptr, band_nr)};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#pragma once
#include "lue/gdal.hpp"
#include "lue/hl/raster_stack.hpp"
#include "lue/translate/format/gdal_raster.hpp"
#include "lue/utility/progress_indicator.hpp"
#include <string>


namespace lue {
namespace utility {

GDALDatasetPtr try_open_gdal_raster_stack_dataset_for_read(std::string const& dataset_name);
gdal::DatasetPtr try_open_gdal_raster_stack_dataset_for_read(std::string const& dataset_name);

class GDALStack
{
Expand Down
6 changes: 3 additions & 3 deletions source/data_model/translate/src/format/gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace lue::utility {
result = hdf5::native_datatype<std::int32_t>();
break;
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
case GDT_UInt64:
{
result = hdf5::native_datatype<std::uint64_t>();
Expand Down Expand Up @@ -92,7 +92,7 @@ namespace lue::utility {
{
result = gdal::data_type_v<std::int32_t>;
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
else if (data_type == hdf5::native_uint64)
{
result = gdal::data_type_v<std::uint64_t>;
Expand Down Expand Up @@ -734,7 +734,7 @@ namespace lue::utility {
gdal_to_lue<RasterView, int32_t>(gdal_raster_band, lue_raster_layer);
break;
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
case GDT_UInt64:
{
gdal_to_lue<RasterView, uint64_t>(gdal_raster_band, lue_raster_layer);
Expand Down
16 changes: 8 additions & 8 deletions source/data_model/translate/src/format/gdal_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace lue {

namespace underscore {

GDALDatasetPtr try_open_gdal_raster_stack_dataset_for_read(std::string const& dataset_name)
gdal::DatasetPtr try_open_gdal_raster_stack_dataset_for_read(std::string const& dataset_name)
{
// What makes a raster a stack:
// - dataset_name matches one of the folowing patterns:
Expand All @@ -30,7 +30,7 @@ namespace lue {

auto const dataset_path = std::filesystem::path{dataset_name};
auto const directory_path = dataset_path.parent_path();
GDALDatasetPtr result;
gdal::DatasetPtr result;
auto stack_rule = utility::stack_rule(dataset_name);

// This only works with Boost >= 1.62.0
Expand Down Expand Up @@ -108,7 +108,7 @@ namespace lue {
}


GDALDatasetPtr open_slice_for_read(
gdal::DatasetPtr open_slice_for_read(
std::string const& dataset_name, GDALStack::SliceIndex const slice_idx)
{
return open_gdal_raster_dataset_for_read(slice_dataset_name(dataset_name, slice_idx));
Expand All @@ -117,12 +117,12 @@ namespace lue {
} // namespace underscore


GDALDatasetPtr open_slice_for_read(
gdal::DatasetPtr open_slice_for_read(
GDALStack::NamingConvention const naming_convention,
std::string const& dataset_name,
GDALStack::SliceIndex const slice_idx)
{
GDALDatasetPtr result;
gdal::DatasetPtr result;

switch (naming_convention)
{
Expand All @@ -144,7 +144,7 @@ namespace lue {
@return A pointer to a ::GDALDataset instance if the dataset can be
opened. Otherwise a pointer containing nullptr.
*/
GDALDatasetPtr try_open_gdal_raster_stack_dataset_for_read(std::string const& dataset_name)
gdal::DatasetPtr try_open_gdal_raster_stack_dataset_for_read(std::string const& dataset_name)
{
auto result = underscore::try_open_gdal_raster_stack_dataset_for_read(dataset_name);

Expand Down Expand Up @@ -365,7 +365,7 @@ namespace lue {
write<int32_t>(raster_stack_band, progress_indicator);
break;
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
case GDT_UInt64:
{
write<uint64_t>(raster_stack_band, progress_indicator);
Expand Down Expand Up @@ -408,7 +408,7 @@ namespace lue {

{
// Open the stack and read the domain and discretization information
GDALDatasetPtr raster_dataset;
gdal::DatasetPtr raster_dataset;

switch (_naming_convention)
{
Expand Down
4 changes: 2 additions & 2 deletions source/data_model/translate/src/translate.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "lue/translate/translate.hpp"
#include "lue/gdal.hpp"
#include "lue/translate/command.hpp"
#include <fmt/format.h>
#include <gdal_priv.h>
#include <hdf5.h>


Expand Down Expand Up @@ -87,7 +87,7 @@ namespace lue {
// lots of messages we usually don't care about.
H5Eset_auto(H5E_DEFAULT, nullptr, nullptr);

GDALAllRegister();
gdal::register_gdal_drivers();
}


Expand Down
15 changes: 7 additions & 8 deletions source/framework/io/src/raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,14 +427,13 @@ namespace lue {
Element no_data_value;

{
gdal::DatasetPtr dataset_ptr{gdal::open_dataset(name, ::GA_ReadOnly)};

array_shape[0] = dataset_ptr->GetRasterYSize();
array_shape[1] = dataset_ptr->GetRasterXSize();

gdal::RasterBandPtr band_ptr{gdal::raster_band(*dataset_ptr)};
gdal::Raster raster{gdal::open_dataset(name, ::GA_ReadOnly)};
gdal::Raster::Band raster_band{raster.band(1)};

std::tie(no_data_value, no_data_value_is_valid) = lue::no_data_value<Element>(*band_ptr);
auto const raster_shape = raster.shape();
array_shape[0] = static_cast<Count>(raster_shape[0]);
array_shape[1] = static_cast<Count>(raster_shape[1]);
std::tie(no_data_value, no_data_value_is_valid) = raster_band.no_data_value<Element>();
}

using Functor = ReadPartitionsPerLocality<Element>;
Expand Down Expand Up @@ -595,7 +594,7 @@ namespace lue {
INSTANTIATE(uint8_t)
INSTANTIATE(uint32_t)
INSTANTIATE(int32_t)
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
INSTANTIATE(uint64_t)
INSTANTIATE(int64_t)
#endif
Expand Down
2 changes: 1 addition & 1 deletion source/framework/python/src/gdal/from_gdal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ namespace lue::framework {
{
result = from_gdal<int32_t>(name, static_partition_shape);
}
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
#if LUE_GDAL_SUPPORTS_64BIT_INTEGERS
else if (data_type == GDT_UInt64)
{
result = from_gdal<uint64_t>(name, static_partition_shape);
Expand Down
11 changes: 6 additions & 5 deletions source/framework/python/src/gdal/to_gdal.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "lue/framework/io/raster.hpp"
#include "lue/gdal.hpp"
#include <pybind11/pybind11.h>
#include <gdal_priv.h>


using namespace pybind11::literals;
Expand All @@ -13,10 +13,11 @@ namespace lue::framework {
module.def("to_gdal", write<std::uint8_t>, "array"_a, "name"_a, "clone_name"_a = "");
module.def("to_gdal", write<std::uint32_t>, "array"_a, "name"_a, "clone_name"_a = "");
module.def("to_gdal", write<std::int32_t>, "array"_a, "name"_a, "clone_name"_a = "");
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
module.def("to_gdal", write<std::uint64_t>, "array"_a, "name"_a, "clone_name"_a = "");
module.def("to_gdal", write<std::int64_t>, "array"_a, "name"_a, "clone_name"_a = "");
#endif
if constexpr (gdal::supports_64bit_integers)
{
module.def("to_gdal", write<std::uint64_t>, "array"_a, "name"_a, "clone_name"_a = "");
module.def("to_gdal", write<std::int64_t>, "array"_a, "name"_a, "clone_name"_a = "");
}
module.def("to_gdal", write<float>, "array"_a, "name"_a, "clone_name"_a = "");
module.def("to_gdal", write<double>, "array"_a, "name"_a, "clone_name"_a = "");
}
Expand Down

0 comments on commit c6e48a5

Please sign in to comment.