Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ Again, please feel free to open an issue if there is a particular function you w
| --------------------------- | -------- | ---------- | ------------- | ---------- | --------------- |
| ST_Point | 🦆 | 🦆 | | | |
| ST_Area | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsHEXWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsText | 🧭 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_AsWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Boundary | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
Expand Down
24 changes: 19 additions & 5 deletions spatial/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ set(CMAKE_CXX_STANDARD 11)

project(${TARGET_NAME})

# Options

# Enable network functionality (OpenSSL and GDAL's CURL based fs/drivers)
option(SPATIAL_USE_NETWORK "Enable network functionality" ON)

include_directories(include)
add_subdirectory(src)

Expand All @@ -26,6 +31,7 @@ execute_process(
-G ${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
-DOSX_BUILD_UNIVERSAL=${OSX_BUILD_UNIVERSAL}
-DSPATIAL_USE_NETWORK=${SPATIAL_USE_NETWORK}
-S ${CMAKE_CURRENT_SOURCE_DIR}/dependencies
-B ${CMAKE_BINARY_DIR}/dependencies
RESULT_VARIABLE DEPENDENCIES_GENERATE_RESULT
Expand Down Expand Up @@ -54,9 +60,7 @@ find_package(PROJ REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(GEOS REQUIRED)
find_package(GDAL REQUIRED)
find_package(CURL REQUIRED)
find_package(EXPAT REQUIRED)
find_package(OpenSSL REQUIRED)

# Important: The link order matters, its the reverse order of dependency
target_link_libraries(
Expand All @@ -65,14 +69,24 @@ target_link_libraries(
GDAL::GDAL
GEOS::geos_c
PROJ::proj
CURL::libcurl
EXPAT::EXPAT
OpenSSL::SSL
OpenSSL::Crypto
SQLite::SQLite3
ZLIB::ZLIB
${SQLITE3_MEMVFS})

if(SPATIAL_USE_NETWORK)
find_package(CURL REQUIRED)
find_package(OpenSSL REQUIRED)

target_link_libraries(
${EXTENSION_NAME}
PUBLIC
CURL::libcurl
OpenSSL::SSL
OpenSSL::Crypto)
endif()


if(WIN32)
target_link_libraries(${EXTENSION_NAME} PUBLIC wbemuuid.lib)
endif()
Expand Down
66 changes: 58 additions & 8 deletions spatial/dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
cmake_minimum_required(VERSION 3.20)



project(dependecies-build)

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

if(APPLE AND ${OSX_BUILD_UNIVERSAL})
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64")

endif()

# Escape semicolons in CMAKE_OSX_ARCHITECTURES before passing to ExternalProject_Add
Expand Down Expand Up @@ -48,6 +45,7 @@ ExternalProject_Add(
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
)

if(SPATIAL_USE_NETWORK)
# CURL
ExternalProject_Add(
CURL
Expand All @@ -69,6 +67,7 @@ ExternalProject_Add(
-DCURL_USE_LIBSSH=OFF
-DOPENSSL_USE_STATIC_LIBS=ON # Propagate to FindOpenSSL.cmake
)
endif()

find_program(EXE_SQLITE3 sqlite3)
# PROJ
Expand Down Expand Up @@ -167,22 +166,73 @@ ExternalProject_Add(
-DGDAL_USE_GEOS=ON
-DGDAL_USE_SQLITE3=ON
-DGDAL_USE_EXPAT=ON
-DGDAL_USE_CURL=ON
-DGDAL_USE_OPENSSL=ON
-DGDAL_USE_CURL=${SPATIAL_USE_NETWORK}
-DGDAL_USE_OPENSSL=${SPATIAL_USE_NETWORK}
-DOPENSSL_USE_STATIC_LIBS=ON # Propagate to FindOpenSSL.cmake

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

# remove optional gdal drivers
# Remove optional gdal drivers
-DGDAL_BUILD_OPTIONAL_DRIVERS=OFF
-DOGR_BUILD_OPTIONAL_DRIVERS=ON
-DOGR_BUILD_OPTIONAL_DRIVERS=OFF

# Build these explicitly
-DOGR_ENABLE_DRIVER_MEM=ON
-DOGR_ENABLE_DRIVER_GEOJSON=ON
-DOGR_ENABLE_DRIVER_GML=ON
-DOGR_ENABLE_DRIVER_TAB=ON
-DOGR_ENABLE_DRIVER_SHAPE=ON
-DOGR_ENABLE_DRIVER_KML=ON
-DOGR_ENABLE_DRIVER_VRT=ON
-DOGR_ENABLE_DRIVER_AVC=ON
-DOGR_ENABLE_DRIVER_NTF=ON
-DOGR_ENABLE_DRIVER_LVBAG=ON
-DOGR_ENABLE_DRIVER_S57=ON
-DOGR_ENABLE_DRIVER_CSV=ON
-DOGR_ENABLE_DRIVER_DGN=ON
-DOGR_ENABLE_DRIVER_GMT=ON
-DOGR_ENABLE_DRIVER_TIGER=ON
-DOGR_ENABLE_DRIVER_GEOCONCEPT=ON
-DOGR_ENABLE_DRIVER_GEORSS=ON
-DOGR_ENABLE_DRIVER_DXF=ON
-DOGR_ENABLE_DRIVER_PGDUMP=ON
-DOGR_ENABLE_DRIVER_GPSBABEL=ON
-DOGR_ENABLE_DRIVER_EDIGEO=ON
-DOGR_ENABLE_DRIVER_SXF=ON
-DOGR_ENABLE_DRIVER_OPENFILEGDB=ON
-DOGR_ENABLE_DRIVER_WASP=ON
-DOGR_ENABLE_DRIVER_SELAFIN=ON
-DOGR_ENABLE_DRIVER_JML=ON
-DOGR_ENABLE_DRIVER_VDV=ON
-DOGR_ENABLE_DRIVER_FLATGEOBUF=ON
-DOGR_ENABLE_DRIVER_MAPML=ON
-DOGR_ENABLE_DRIVER_GPX=ON
-DOGR_ENABLE_DRIVER_SVG=ON
-DOGR_ENABLE_DRIVER_SQLITE=ON
-DOGR_ENABLE_DRIVER_GPKG=ON
-DOGR_ENABLE_DRIVER_OSM=ON
-DOGR_ENABLE_DRIVER_XLSX=ON
-DOGR_ENABLE_DRIVER_CAD=ON
-DOGR_ENABLE_DRIVER_ODS=ON
-DOGR_ENABLE_DRIVER_LVBAG=ON
-DOGR_ENABLE_DRIVER_VFK=ON
-DOGR_ENABLE_DRIVER_MVT=ON

# Drivers requiring network/curl
-DOGR_ENABLE_DRIVER_AMIGOCLOUD=${SPATIAL_USE_NETWORK}
-DOGR_ENABLE_DRIVER_CARTO=${SPATIAL_USE_NETWORK}
-DOGR_ENABLE_DRIVER_WFS=${SPATIAL_USE_NETWORK}
-DOGR_ENABLE_DRIVER_NGW=${SPATIAL_USE_NETWORK}
-DOGR_ENABLE_DRIVER_ELASTIC=${SPATIAL_USE_NETWORK}
-DOGR_ENABLE_DRIVER_CSW=${SPATIAL_USE_NETWORK}
-DOGR_ENABLE_DRIVER_PLSCENES=${SPATIAL_USE_NETWORK}

# Remove bindings
-DBUILD_PYTHON_BINDINGS=OFF
)


# Ouch! Remember that the order of these libraries is important! (reverse order of dependencies)
#target_link_libraries(dependencies INTERFACE gdal geos_c geos proj expat memvfs sqlite3 zlib)
#target_link_libraries(dependencies INTERFACE gdal geos_c geos proj expat memvfs sqlite3 zlib)
2 changes: 2 additions & 0 deletions spatial/include/spatial/core/functions/cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ struct CoreCastFunctions {
RegisterVarcharCasts(context);
RegisterDimensionalCasts(context);
RegisterGeometryCasts(context);
RegisterWKBCasts(context);
}
private:
static void RegisterVarcharCasts(ClientContext &context);
static void RegisterDimensionalCasts(ClientContext &context);
static void RegisterGeometryCasts(ClientContext &context);
static void RegisterWKBCasts(ClientContext &context);
};

} // namespace core
Expand Down
4 changes: 4 additions & 0 deletions spatial/include/spatial/core/functions/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct CoreScalarFunctions {
RegisterStArea(context);
RegisterStAsText(context);
RegisterStAsWKB(context);
RegisterStAsHEXWKB(context);
RegisterStCentroid(context);
RegisterStCollect(context);
RegisterStContains(context);
Expand All @@ -34,6 +35,9 @@ struct CoreScalarFunctions {
// ST_AsText
static void RegisterStAsText(ClientContext &context);

// ST_AsHextWKB
static void RegisterStAsHEXWKB(ClientContext &context);

// ST_AsWKB
static void RegisterStAsWKB(ClientContext &context);

Expand Down
2 changes: 1 addition & 1 deletion spatial/include/spatial/geos/geos_wrappers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ struct WKTReader {
auto str = wkt.GetString();
auto geom = GEOSWKTReader_read_r(ctx, reader, str.c_str());
if (!geom) {
throw InvalidInputException("Could not read WKT");
return GeometryPtr(ctx, nullptr);
}
return GeometryPtr(ctx, geom);
}
Expand Down
1 change: 1 addition & 0 deletions spatial/src/spatial/core/functions/cast/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ set(EXTENSION_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/dimensional_cast.cpp
${CMAKE_CURRENT_SOURCE_DIR}/geometry_cast.cpp
${CMAKE_CURRENT_SOURCE_DIR}/varchar_cast.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wkb_cast.cpp
PARENT_SCOPE
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ static bool ToPoint2DCast(Vector &source, Vector &result, idx_t count, CastParam
// Register functions
//------------------------------------------------------------------------------
void CoreCastFunctions::RegisterDimensionalCasts(ClientContext &context) {
auto &catalog = Catalog::GetSystemCatalog(context);
auto &config = DBConfig::GetConfig(context);
auto &casts = config.GetCastFunctions();

Expand Down
5 changes: 2 additions & 3 deletions spatial/src/spatial/core/functions/cast/geometry_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#include "spatial/core/geometry/geometry.hpp"
#include "spatial/core/geometry/geometry_factory.hpp"
#include "spatial/core/functions/common.hpp"
#include "spatial/core/geometry/wkb_writer.hpp"

#include "duckdb/function/cast/cast_function_set.hpp"
#include "duckdb/common/vector_operations/generic_executor.hpp"


namespace spatial {

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

auto poly_entries = ListVector::GetData(result);
auto &ring_vec = ListVector::GetEntry(result);

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


casts.RegisterCastFunction(GeoTypes::BOX_2D(), GeoTypes::GEOMETRY(),
BoundCastInfo(Box2DToGeometryCast, nullptr, GeometryFunctionLocalState::InitCast), 1);
}
Expand Down
66 changes: 66 additions & 0 deletions spatial/src/spatial/core/functions/cast/wkb_cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "spatial/common.hpp"
#include "spatial/core/types.hpp"
#include "spatial/core/functions/cast.hpp"
#include "spatial/core/geometry/geometry.hpp"
#include "spatial/core/geometry/geometry_factory.hpp"
#include "spatial/core/functions/common.hpp"
#include "spatial/core/geometry/wkb_writer.hpp"

#include "duckdb/function/cast/cast_function_set.hpp"
#include "duckdb/common/vector_operations/generic_executor.hpp"

namespace spatial {

namespace core {

//------------------------------------------------------------------------------
// WKB -> GEOMETRY
//------------------------------------------------------------------------------
static bool WKBToGeometryCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {
auto &lstate = GeometryFunctionLocalState::ResetAndGet(parameters);

UnaryExecutor::Execute<string_t, string_t>(source, result, count, [&](string_t input) {
auto geometry = lstate.factory.FromWKB(input.GetDataUnsafe(), input.GetSize());
return lstate.factory.Serialize(result, geometry);
});
return true;
}

//------------------------------------------------------------------------------
// GEOMETRY -> WKB
//------------------------------------------------------------------------------
static bool GeometryToWKBCast(Vector &source, Vector &result, idx_t count, CastParameters &parameters) {

auto &lstate = GeometryFunctionLocalState::ResetAndGet(parameters);

UnaryExecutor::Execute<string_t, string_t>(source, result, count, [&](string_t input) {
auto geometry = lstate.factory.Deserialize(input);
auto size = WKBWriter::GetRequiredSize(geometry);
auto str = StringVector::EmptyString(result, size);
auto ptr = (data_ptr_t)(str.GetDataUnsafe());
WKBWriter::Write(geometry, ptr);
return str;
});

return true;
}

//------------------------------------------------------------------------------
// Register functions
//------------------------------------------------------------------------------
void CoreCastFunctions::RegisterWKBCasts(ClientContext &context) {
auto &config = DBConfig::GetConfig(context);
auto &casts = config.GetCastFunctions();

// Geometry <-> WKB is explicitly castable
casts.RegisterCastFunction(GeoTypes::GEOMETRY(), GeoTypes::WKB_BLOB(), BoundCastInfo(GeometryToWKBCast, nullptr, GeometryFunctionLocalState::InitCast));

casts.RegisterCastFunction(GeoTypes::WKB_BLOB(), GeoTypes::GEOMETRY(), BoundCastInfo(WKBToGeometryCast, nullptr, GeometryFunctionLocalState::InitCast));

// WKB -> BLOB is implicitly castable
casts.RegisterCastFunction(GeoTypes::WKB_BLOB(), LogicalType::BLOB, DefaultCasts::ReinterpretCast, 1);
}

} // namespace core

} // namespace spatial
1 change: 1 addition & 0 deletions spatial/src/spatial/core/functions/scalar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(EXTENSION_SOURCES
${EXTENSION_SOURCES}
${CMAKE_CURRENT_SOURCE_DIR}/st_area.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_ashexwkb.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_astext.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_aswkb.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_centroid.cpp
Expand Down
Loading