Skip to content

Commit

Permalink
Changes for v0.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuarte47 committed Apr 4, 2024
1 parent 3ebb335 commit fef62bf
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 31 deletions.
7 changes: 4 additions & 3 deletions spatial/include/spatial/gdal/raster/raster.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include "spatial/common.hpp"
#include "spatial/core/types.hpp"
#include "spatial/core/geometry/geometry.hpp"
#include "spatial/core/geometry/geometry_factory.hpp"
#include "spatial/core/geometry/geometry_type.hpp"
#include "spatial/gdal/types.hpp"

class GDALDataset;
Expand Down Expand Up @@ -47,7 +48,7 @@ class Raster {
bool GetInvGeoTransform(double *inv_matrix) const;

//! Returns the polygon representation of the extent of the raster
Polygon GetGeometry(GeometryFactory &factory) const;
Polygon GetGeometry(ArenaAllocator &allocator) const;

//! Returns the geometric X and Y (longitude and latitude) given a column and row
bool RasterToWorldCoord(PointXY &point, int32_t col, int32_t row) const;
Expand Down Expand Up @@ -76,7 +77,7 @@ class Raster {

//! Returns a raster that is clipped by the input geometry
static GDALDataset *Clip(GDALDataset *dataset,
const Geometry &geometry,
const geometry_t &geometry,
const std::vector<std::string> &options = std::vector<std::string>());

//! Get the last error message.
Expand Down
3 changes: 1 addition & 2 deletions spatial/src/spatial/gdal/functions/cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "spatial/core/types.hpp"
#include "spatial/core/functions/common.hpp"
#include "spatial/core/geometry/geometry.hpp"
#include "spatial/core/geometry/vertex_vector.hpp"
#include "spatial/gdal/functions/cast.hpp"
#include "spatial/gdal/raster/raster.hpp"

Expand Down Expand Up @@ -35,7 +34,7 @@ static bool RasterToGeometryCast(Vector &source, Vector &result, idx_t count, Ca

UnaryExecutor::Execute<uintptr_t, geometry_t>(source, result, count, [&](uintptr_t &input) {
Raster raster(reinterpret_cast<GDALDataset *>(input));
return lstate.factory.Serialize(result, raster.GetGeometry(lstate.factory), false, false);
return Geometry(raster.GetGeometry(lstate.arena)).Serialize(result);
});
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void RasterAsFileFunction_01(DataChunk &args, ExpressionState &state, Vec

auto raw_file_name = file_name.GetString();
auto &client_ctx = GDALClientContextState::GetOrCreate(context);
auto prefixed_file_name = client_ctx.GetPrefix() + raw_file_name;
auto prefixed_file_name = client_ctx.GetPrefix(raw_file_name);

if (!RasterFactory::WriteFile(dataset, prefixed_file_name, gdal_driver_name)) {
auto error = Raster::GetLastErrorMsg();
Expand Down Expand Up @@ -75,7 +75,7 @@ static void RasterAsFileFunction_02(DataChunk &args, ExpressionState &state, Vec

auto raw_file_name = file_name.GetString();
auto &client_ctx = GDALClientContextState::GetOrCreate(context);
auto prefixed_file_name = client_ctx.GetPrefix() + raw_file_name;
auto prefixed_file_name = client_ctx.GetPrefix(raw_file_name);

auto options = std::vector<std::string>();

Expand Down
18 changes: 8 additions & 10 deletions spatial/src/spatial/gdal/functions/scalar/st_raster_clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace gdal {

static void RasterClipFunction_01(DataChunk &args, ExpressionState &state, Vector &result) {
auto &context = state.GetContext();
auto &lstate = GeometryFunctionLocalState::ResetAndGet(state);
auto &ctx_state = GDALClientContextState::GetOrCreate(context);

using POINTER_TYPE = PrimitiveType<uintptr_t>;
Expand All @@ -33,7 +32,7 @@ static void RasterClipFunction_01(DataChunk &args, ExpressionState &state, Vecto
GenericExecutor::ExecuteBinary<POINTER_TYPE, GEOMETRY_TYPE, POINTER_TYPE>(p1, p2, result, args.size(),
[&](POINTER_TYPE p1, GEOMETRY_TYPE p2) {
auto input = p1.val;
auto geometry = lstate.factory.Deserialize(p2.val);
auto geometry = p2.val;

GDALDataset *dataset = reinterpret_cast<GDALDataset *>(input);

Expand All @@ -58,7 +57,6 @@ static void RasterClipFunction_01(DataChunk &args, ExpressionState &state, Vecto

static void RasterClipFunction_02(DataChunk &args, ExpressionState &state, Vector &result) {
auto &context = state.GetContext();
auto &lstate = GeometryFunctionLocalState::ResetAndGet(state);
auto &ctx_state = GDALClientContextState::GetOrCreate(context);

using POINTER_TYPE = PrimitiveType<uintptr_t>;
Expand All @@ -73,7 +71,7 @@ static void RasterClipFunction_02(DataChunk &args, ExpressionState &state, Vecto
GenericExecutor::ExecuteTernary<POINTER_TYPE, GEOMETRY_TYPE, LIST_TYPE, POINTER_TYPE>(p1, p2, p3, result, args.size(),
[&](POINTER_TYPE p1, GEOMETRY_TYPE p2, LIST_TYPE p3_offlen) {
auto input = p1.val;
auto geometry = lstate.factory.Deserialize(p2.val);
auto geometry = p2.val;
auto offlen = p3_offlen.val;

GDALDataset *dataset = reinterpret_cast<GDALDataset *>(input);
Expand Down Expand Up @@ -112,13 +110,13 @@ static void RasterClipFunction_02(DataChunk &args, ExpressionState &state, Vecto
void GdalScalarFunctions::RegisterStRasterClip(DatabaseInstance &db) {

ScalarFunctionSet set("ST_RasterClip");
set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY()}, GeoTypes::RASTER(), RasterClipFunction_01,
nullptr, nullptr, nullptr,
GeometryFunctionLocalState::Init));
set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY()},
GeoTypes::RASTER(),
RasterClipFunction_01));

set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY(), LogicalType::LIST(LogicalType::VARCHAR)}, GeoTypes::RASTER(), RasterClipFunction_02,
nullptr, nullptr, nullptr,
GeometryFunctionLocalState::Init));
set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY(), LogicalType::LIST(LogicalType::VARCHAR)},
GeoTypes::RASTER(),
RasterClipFunction_02));

ExtensionUtil::RegisterFunction(db, set);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void RasterFromFileFunction(DataChunk &args, ExpressionState &state, Vect
auto &ctx_state = GDALClientContextState::GetOrCreate(context);

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

GDALDataset *dataset = RasterFactory::FromFile(prefixed_file_name);
if (dataset == nullptr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "spatial/common.hpp"
#include "spatial/core/types.hpp"
#include "spatial/core/functions/common.hpp"
#include "spatial/core/geometry/geometry.hpp"
#include "spatial/gdal/functions/scalar.hpp"
#include "spatial/gdal/raster/raster.hpp"

Expand All @@ -23,7 +24,7 @@ static void RasterGetGeometryFunction(DataChunk &args, ExpressionState &state, V

UnaryExecutor::Execute<uintptr_t, geometry_t>(args.data[0], result, args.size(), [&](uintptr_t input) {
Raster raster(reinterpret_cast<GDALDataset *>(input));
return lstate.factory.Serialize(result, raster.GetGeometry(lstate.factory), false, false);
return Geometry(raster.GetGeometry(lstate.arena)).Serialize(result);
});
}

Expand Down
2 changes: 1 addition & 1 deletion spatial/src/spatial/gdal/functions/st_read_raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void GdalRasterTableFunction::Execute(ClientContext &context, TableFunctionInput
// Now we can open the dataset
auto raw_file_name = bind_data.file_name;
auto &ctx_state = GDALClientContextState::GetOrCreate(context);
auto prefixed_file_name = ctx_state.GetPrefix() + raw_file_name;
auto prefixed_file_name = ctx_state.GetPrefix(raw_file_name);
auto dataset =
GDALDataset::Open(prefixed_file_name.c_str(), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
gdal_allowed_drivers.empty() ? nullptr : gdal_allowed_drivers.data(),
Expand Down
2 changes: 1 addition & 1 deletion spatial/src/spatial/gdal/functions/st_read_raster_meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void Scan(ClientContext &context, TableFunctionInput &input, DataChunk &o

for (idx_t out_idx = 0; out_idx < out_size; out_idx++, state.current_file_idx++) {
auto file_name = bind_data.file_names[state.current_file_idx];
auto prefixed_file_name = GDALClientContextState::GetOrCreate(context).GetPrefix() + file_name;
auto prefixed_file_name = GDALClientContextState::GetOrCreate(context).GetPrefix(file_name);

GDALDatasetUniquePtr dataset;
try {
Expand Down
2 changes: 1 addition & 1 deletion spatial/src/spatial/gdal/functions/st_write_raster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static void Sink(ExecutionContext &context, FunctionData &bdata, GlobalFunctionD

auto raw_file_name = bind_data.file_path;
auto &client_ctx = GDALClientContextState::GetOrCreate(context.client);
auto prefixed_file_name = client_ctx.GetPrefix() + raw_file_name;
auto prefixed_file_name = client_ctx.GetPrefix(raw_file_name);
auto driver_name = bind_data.driver_name;
auto creation_options = bind_data.creation_options;

Expand Down
24 changes: 15 additions & 9 deletions spatial/src/spatial/gdal/raster/raster.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "duckdb/common/types/uuid.hpp"
#include "spatial/core/types.hpp"
#include "spatial/core/geometry/wkb_writer.hpp"
#include "spatial/gdal/types.hpp"
#include "spatial/gdal/raster/raster.hpp"

Expand Down Expand Up @@ -81,16 +82,16 @@ static VertexXY rasterToWorldVertex(double matrix[], int32_t col, int32_t row) {
return VertexXY {xgeo, ygeo};
}

Polygon Raster::GetGeometry(GeometryFactory &factory) const {
Polygon Raster::GetGeometry(ArenaAllocator &allocator) const {
auto cols = dataset_->GetRasterXSize();
auto rows = dataset_->GetRasterYSize();

double gt[6] = {0};
GetGeoTransform(gt);

Polygon polygon(factory.allocator, 1, false, false);
Polygon polygon(allocator, 1, false, false);
auto &ring = polygon[0];
ring.Resize(factory.allocator, 5); // 4 vertices + 1 for closing the polygon
ring.Resize(allocator, 5); // 4 vertices + 1 for closing the polygon
ring.Set(0, rasterToWorldVertex(gt, 0, 0));
ring.Set(1, rasterToWorldVertex(gt, cols, 0));
ring.Set(2, rasterToWorldVertex(gt, cols, rows));
Expand Down Expand Up @@ -268,7 +269,7 @@ class CutlineTransformer : public OGRCoordinateTransformation
};

GDALDataset *Raster::Clip(GDALDataset *dataset,
const Geometry &geometry,
const geometry_t &geometry,
const std::vector<std::string> &options) {

GDALDatasetH hDataset = GDALDataset::ToHandle(dataset);
Expand All @@ -287,7 +288,9 @@ GDALDataset *Raster::Clip(GDALDataset *dataset,
}

// Add Bounds & Geometry in pixel/line coordinates to the options.
if (!geometry.IsEmpty()) {
if (geometry.GetType() == GeometryType::POLYGON ||
geometry.GetType() == GeometryType::MULTIPOLYGON) {

OGRGeometryUniquePtr ogr_geom;

OGRSpatialReference srs;
Expand All @@ -297,10 +300,13 @@ GDALDataset *Raster::Clip(GDALDataset *dataset,
srs.importFromWkt(&proj_ref, nullptr);
}

vector<data_t> buffer;
WKBWriter::Write(geometry, buffer);

OGRGeometry *ptr_geom = nullptr;
std::string wkt_geom = geometry.ToString();
const char *cpszWkt = wkt_geom.c_str();
if (OGRGeometryFactory::createFromWkt(&cpszWkt, &srs, &ptr_geom) != CE_None) {
if (OGRGeometryFactory::createFromWkb(
buffer.data(), &srs, &ptr_geom, buffer.size(), wkbVariantIso) != OGRERR_NONE) {

CSLDestroy(papszArgv);
throw InvalidInputException("Input Geometry could not imported");
} else {
Expand All @@ -324,7 +330,7 @@ GDALDataset *Raster::Clip(GDALDataset *dataset,
CPLFree(pszWkt);
throw InvalidInputException("Input Geometry could not loaded");
}
wkt_geom = pszWkt;
std::string wkt_geom = pszWkt;
CPLFree(pszWkt);

std::string wkt_option = "CUTLINE=" + wkt_geom;
Expand Down

0 comments on commit fef62bf

Please sign in to comment.