Skip to content

Commit a3897e7

Browse files
authored
Merge pull request #55 from Maxxen/dev
2 parents 211eb65 + 3f15812 commit a3897e7

File tree

17 files changed

+312
-48
lines changed

17 files changed

+312
-48
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ Again, please feel free to open an issue if there is a particular function you w
336336
| --------------------------- | -------- | ---------- | ------------- | ---------- | --------------- |
337337
| ST_Point | 🦆 | 🦆 | | | |
338338
| ST_Area | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
339+
| ST_AsHEXWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
339340
| ST_AsText | 🧭 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
340341
| ST_AsWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
341342
| ST_Boundary | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |

spatial/CMakeLists.txt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ set(CMAKE_CXX_STANDARD 11)
88

99
project(${TARGET_NAME})
1010

11+
# Options
12+
13+
# Enable network functionality (OpenSSL and GDAL's CURL based fs/drivers)
14+
option(SPATIAL_USE_NETWORK "Enable network functionality" ON)
15+
1116
include_directories(include)
1217
add_subdirectory(src)
1318

@@ -26,6 +31,7 @@ execute_process(
2631
-G ${CMAKE_GENERATOR}
2732
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
2833
-DOSX_BUILD_UNIVERSAL=${OSX_BUILD_UNIVERSAL}
34+
-DSPATIAL_USE_NETWORK=${SPATIAL_USE_NETWORK}
2935
-S ${CMAKE_CURRENT_SOURCE_DIR}/dependencies
3036
-B ${CMAKE_BINARY_DIR}/dependencies
3137
RESULT_VARIABLE DEPENDENCIES_GENERATE_RESULT
@@ -54,9 +60,7 @@ find_package(PROJ REQUIRED)
5460
find_package(SQLite3 REQUIRED)
5561
find_package(GEOS REQUIRED)
5662
find_package(GDAL REQUIRED)
57-
find_package(CURL REQUIRED)
5863
find_package(EXPAT REQUIRED)
59-
find_package(OpenSSL REQUIRED)
6064

6165
# Important: The link order matters, its the reverse order of dependency
6266
target_link_libraries(
@@ -65,14 +69,24 @@ target_link_libraries(
6569
GDAL::GDAL
6670
GEOS::geos_c
6771
PROJ::proj
68-
CURL::libcurl
6972
EXPAT::EXPAT
70-
OpenSSL::SSL
71-
OpenSSL::Crypto
7273
SQLite::SQLite3
7374
ZLIB::ZLIB
7475
${SQLITE3_MEMVFS})
7576

77+
if(SPATIAL_USE_NETWORK)
78+
find_package(CURL REQUIRED)
79+
find_package(OpenSSL REQUIRED)
80+
81+
target_link_libraries(
82+
${EXTENSION_NAME}
83+
PUBLIC
84+
CURL::libcurl
85+
OpenSSL::SSL
86+
OpenSSL::Crypto)
87+
endif()
88+
89+
7690
if(WIN32)
7791
target_link_libraries(${EXTENSION_NAME} PUBLIC wbemuuid.lib)
7892
endif()

spatial/dependencies/CMakeLists.txt

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
cmake_minimum_required(VERSION 3.20)
22

3-
4-
53
project(dependecies-build)
64

75
# Install and build dependencies locally
@@ -15,7 +13,6 @@ endif()
1513

1614
if(APPLE AND ${OSX_BUILD_UNIVERSAL})
1715
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")
18-
1916
endif()
2017

2118
# Escape semicolons in CMAKE_OSX_ARCHITECTURES before passing to ExternalProject_Add
@@ -48,6 +45,7 @@ ExternalProject_Add(
4845
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
4946
)
5047

48+
if(SPATIAL_USE_NETWORK)
5149
# CURL
5250
ExternalProject_Add(
5351
CURL
@@ -69,6 +67,7 @@ ExternalProject_Add(
6967
-DCURL_USE_LIBSSH=OFF
7068
-DOPENSSL_USE_STATIC_LIBS=ON # Propagate to FindOpenSSL.cmake
7169
)
70+
endif()
7271

7372
find_program(EXE_SQLITE3 sqlite3)
7473
# PROJ
@@ -167,22 +166,73 @@ ExternalProject_Add(
167166
-DGDAL_USE_GEOS=ON
168167
-DGDAL_USE_SQLITE3=ON
169168
-DGDAL_USE_EXPAT=ON
170-
-DGDAL_USE_CURL=ON
171-
-DGDAL_USE_OPENSSL=ON
169+
-DGDAL_USE_CURL=${SPATIAL_USE_NETWORK}
170+
-DGDAL_USE_OPENSSL=${SPATIAL_USE_NETWORK}
172171
-DOPENSSL_USE_STATIC_LIBS=ON # Propagate to FindOpenSSL.cmake
173172

174173
# This is not true, but a bug in gdal's cmake files
175174
-DACCEPT_MISSING_SQLITE3_RTREE:BOOL=ON
176175
-DACCEPT_MISSING_SQLITE3_MUTEX_ALLOC:BOOL=ON
177176

178-
# remove optional gdal drivers
177+
# Remove optional gdal drivers
179178
-DGDAL_BUILD_OPTIONAL_DRIVERS=OFF
180-
-DOGR_BUILD_OPTIONAL_DRIVERS=ON
179+
-DOGR_BUILD_OPTIONAL_DRIVERS=OFF
180+
181+
# Build these explicitly
182+
-DOGR_ENABLE_DRIVER_MEM=ON
183+
-DOGR_ENABLE_DRIVER_GEOJSON=ON
184+
-DOGR_ENABLE_DRIVER_GML=ON
185+
-DOGR_ENABLE_DRIVER_TAB=ON
186+
-DOGR_ENABLE_DRIVER_SHAPE=ON
187+
-DOGR_ENABLE_DRIVER_KML=ON
188+
-DOGR_ENABLE_DRIVER_VRT=ON
189+
-DOGR_ENABLE_DRIVER_AVC=ON
190+
-DOGR_ENABLE_DRIVER_NTF=ON
191+
-DOGR_ENABLE_DRIVER_LVBAG=ON
192+
-DOGR_ENABLE_DRIVER_S57=ON
193+
-DOGR_ENABLE_DRIVER_CSV=ON
194+
-DOGR_ENABLE_DRIVER_DGN=ON
195+
-DOGR_ENABLE_DRIVER_GMT=ON
196+
-DOGR_ENABLE_DRIVER_TIGER=ON
197+
-DOGR_ENABLE_DRIVER_GEOCONCEPT=ON
198+
-DOGR_ENABLE_DRIVER_GEORSS=ON
199+
-DOGR_ENABLE_DRIVER_DXF=ON
200+
-DOGR_ENABLE_DRIVER_PGDUMP=ON
201+
-DOGR_ENABLE_DRIVER_GPSBABEL=ON
202+
-DOGR_ENABLE_DRIVER_EDIGEO=ON
203+
-DOGR_ENABLE_DRIVER_SXF=ON
204+
-DOGR_ENABLE_DRIVER_OPENFILEGDB=ON
205+
-DOGR_ENABLE_DRIVER_WASP=ON
206+
-DOGR_ENABLE_DRIVER_SELAFIN=ON
207+
-DOGR_ENABLE_DRIVER_JML=ON
208+
-DOGR_ENABLE_DRIVER_VDV=ON
209+
-DOGR_ENABLE_DRIVER_FLATGEOBUF=ON
210+
-DOGR_ENABLE_DRIVER_MAPML=ON
211+
-DOGR_ENABLE_DRIVER_GPX=ON
212+
-DOGR_ENABLE_DRIVER_SVG=ON
213+
-DOGR_ENABLE_DRIVER_SQLITE=ON
214+
-DOGR_ENABLE_DRIVER_GPKG=ON
215+
-DOGR_ENABLE_DRIVER_OSM=ON
216+
-DOGR_ENABLE_DRIVER_XLSX=ON
217+
-DOGR_ENABLE_DRIVER_CAD=ON
218+
-DOGR_ENABLE_DRIVER_ODS=ON
219+
-DOGR_ENABLE_DRIVER_LVBAG=ON
220+
-DOGR_ENABLE_DRIVER_VFK=ON
221+
-DOGR_ENABLE_DRIVER_MVT=ON
222+
223+
# Drivers requiring network/curl
224+
-DOGR_ENABLE_DRIVER_AMIGOCLOUD=${SPATIAL_USE_NETWORK}
225+
-DOGR_ENABLE_DRIVER_CARTO=${SPATIAL_USE_NETWORK}
226+
-DOGR_ENABLE_DRIVER_WFS=${SPATIAL_USE_NETWORK}
227+
-DOGR_ENABLE_DRIVER_NGW=${SPATIAL_USE_NETWORK}
228+
-DOGR_ENABLE_DRIVER_ELASTIC=${SPATIAL_USE_NETWORK}
229+
-DOGR_ENABLE_DRIVER_CSW=${SPATIAL_USE_NETWORK}
230+
-DOGR_ENABLE_DRIVER_PLSCENES=${SPATIAL_USE_NETWORK}
181231

182232
# Remove bindings
183233
-DBUILD_PYTHON_BINDINGS=OFF
184234
)
185235

186236

187237
# Ouch! Remember that the order of these libraries is important! (reverse order of dependencies)
188-
#target_link_libraries(dependencies INTERFACE gdal geos_c geos proj expat memvfs sqlite3 zlib)
238+
#target_link_libraries(dependencies INTERFACE gdal geos_c geos proj expat memvfs sqlite3 zlib)

spatial/include/spatial/core/functions/cast.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ struct CoreCastFunctions {
1111
RegisterVarcharCasts(context);
1212
RegisterDimensionalCasts(context);
1313
RegisterGeometryCasts(context);
14+
RegisterWKBCasts(context);
1415
}
1516
private:
1617
static void RegisterVarcharCasts(ClientContext &context);
1718
static void RegisterDimensionalCasts(ClientContext &context);
1819
static void RegisterGeometryCasts(ClientContext &context);
20+
static void RegisterWKBCasts(ClientContext &context);
1921
};
2022

2123
} // namespace core

spatial/include/spatial/core/functions/scalar.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct CoreScalarFunctions {
1212
RegisterStArea(context);
1313
RegisterStAsText(context);
1414
RegisterStAsWKB(context);
15+
RegisterStAsHEXWKB(context);
1516
RegisterStCentroid(context);
1617
RegisterStCollect(context);
1718
RegisterStContains(context);
@@ -34,6 +35,9 @@ struct CoreScalarFunctions {
3435
// ST_AsText
3536
static void RegisterStAsText(ClientContext &context);
3637

38+
// ST_AsHextWKB
39+
static void RegisterStAsHEXWKB(ClientContext &context);
40+
3741
// ST_AsWKB
3842
static void RegisterStAsWKB(ClientContext &context);
3943

spatial/include/spatial/geos/geos_wrappers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct WKTReader {
184184
auto str = wkt.GetString();
185185
auto geom = GEOSWKTReader_read_r(ctx, reader, str.c_str());
186186
if (!geom) {
187-
throw InvalidInputException("Could not read WKT");
187+
return GeometryPtr(ctx, nullptr);
188188
}
189189
return GeometryPtr(ctx, geom);
190190
}

spatial/src/spatial/core/functions/cast/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ set(EXTENSION_SOURCES
33
${CMAKE_CURRENT_SOURCE_DIR}/dimensional_cast.cpp
44
${CMAKE_CURRENT_SOURCE_DIR}/geometry_cast.cpp
55
${CMAKE_CURRENT_SOURCE_DIR}/varchar_cast.cpp
6+
${CMAKE_CURRENT_SOURCE_DIR}/wkb_cast.cpp
67
PARENT_SCOPE
78
)

spatial/src/spatial/core/functions/cast/dimensional_cast.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ static bool ToPoint2DCast(Vector &source, Vector &result, idx_t count, CastParam
3333
// Register functions
3434
//------------------------------------------------------------------------------
3535
void CoreCastFunctions::RegisterDimensionalCasts(ClientContext &context) {
36-
auto &catalog = Catalog::GetSystemCatalog(context);
3736
auto &config = DBConfig::GetConfig(context);
3837
auto &casts = config.GetCastFunctions();
3938

spatial/src/spatial/core/functions/cast/geometry_cast.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
#include "spatial/core/geometry/geometry.hpp"
55
#include "spatial/core/geometry/geometry_factory.hpp"
66
#include "spatial/core/functions/common.hpp"
7+
#include "spatial/core/geometry/wkb_writer.hpp"
8+
79
#include "duckdb/function/cast/cast_function_set.hpp"
810
#include "duckdb/common/vector_operations/generic_executor.hpp"
911

10-
1112
namespace spatial {
1213

1314
namespace core {
@@ -155,7 +156,6 @@ static bool Polygon2DToGeometryCast(Vector &source, Vector &result, idx_t count,
155156
static bool GeometryToPolygon2DCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
156157
auto &lstate = GeometryFunctionLocalState::ResetAndGet(parameters);
157158

158-
auto poly_entries = ListVector::GetData(result);
159159
auto &ring_vec = ListVector::GetEntry(result);
160160

161161
idx_t total_rings = 0;
@@ -263,7 +263,6 @@ void CoreCastFunctions::RegisterGeometryCasts(ClientContext &context) {
263263
casts.RegisterCastFunction(GeoTypes::POLYGON_2D(), GeoTypes::GEOMETRY(),
264264
BoundCastInfo(Polygon2DToGeometryCast, nullptr, GeometryFunctionLocalState::InitCast), 1);
265265

266-
267266
casts.RegisterCastFunction(GeoTypes::BOX_2D(), GeoTypes::GEOMETRY(),
268267
BoundCastInfo(Box2DToGeometryCast, nullptr, GeometryFunctionLocalState::InitCast), 1);
269268
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "spatial/common.hpp"
2+
#include "spatial/core/types.hpp"
3+
#include "spatial/core/functions/cast.hpp"
4+
#include "spatial/core/geometry/geometry.hpp"
5+
#include "spatial/core/geometry/geometry_factory.hpp"
6+
#include "spatial/core/functions/common.hpp"
7+
#include "spatial/core/geometry/wkb_writer.hpp"
8+
9+
#include "duckdb/function/cast/cast_function_set.hpp"
10+
#include "duckdb/common/vector_operations/generic_executor.hpp"
11+
12+
namespace spatial {
13+
14+
namespace core {
15+
16+
//------------------------------------------------------------------------------
17+
// WKB -> GEOMETRY
18+
//------------------------------------------------------------------------------
19+
static bool WKBToGeometryCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
20+
auto &lstate = GeometryFunctionLocalState::ResetAndGet(parameters);
21+
22+
UnaryExecutor::Execute<string_t, string_t>(source, result, count, [&](string_t input) {
23+
auto geometry = lstate.factory.FromWKB(input.GetDataUnsafe(), input.GetSize());
24+
return lstate.factory.Serialize(result, geometry);
25+
});
26+
return true;
27+
}
28+
29+
//------------------------------------------------------------------------------
30+
// GEOMETRY -> WKB
31+
//------------------------------------------------------------------------------
32+
static bool GeometryToWKBCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
33+
34+
auto &lstate = GeometryFunctionLocalState::ResetAndGet(parameters);
35+
36+
UnaryExecutor::Execute<string_t, string_t>(source, result, count, [&](string_t input) {
37+
auto geometry = lstate.factory.Deserialize(input);
38+
auto size = WKBWriter::GetRequiredSize(geometry);
39+
auto str = StringVector::EmptyString(result, size);
40+
auto ptr = (data_ptr_t)(str.GetDataUnsafe());
41+
WKBWriter::Write(geometry, ptr);
42+
return str;
43+
});
44+
45+
return true;
46+
}
47+
48+
//------------------------------------------------------------------------------
49+
// Register functions
50+
//------------------------------------------------------------------------------
51+
void CoreCastFunctions::RegisterWKBCasts(ClientContext &context) {
52+
auto &config = DBConfig::GetConfig(context);
53+
auto &casts = config.GetCastFunctions();
54+
55+
// Geometry <-> WKB is explicitly castable
56+
casts.RegisterCastFunction(GeoTypes::GEOMETRY(), GeoTypes::WKB_BLOB(), BoundCastInfo(GeometryToWKBCast, nullptr, GeometryFunctionLocalState::InitCast));
57+
58+
casts.RegisterCastFunction(GeoTypes::WKB_BLOB(), GeoTypes::GEOMETRY(), BoundCastInfo(WKBToGeometryCast, nullptr, GeometryFunctionLocalState::InitCast));
59+
60+
// WKB -> BLOB is implicitly castable
61+
casts.RegisterCastFunction(GeoTypes::WKB_BLOB(), LogicalType::BLOB, DefaultCasts::ReinterpretCast, 1);
62+
}
63+
64+
} // namespace core
65+
66+
} // namespace spatial

0 commit comments

Comments
 (0)