Skip to content

Commit fef62bf

Browse files
committed
Changes for v0.10.1
1 parent 3ebb335 commit fef62bf

File tree

10 files changed

+36
-31
lines changed

10 files changed

+36
-31
lines changed

spatial/include/spatial/gdal/raster/raster.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22
#include "spatial/common.hpp"
3+
#include "spatial/core/types.hpp"
34
#include "spatial/core/geometry/geometry.hpp"
4-
#include "spatial/core/geometry/geometry_factory.hpp"
5+
#include "spatial/core/geometry/geometry_type.hpp"
56
#include "spatial/gdal/types.hpp"
67

78
class GDALDataset;
@@ -47,7 +48,7 @@ class Raster {
4748
bool GetInvGeoTransform(double *inv_matrix) const;
4849

4950
//! Returns the polygon representation of the extent of the raster
50-
Polygon GetGeometry(GeometryFactory &factory) const;
51+
Polygon GetGeometry(ArenaAllocator &allocator) const;
5152

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

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

8283
//! Get the last error message.

spatial/src/spatial/gdal/functions/cast.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "spatial/core/types.hpp"
44
#include "spatial/core/functions/common.hpp"
55
#include "spatial/core/geometry/geometry.hpp"
6-
#include "spatial/core/geometry/vertex_vector.hpp"
76
#include "spatial/gdal/functions/cast.hpp"
87
#include "spatial/gdal/raster/raster.hpp"
98

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

3635
UnaryExecutor::Execute<uintptr_t, geometry_t>(source, result, count, [&](uintptr_t &input) {
3736
Raster raster(reinterpret_cast<GDALDataset *>(input));
38-
return lstate.factory.Serialize(result, raster.GetGeometry(lstate.factory), false, false);
37+
return Geometry(raster.GetGeometry(lstate.arena)).Serialize(result);
3938
});
4039
return true;
4140
}

spatial/src/spatial/gdal/functions/scalar/st_raster_as_file.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static void RasterAsFileFunction_01(DataChunk &args, ExpressionState &state, Vec
3232

3333
auto raw_file_name = file_name.GetString();
3434
auto &client_ctx = GDALClientContextState::GetOrCreate(context);
35-
auto prefixed_file_name = client_ctx.GetPrefix() + raw_file_name;
35+
auto prefixed_file_name = client_ctx.GetPrefix(raw_file_name);
3636

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

7676
auto raw_file_name = file_name.GetString();
7777
auto &client_ctx = GDALClientContextState::GetOrCreate(context);
78-
auto prefixed_file_name = client_ctx.GetPrefix() + raw_file_name;
78+
auto prefixed_file_name = client_ctx.GetPrefix(raw_file_name);
7979

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

spatial/src/spatial/gdal/functions/scalar/st_raster_clip.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ namespace gdal {
2121

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

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

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

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

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

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

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

114112
ScalarFunctionSet set("ST_RasterClip");
115-
set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY()}, GeoTypes::RASTER(), RasterClipFunction_01,
116-
nullptr, nullptr, nullptr,
117-
GeometryFunctionLocalState::Init));
113+
set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY()},
114+
GeoTypes::RASTER(),
115+
RasterClipFunction_01));
118116

119-
set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY(), LogicalType::LIST(LogicalType::VARCHAR)}, GeoTypes::RASTER(), RasterClipFunction_02,
120-
nullptr, nullptr, nullptr,
121-
GeometryFunctionLocalState::Init));
117+
set.AddFunction(ScalarFunction({GeoTypes::RASTER(), GeoTypes::GEOMETRY(), LogicalType::LIST(LogicalType::VARCHAR)},
118+
GeoTypes::RASTER(),
119+
RasterClipFunction_02));
122120

123121
ExtensionUtil::RegisterFunction(db, set);
124122
}

