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

Add raster type & functions to manage rasters in the spatial extension #298

Open
wants to merge 29 commits into
base: v1.0.0
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fb953e4
Load & show GDAL Drivers that support raster
ahuarte47 Mar 27, 2024
ac99dbe
Add ST_ReadRaster function
ahuarte47 Mar 28, 2024
2960227
Add ST_ReadRaster_Meta table function
ahuarte47 Mar 28, 2024
9393387
Add ST_SRID function
ahuarte47 Mar 28, 2024
2c7ad12
Add ST_GetGeometry function
ahuarte47 Mar 28, 2024
7f79e36
Add methods to get metadata from a raster
ahuarte47 Mar 28, 2024
64a25b6
Add methods to get metadata from a raster band
ahuarte47 Mar 28, 2024
1846161
Add ST_WorldToRasterCoord[XY] & ST_RasterToWorldCoord[XY] functions
ahuarte47 Mar 28, 2024
bfdc07f
Add ST_Value function
ahuarte47 Mar 28, 2024
d932a7c
Add ST_RasterFromFile function
ahuarte47 Mar 28, 2024
0cc5aad
Add ST_RasterAsFile function
ahuarte47 Mar 28, 2024
3bb40c6
Add aggregate functions (ST_RasterMosaic_Agg, ST_RasterUnion_Agg)
ahuarte47 Mar 28, 2024
a22a85c
Add ST_RasterWarp function
ahuarte47 Mar 28, 2024
ef7f2d3
Add ST_RasterClip function
ahuarte47 Mar 28, 2024
8c93e3a
Add ResampleAlg enum
ahuarte47 Mar 28, 2024
397c71e
Ass CopyTo raster function
ahuarte47 Mar 28, 2024
e2c37b3
Add tests & sample data for testing
ahuarte47 Mar 29, 2024
faec181
Add docs of functions
ahuarte47 Apr 2, 2024
3ebb335
Add missing registration of RASTER_COORD
ahuarte47 Apr 3, 2024
fef62bf
Changes for v0.10.1
ahuarte47 Apr 4, 2024
03fa25b
Remove docs in old style, they will be added later in current style
ahuarte47 Apr 4, 2024
14a5ff7
Add documentation of raster functions
ahuarte47 Apr 7, 2024
9aef365
Fix format
ahuarte47 Apr 8, 2024
9ca99bb
Fix undefined symbols for MacOS
ahuarte47 Apr 8, 2024
529c5e2
Merge remote-tracking branch 'upstream/v0.10.2' into main_raster-type
ahuarte47 May 1, 2024
678b4de
Changes for updating to v0.10.2
ahuarte47 May 1, 2024
7a8862e
Temporal fix when creating a Geometry from a BBOX
ahuarte47 May 1, 2024
3233443
Merge remote-tracking branch 'upstream/v1.0.0' into main_raster-type
ahuarte47 Jun 9, 2024
029ff33
Changes for updating to v1.0.0
ahuarte47 Jun 9, 2024
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
Prev Previous commit
Next Next commit
Add ST_RasterFromFile function
ahuarte47 committed Apr 3, 2024
commit d932a7ca76aa467c8883e5550b40429a8aa2a786
4 changes: 4 additions & 0 deletions spatial/include/spatial/gdal/functions/scalar.hpp
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ struct GdalScalarFunctions {
RegisterStRasterToWorldCoord(db);
RegisterStWorldToRasterCoord(db);
RegisterStGetValue(db);
RegisterStRasterFromFile(db);
}

private:
@@ -61,6 +62,9 @@ struct GdalScalarFunctions {

// ST_Value
static void RegisterStGetValue(DatabaseInstance &db);

// ST_RasterFromFile
static void RegisterStRasterFromFile(DatabaseInstance &db);
};

} // namespace gdal
8 changes: 8 additions & 0 deletions spatial/include/spatial/gdal/raster/raster_factory.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "spatial/common.hpp"

class GDALDataset;

