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
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions spatial/include/spatial/core/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ struct GeoTypes {
static LogicalType GEOMETRY();
static LogicalType WKB_BLOB();

static LogicalType RASTER();
static LogicalType RASTER_COORD();

static void Register(DatabaseInstance &db);

static LogicalType CreateEnumType(const string &name, const vector<string> &members);
Expand Down
5 changes: 5 additions & 0 deletions spatial/include/spatial/gdal/file_handler.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "spatial/common.hpp"
#include "spatial/gdal/raster/raster_registry.hpp"
#include "duckdb/main/database.hpp"

namespace spatial {
Expand All @@ -12,13 +13,17 @@ class DuckDBFileSystemHandler;
class GDALClientContextState : public ClientContextState {
string client_prefix;
DuckDBFileSystemHandler *fs_handler;
std::map<uintptr_t, std::unique_ptr<RasterRegistry>> registries;
std::mutex lock;

public:
explicit GDALClientContextState(ClientContext &context);
~GDALClientContextState() override;
void QueryEnd() override;
void QueryEnd(ClientContext &context) override;
string GetPrefix(const string &value) const;
static GDALClientContextState &GetOrCreate(ClientContext &context);
RasterRegistry &GetRasterRegistry(ClientContext &context);
};

} // namespace gdal
Expand Down
24 changes: 24 additions & 0 deletions spatial/include/spatial/gdal/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ struct GdalTableFunction : ArrowTableFunction {
static void Register(DatabaseInstance &db);
};

struct GdalRasterTableFunction {
private:
static unique_ptr<FunctionData> Bind(ClientContext &context, TableFunctionBindInput &input,
vector<LogicalType> &return_types, vector<string> &names);

static void Execute(ClientContext &context, TableFunctionInput &input, DataChunk &output);

static unique_ptr<NodeStatistics> Cardinality(ClientContext &context, const FunctionData *data);

static unique_ptr<TableRef> ReplacementScan(ClientContext &context, ReplacementScanInput &input,
optional_ptr<ReplacementScanData> data);

public:
static void Register(DatabaseInstance &db);
};

struct GdalDriversTableFunction {

struct BindData : public TableFunctionData {
Expand Down Expand Up @@ -61,6 +77,14 @@ struct GdalMetadataFunction {
static void Register(DatabaseInstance &db);
};

struct GdalRasterMetadataFunction {
static void Register(DatabaseInstance &db);
};

struct GdalRasterCopyFunction {
static void Register(DatabaseInstance &db);
};

} // namespace gdal

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

namespace spatial {

namespace gdal {

struct GdalAggregateFunctions {
public:
static void Register(DatabaseInstance &db) {
RegisterStRasterMosaicAgg(db);
RegisterStRasterUnionAgg(db);
}

private:
static void RegisterStRasterMosaicAgg(DatabaseInstance &db);
static void RegisterStRasterUnionAgg(DatabaseInstance &db);
};

} // namespace gdal

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

namespace spatial {

namespace gdal {

struct GdalCastFunctions {
public:
static void Register(DatabaseInstance &db);
};

} // namespace gdal

} // namespace spatial
114 changes: 114 additions & 0 deletions spatial/include/spatial/gdal/functions/raster_agg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#pragma once
#include "duckdb/function/aggregate_function.hpp"
#include "spatial/common.hpp"

class GDALDataset;

namespace spatial {

namespace gdal {

struct RasterAggState {
bool is_set;
std::vector<GDALDataset *> datasets;
};

struct RasterAggUnaryOperation {

template <class STATE>
static void Initialize(STATE &state) {
state.is_set = false;
}

template <class STATE, class OP>
static void Combine(const STATE &source, STATE &target, AggregateInputData &) {
if (!source.is_set) {
return;
}
if (!target.is_set) {
target = source;
return;
}
target.datasets.insert(target.datasets.end(), source.datasets.begin(), source.datasets.end());
}

template <class INPUT_TYPE, class STATE, class OP>
static void Operation(STATE &state, const INPUT_TYPE &input, AggregateUnaryInput &) {
GDALDataset *dataset = reinterpret_cast<GDALDataset *>(input);
state.is_set = true;
state.datasets.emplace_back(dataset);
}

template <class INPUT_TYPE, class STATE, class OP>
static void ConstantOperation(STATE &state, const INPUT_TYPE &input, AggregateUnaryInput &agg, idx_t) {
Operation<INPUT_TYPE, STATE, OP>(state, input, agg);
}

static bool IgnoreNull() {
return true;
}
};

struct RasterAggBinaryOperation {

template <class STATE>
static void Initialize(STATE &state) {
state.is_set = false;
}

template <class STATE, class OP>
static void Combine(const STATE &source, STATE &target, AggregateInputData &) {
if (!source.is_set) {
return;
}
if (!target.is_set) {
target = source;
return;
}
target.datasets.insert(target.datasets.end(), source.datasets.begin(), source.datasets.end());
}

template <class INPUT_TYPE, class OPTS_TYPE, class STATE, class OP>
static void Operation(STATE &state, const INPUT_TYPE &input, const OPTS_TYPE &opts, AggregateBinaryInput &) {
GDALDataset *dataset = reinterpret_cast<GDALDataset *>(input);
state.is_set = true;
state.datasets.emplace_back(dataset);
}

template <class INPUT_TYPE, class OPTS_TYPE, class STATE, class OP>
static void ConstantOperation(STATE &state, const INPUT_TYPE &input, const OPTS_TYPE &opts,
AggregateBinaryInput &agg, idx_t) {
Operation<INPUT_TYPE, OPTS_TYPE, STATE, OP>(state, input, opts, agg);
}

static bool IgnoreNull() {
return true;
}
};

struct RasterAggBindData : public FunctionData {
//! The client context for the function call
ClientContext &context;
//! The list of options for the function
std::vector<std::string> options;

explicit RasterAggBindData(ClientContext &context, std::vector<std::string> options)
: context(context), options(options) {
}

unique_ptr<FunctionData> Copy() const override {
return make_uniq<RasterAggBindData>(context, options);
}

bool Equals(const FunctionData &other_p) const override {
auto &other = other_p.Cast<RasterAggBindData>();
return options == other.options;
}
};

unique_ptr<FunctionData> BindRasterAggOperation(ClientContext &context, AggregateFunction &function,
vector<unique_ptr<Expression>> &arguments);

} // namespace gdal

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

namespace spatial {

namespace gdal {

struct GdalScalarFunctions {
public:
static void Register(DatabaseInstance &db) {
RegisterStGetSRID(db);
RegisterStGetGeometry(db);
RegisterStGetMetadata(db);
RegisterStGetHasNoBand(db);
RegisterStBandPixelType(db);
RegisterStBandPixelTypeName(db);
RegisterStBandNoDataValue(db);
RegisterStBandColorInterp(db);
RegisterStBandColorInterpName(db);
RegisterStRasterToWorldCoord(db);
RegisterStWorldToRasterCoord(db);
RegisterStGetValue(db);
RegisterStRasterFromFile(db);
RegisterStRasterAsFile(db);
RegisterStRasterWarp(db);
RegisterStRasterClip(db);
}

private:
// ST_SRID
static void RegisterStGetSRID(DatabaseInstance &db);

// ST_GetGeometry
static void RegisterStGetGeometry(DatabaseInstance &db);

// Raster Accessors:
// ST_Width, ST_Height, ST_NumBands,
// ST_UpperLeftX, ST_UpperLeftY, ST_ScaleX, ST_ScaleY, ST_SkewX, ST_SkewY,
// ST_PixelWidth, ST_PixelHeight
static void RegisterStGetMetadata(DatabaseInstance &db);

// ST_HasNoBand
static void RegisterStGetHasNoBand(DatabaseInstance &db);

// ST_GetBandPixelType
static void RegisterStBandPixelType(DatabaseInstance &db);

// ST_GetBandPixelTypeName
static void RegisterStBandPixelTypeName(DatabaseInstance &db);

// ST_GetBandNoDataValue
static void RegisterStBandNoDataValue(DatabaseInstance &db);

// ST_GetBandColorInterp
static void RegisterStBandColorInterp(DatabaseInstance &db);

// ST_GetBandColorInterpName
static void RegisterStBandColorInterpName(DatabaseInstance &db);

// ST_RasterToWorldCoord[XY]
static void RegisterStRasterToWorldCoord(DatabaseInstance &db);

// ST_WorldToRasterCoord[XY]
static void RegisterStWorldToRasterCoord(DatabaseInstance &db);

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

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

// ST_RasterAsFile
static void RegisterStRasterAsFile(DatabaseInstance &db);

// ST_RasterWarp
static void RegisterStRasterWarp(DatabaseInstance &db);

// ST_RasterClip
static void RegisterStRasterClip(DatabaseInstance &db);
};

} // namespace gdal

} // namespace spatial
Loading
Loading