spatial/src/spatial/gdal/functions/scalar/st_raster_from_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static void RasterFromFileFunction(DataChunk &args, ExpressionState &state, Vect
2525
auto &ctx_state = GDALClientContextState::GetOrCreate(context);
2626

2727
auto raw_file_name = input.GetString();
28-
auto prefixed_file_name = ctx_state.GetPrefix() + raw_file_name;
28+
auto prefixed_file_name = ctx_state.GetPrefix(raw_file_name);
2929

3030
GDALDataset *dataset = RasterFactory::FromFile(prefixed_file_name);
3131
if (dataset == nullptr) {

spatial/src/spatial/gdal/functions/scalar/st_raster_geometry.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "spatial/common.hpp"
44
#include "spatial/core/types.hpp"
55
#include "spatial/core/functions/common.hpp"
6+
#include "spatial/core/geometry/geometry.hpp"
67
#include "spatial/gdal/functions/scalar.hpp"
78
#include "spatial/gdal/raster/raster.hpp"
89

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

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

spatial/src/spatial/gdal/functions/st_read_raster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void GdalRasterTableFunction::Execute(ClientContext &context, TableFunctionInput
8080
// Now we can open the dataset
8181
auto raw_file_name = bind_data.file_name;
8282
auto &ctx_state = GDALClientContextState::GetOrCreate(context);
83-
auto prefixed_file_name = ctx_state.GetPrefix() + raw_file_name;
83+
auto prefixed_file_name = ctx_state.GetPrefix(raw_file_name);
8484
auto dataset =
8585
GDALDataset::Open(prefixed_file_name.c_str(), GDAL_OF_RASTER | GDAL_OF_VERBOSE_ERROR,
8686
gdal_allowed_drivers.empty() ? nullptr : gdal_allowed_drivers.data(),

spatial/src/spatial/gdal/functions/st_read_raster_meta.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void Scan(ClientContext &context, TableFunctionInput &input, DataChunk &o
8989

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

9494
GDALDatasetUniquePtr dataset;
9595
try {

spatial/src/spatial/gdal/functions/st_write_raster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ static void Sink(ExecutionContext &context, FunctionData &bdata, GlobalFunctionD
145145

146146
auto raw_file_name = bind_data.file_path;
147147
auto &client_ctx = GDALClientContextState::GetOrCreate(context.client);
148-
auto prefixed_file_name = client_ctx.GetPrefix() + raw_file_name;
148+
auto prefixed_file_name = client_ctx.GetPrefix(raw_file_name);
149149
auto driver_name = bind_data.driver_name;
150150
auto creation_options = bind_data.creation_options;
151151

spatial/src/spatial/gdal/raster/raster.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "duckdb/common/types/uuid.hpp"
22
#include "spatial/core/types.hpp"
3+
#include "spatial/core/geometry/wkb_writer.hpp"
34
#include "spatial/gdal/types.hpp"
45
#include "spatial/gdal/raster/raster.hpp"
56

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

84-
Polygon Raster::GetGeometry(GeometryFactory &factory) const {
85+
Polygon Raster::GetGeometry(ArenaAllocator &allocator) const {
8586
auto cols = dataset_->GetRasterXSize();
8687
auto rows = dataset_->GetRasterYSize();
8788

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

91-
Polygon polygon(factory.allocator, 1, false, false);
92+
Polygon polygon(allocator, 1, false, false);
9293
auto &ring = polygon[0];
93-
ring.Resize(factory.allocator, 5); // 4 vertices + 1 for closing the polygon
94+
ring.Resize(allocator, 5); // 4 vertices + 1 for closing the polygon
9495
ring.Set(0, rasterToWorldVertex(gt, 0, 0));
9596
ring.Set(1, rasterToWorldVertex(gt, cols, 0));
9697
ring.Set(2, rasterToWorldVertex(gt, cols, rows));
@@ -268,7 +269,7 @@ class CutlineTransformer : public OGRCoordinateTransformation
268269
};
269270

270271
GDALDataset *Raster::Clip(GDALDataset *dataset,
271-
const Geometry &geometry,
272+
const geometry_t &geometry,
272273
const std::vector<std::string> &options) {
273274

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

289290
// Add Bounds & Geometry in pixel/line coordinates to the options.
290-
if (!geometry.IsEmpty()) {
291+
if (geometry.GetType() == GeometryType::POLYGON ||
292+
geometry.GetType() == GeometryType::MULTIPOLYGON) {
293+
291294
OGRGeometryUniquePtr ogr_geom;
292295

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

303+
vector<data_t> buffer;
304+
WKBWriter::Write(geometry, buffer);
305+
300306
OGRGeometry *ptr_geom = nullptr;
301-
std::string wkt_geom = geometry.ToString();
302-
const char *cpszWkt = wkt_geom.c_str();
303-
if (OGRGeometryFactory::createFromWkt(&cpszWkt, &srs, &ptr_geom) != CE_None) {
307+
if (OGRGeometryFactory::createFromWkb(
308+
buffer.data(), &srs, &ptr_geom, buffer.size(), wkbVariantIso) != OGRERR_NONE) {
309+
304310
CSLDestroy(papszArgv);
305311
throw InvalidInputException("Input Geometry could not imported");
306312
} else {
@@ -324,7 +330,7 @@ GDALDataset *Raster::Clip(GDALDataset *dataset,
324330
CPLFree(pszWkt);
325331
throw InvalidInputException("Input Geometry could not loaded");
326332
}
327-
wkt_geom = pszWkt;
333+
std::string wkt_geom = pszWkt;
328334
CPLFree(pszWkt);
329335

330336
std::string wkt_option = "CUTLINE=" + wkt_geom;

0 commit comments

Comments
 (0)