namespace spatial {

namespace gdal {
@@ -10,6 +12,12 @@ namespace gdal {
class RasterFactory {
public:

//! Given a file path, returns a GDALDataset
static GDALDataset *FromFile(const std::string &file_path,
const std::vector<std::string> &allowed_drivers = std::vector<std::string>(),
const std::vector<std::string> &open_options = std::vector<std::string>(),
const std::vector<std::string> &sibling_files = std::vector<std::string>());

//! Transforms a vector of strings as a vector of const char pointers.
static std::vector<char const *> FromVectorOfStrings(const std::vector<std::string> &input);
//! Transforms a map of params as a vector of const char pointers.
1 change: 1 addition & 0 deletions spatial/src/spatial/gdal/functions/scalar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ set(EXTENSION_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/st_band_pixel_type.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_has_no_band.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_metadata.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_raster_from_file.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_raster_geometry.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_raster_to_world_coord.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_srid.cpp
56 changes: 56 additions & 0 deletions spatial/src/spatial/gdal/functions/scalar/st_raster_from_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/common/vector_operations/generic_executor.hpp"
#include "spatial/common.hpp"
#include "spatial/core/types.hpp"
#include "spatial/gdal/file_handler.hpp"
#include "spatial/gdal/functions/scalar.hpp"
#include "spatial/gdal/raster/raster.hpp"
#include "spatial/gdal/raster/raster_factory.hpp"
#include "spatial/gdal/raster/raster_registry.hpp"

#include "gdal_priv.h"

namespace spatial {

namespace gdal {

//------------------------------------------------------------------------------
// ST_RasterFromFile
//------------------------------------------------------------------------------

static void RasterFromFileFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &context = state.GetContext();

UnaryExecutor::Execute<string_t, uintptr_t>(args.data[0], result, args.size(), [&](string_t input) {
auto &ctx_state = GDALClientContextState::GetOrCreate(context);

auto raw_file_name = input.GetString();
auto prefixed_file_name = ctx_state.GetPrefix() + raw_file_name;

GDALDataset *dataset = RasterFactory::FromFile(prefixed_file_name);
if (dataset == nullptr) {
auto error = Raster::GetLastErrorMsg();
throw IOException("Could not open file: " + raw_file_name + " (" + error + ")");
}

auto &raster_registry = ctx_state.GetRasterRegistry(context);
raster_registry.RegisterRaster(dataset);

return CastPointerToValue(dataset);
});
}

//------------------------------------------------------------------------------
// Register functions
//------------------------------------------------------------------------------

void GdalScalarFunctions::RegisterStRasterFromFile(DatabaseInstance &db) {

ScalarFunctionSet set("ST_RasterFromFile");
set.AddFunction(ScalarFunction({LogicalType::VARCHAR}, GeoTypes::RASTER(), RasterFromFileFunction));
ExtensionUtil::RegisterFunction(db, set);
}

} // namespace gdal

} // namespace spatial
21 changes: 21 additions & 0 deletions spatial/src/spatial/gdal/raster/raster_factory.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
#include "spatial/common.hpp"
#include "spatial/gdal/raster/raster_factory.hpp"

#include "gdal_priv.h"

namespace spatial {

namespace gdal {

GDALDataset *RasterFactory::FromFile(const std::string& file_path,
const std::vector<std::string> &allowed_drivers,
const std::vector<std::string> &open_options,
const std::vector<std::string> &sibling_files) {

auto gdal_allowed_drivers = RasterFactory::FromVectorOfStrings(allowed_drivers);
auto gdal_open_options = RasterFactory::FromVectorOfStrings(open_options);
auto gdal_sibling_files = RasterFactory::FromVectorOfStrings(sibling_files);

GDALDataset *dataset =
GDALDataset::Open(file_path.c_str(),
GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
gdal_allowed_drivers.empty() ? nullptr : gdal_allowed_drivers.data(),
gdal_open_options.empty() ? nullptr : gdal_open_options.data(),
gdal_sibling_files.empty() ? nullptr : gdal_sibling_files.data());

return dataset;
}

std::vector<char const *> RasterFactory::FromVectorOfStrings(const std::vector<std::string> &input) {
auto output = std::vector<char const *>();