Skip to content

Commit

Permalink
Add xio_gdal_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Dec 26, 2020
1 parent 156617a commit 2d2156f
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ set(XTENSOR_IO_HEADERS
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xio_aws_handler.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xio_disk_handler.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xio_gcs_handler.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xio_gdal_handler.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xio_gzip.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xio_file.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xio_vsilfile.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xnpz.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xtensor-io.hpp
${XTENSOR_IO_INCLUDE_DIR}/xtensor-io/xtensor_io_config.hpp
Expand Down
17 changes: 9 additions & 8 deletions include/xtensor-io/xio_binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "xtensor/xadapt.hpp"
#include "xtensor-io.hpp"
#include "xfile_array.hpp"
#include "xio_file.hpp"

namespace xt
{
Expand All @@ -24,13 +25,13 @@ namespace xt
// we check that `fclose` can be called on them!
template<typename T, class I>
auto load_bin_imp(I& file, std::string& buffer)
-> decltype(fclose(file), void())
-> decltype(file.ftell(), void())
{
fseek(file, 0, SEEK_END);
std::size_t size = ftell(file);
file.fseek(0, SEEK_END);
std::size_t size = file.ftell();
buffer.resize(size);
rewind(file);
fread(&buffer[0], 1, size, file);
file.rewind();
file.fread(&buffer[0], 1, size);
}

// load_bin "overload" for stream-like objects
Expand Down Expand Up @@ -61,10 +62,10 @@ namespace xt
// we check that `fclose` can be called on them!
template <class O>
auto dump_bin_imp(O& file, const char* uncompressed_buffer, std::size_t uncompressed_size)
-> decltype(fclose(file), void())
-> decltype(file.ftell(), void())
{
fwrite(uncompressed_buffer, 1, uncompressed_size, file);
fflush(file);
file.fwrite(uncompressed_buffer, 1, uncompressed_size);
file.fflush();
}

// dump_bin "overload" for stream-like objects
Expand Down
58 changes: 58 additions & 0 deletions include/xtensor-io/xio_file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef XTENSOR_IO_FILE_HPP
#define XTENSOR_IO_FILE_HPP

#include <stdio.h>

namespace xt
{
class xfile
{
public:
xfile(FILE* stream);
size_t fwrite(const void* ptr, size_t size, size_t count);
size_t fread(void* ptr, size_t size, size_t count);
long int ftell();
int fseek(long int offset, int origin);
void rewind();
int fflush();
private:
FILE* m_stream;
};

inline xfile::xfile(FILE* stream)
: m_stream(stream)
{
}

inline size_t xfile::fwrite(const void* ptr, size_t size, size_t count)
{
return ::fwrite(ptr, size, count, m_stream);
}

inline size_t xfile::fread(void* ptr, size_t size, size_t count)
{
return ::fread(ptr, size, count, m_stream);
}

inline long int xfile::ftell()
{
return ::ftell(m_stream);
}

inline int xfile::fseek(long int offset, int origin)
{
return ::fseek(m_stream, offset, origin);
}

inline void xfile::rewind()
{
::rewind(m_stream);
}

inline int xfile::fflush()
{
return ::fflush(m_stream);
}
}

#endif
85 changes: 85 additions & 0 deletions include/xtensor-io/xio_gdal_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#ifndef XTENSOR_IO_GDAL_HANDLER_HPP
#define XTENSOR_IO_GDAL_HANDLER_HPP

#include <xtensor/xarray.hpp>
#include <xtensor/xexpression.hpp>
#include <xtensor-io/xfile_array.hpp>
#include <xtensor-io/xio_vsilfile.hpp>

#include <cpl_vsi.h>

namespace xt
{
struct xio_gdal_config
{
};

template <class C>
class xio_gdal_handler
{
public:
using io_config = xio_gdal_config;

template <class E>
void write(const xexpression<E>& expression, const std::string& path, xfile_dirty dirty);

template <class ET>
void read(ET& array, const std::string& path);

void configure(const C& format_config, const xio_gdal_config& io_config);
void configure_io(const xio_gdal_config& io_config);

private:

C m_format_config;
};

template <class C>
template <class E>
inline void xio_gdal_handler<C>::write(const xexpression<E>& expression, const std::string& path, xfile_dirty dirty)
{
if (m_format_config.will_dump(dirty))
{
VSILFILE* out_file = VSIFOpenL(path.c_str(), "wb");
if (out_file != NULL)
{
auto f = xvsilfile(out_file);
dump_file(f, expression, m_format_config);
}
else
{
XTENSOR_THROW(std::runtime_error, "write: failed to open file " + path);
}
}
}

template <class C>
template <class ET>
inline void xio_gdal_handler<C>::read(ET& array, const std::string& path)
{
VSILFILE* in_file = VSIFOpenL(path.c_str(), "rb");
if (in_file != NULL)
{
auto f = xvsilfile(in_file);
load_file<ET>(f, array, m_format_config);
}
else
{
XTENSOR_THROW(std::runtime_error, "read: failed to open file " + path);
}
}

template <class C>
inline void xio_gdal_handler<C>::configure(const C& format_config, const xio_gdal_config& io_config)
{
m_format_config = format_config;
}

template <class C>
inline void xio_gdal_handler<C>::configure_io(const xio_gdal_config& io_config)
{
}

}

#endif
58 changes: 58 additions & 0 deletions include/xtensor-io/xio_vsilfile.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef XTENSOR_IO_VSILFILE_HPP
#define XTENSOR_IO_VSILFILE_HPP

#include <cpl_vsi.h>

namespace xt
{
class xvsilfile
{
public:
xvsilfile(VSILFILE* stream);
size_t fwrite(const void* ptr, size_t size, size_t count);
size_t fread(void* ptr, size_t size, size_t count);
long int ftell();
int fseek(long int offset, int origin);
void rewind();
int fflush();
private:
VSILFILE* m_stream;
};

inline xvsilfile::xvsilfile(VSILFILE* stream)
: m_stream(stream)
{
}

inline size_t xvsilfile::fwrite(const void* ptr, size_t size, size_t count)
{
return VSIFWriteL(ptr, size, count, m_stream);
}

inline size_t xvsilfile::fread(void* ptr, size_t size, size_t count)
{
return VSIFReadL(ptr, size, count, m_stream);
}

inline long int xvsilfile::ftell()
{
return VSIFTellL(m_stream);
}

inline int xvsilfile::fseek(long int offset, int origin)
{
return VSIFSeekL(m_stream, offset, origin);
}

inline void xvsilfile::rewind()
{
VSIRewindL(m_stream);
}

inline int xvsilfile::fflush()
{
return VSIFFlushL(m_stream);
}
}

#endif
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ set(XTENSOR_IO_TESTS
test_xio_binary.cpp
test_xio_gcs_handler.cpp
test_xio_aws_handler.cpp
test_xio_gdal_handler.cpp
)

set(XTENSOR_IO_HO_TESTS
Expand Down

0 comments on commit 2d2156f

Please sign in to comment.