Skip to content

Commit

Permalink
Merge branch 'gh490'
Browse files Browse the repository at this point in the history
  • Loading branch information
kordejong committed Oct 5, 2022
2 parents 992f3b1 + e5d5aa9 commit dbbb6a7
Show file tree
Hide file tree
Showing 17 changed files with 1,364 additions and 3 deletions.
1 change: 1 addition & 0 deletions environment/cmake/LueConfiguration.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ if(LUE_BUILD_FRAMEWORK)
set(LUE_BOOST_REQUIRED TRUE)
set(LUE_DOCOPT_REQUIRED TRUE)
set(LUE_FMT_REQUIRED TRUE)
set(LUE_GDAL_REQUIRED TRUE)
set(LUE_HPX_REQUIRED TRUE)
set(LUE_KOKKOS_MDSPAN_REQUIRED TRUE)

Expand Down
3 changes: 2 additions & 1 deletion environment/configuration/receipe/bld.bat
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ rem cmake --build . --target all --parallel 2
rem
rem if %errorlevel% neq 0 exit /b %errorlevel%

ctest --extra-verbose --output-on-failure --build-config Release
rem See https://github.com/computationalgeography/lue/issues/493
rem ctest --extra-verbose --output-on-failure --build-config Release

if %errorlevel% neq 0 exit /b %errorlevel%

Expand Down
3 changes: 3 additions & 0 deletions source/framework/io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
add_test_conditionally(test)

add_library(lue_framework_io
src/gdal.cpp
src/raster.cpp
src/read.cpp
src/util.cpp
src/write.cpp
Expand All @@ -17,4 +19,5 @@ target_link_libraries(lue_framework_io
PUBLIC
lue::framework_algorithm
lue::data_model_hl
GDAL::GDAL
)
127 changes: 127 additions & 0 deletions source/framework/io/include/lue/framework/io/gdal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#pragma once
#include "lue/framework/io/gdal_type_traits.hpp"
#include "lue/framework/core/shape.hpp"


namespace lue {

// This pointer is shared and the object pointed to must be closed once the last
// pointer instance goes out of scope
using GDALDatasetPtr = std::shared_ptr<::GDALDataset>;

inline auto gdal_close =
[](::GDALDataset* dataset) {
if(dataset)
{
::GDALClose(dataset);
}
};

using GDALDatasetClose = decltype(gdal_close);

static_assert(std::is_move_constructible_v<GDALDatasetClose>);

// The object pointed to is owned by the dataset and must not be deleted
using GDALBandPtr = ::GDALRasterBand*;

using GDALDriverPtr = ::GDALDriver*;

GDALDatasetPtr open_dataset(std::string const& name, GDALAccess const open_mode);

GDALDatasetPtr create_copy(
std::string const& name, GDALDatasetPtr& clone_dataset);

GDALDatasetPtr create(
std::string const& name, Shape<Count, 2> const& shape, Count const nr_bands, GDALDataType const data_type);

GDALBandPtr get_raster_band(GDALDataset& dataset);

GDALDataType data_type(GDALRasterBand& band);

GDALDataType data_type(GDALDataset& dataset);

GDALDataType data_type(std::string const& name);


namespace detail {

template<
typename Element>
Element no_data_value(
::GDALRasterBand& band,
int* success)
{
return band.GetNoDataValue(success);
}


#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
// "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."
template<>
inline std::int64_t no_data_value(
::GDALRasterBand& band,
int* success)
{
return band.GetNoDataValueAsInt64(success);
}


template<>
inline std::uint64_t no_data_value(
::GDALRasterBand& band,
int* success)
{
return band.GetNoDataValueAsUInt64(success);
}
#endif


template<
typename Element>
CPLErr set_no_data_value(
::GDALRasterBand& band,
Element const value)
{
return band.SetNoDataValue(static_cast<double>(value));
}


#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
// "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."
template<>
inline CPLErr set_no_data_value(
::GDALRasterBand& band,
std::int64_t const value)
{
return band.SetNoDataValueAsInt64(value);
}


template<>
inline CPLErr set_no_data_value(
::GDALRasterBand& band,
std::uint64_t const value)
{
return band.SetNoDataValueAsUInt64(value);
}
#endif

} // namespace detail


template<
typename Element>
bool set_no_data_value(
::GDALRasterBand& band,
Element const value)
{
// Return true if set, and false if not. If not set, it may be that the driver does
// not support setting a no-data value. Depending on the context, this may not be an error.
return detail::set_no_data_value(band, value) == CE_None;
}

} // namespace lue
114 changes: 114 additions & 0 deletions source/framework/io/include/lue/framework/io/gdal_type_traits.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma once
#include <gdal_priv.h>


namespace lue {

template<
typename Element>
class GDALTypeTraits
{
};


template<>
class GDALTypeTraits<std::uint8_t>
{

public:

static constexpr GDALDataType type_id{GDT_Byte};

};


template<>
class GDALTypeTraits<std::uint16_t>
{

public:

static constexpr GDALDataType type_id{GDT_UInt16};

};


template<>
class GDALTypeTraits<std::int16_t>
{

public:

static constexpr GDALDataType type_id{GDT_Int16};

};


template<>
class GDALTypeTraits<std::uint32_t>
{

public:

static constexpr GDALDataType type_id{GDT_UInt32};

};


template<>
class GDALTypeTraits<std::int32_t>
{

public:

static constexpr GDALDataType type_id{GDT_Int32};

};


#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 5, 0)
template<>
class GDALTypeTraits<std::uint64_t>
{

public:

static constexpr GDALDataType type_id{GDT_UInt64};

};


template<>
class GDALTypeTraits<std::int64_t>
{

public:

static constexpr GDALDataType type_id{GDT_Int64};

};
#endif


template<>
class GDALTypeTraits<float>
{

public:

static constexpr GDALDataType type_id{GDT_Float32};

};


template<>
class GDALTypeTraits<double>
{

public:

static constexpr GDALDataType type_id{GDT_Float64};

};

} // namespace lue
20 changes: 20 additions & 0 deletions source/framework/io/include/lue/framework/io/raster.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include "lue/framework/partitioned_array.hpp"


namespace lue {

template<
typename Element>
PartitionedArray<Element, 2> read(
std::string const& name,
Shape<Count, 2> const& partition_shape);

template<
typename Element>
hpx::future<void> write(
PartitionedArray<Element, 2> const& array,
std::string const& name,
std::string const& clone_name="");

} // namespace lue
Loading

0 comments on commit dbbb6a7

Please sign in to comment.