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 ST_LineMerge, Fix GCC compilation issues #174

Merged
merged 3 commits into from
Nov 4, 2023
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
106 changes: 54 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,55 +336,57 @@ We are actively working on implementing more functions, and will update this tab
Again, please feel free to open an issue if there is a particular function you would like to see implemented. Contributions are also welcome!


| Scalar functions | GEOMETRY | POINT_2D | LINESTRING_2D | POLYGON_2D | BOX_2D |
| --------------------------- | -------- | ---------- | ------------- | ---------- | --------------- |
| ST_Point | 🦆 | 🦆 | | | |
| ST_Area | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsGeoJSON | 🦆 | 🦆 | 🦆 | 🦆. | 🦆 |
| ST_AsHEXWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsText | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Boundary | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Buffer | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Centroid | 🧭 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Collect | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_CollectionExtract | 🦆 | | | | |
| ST_Contains | 🧭 | 🔄 | 🔄 | 🦆 or 🔄 | 🔄 (as POLYGON) |
| ST_ContainsProperly | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_ConvexHull | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_CoveredBy | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Covers | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Crosses | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Difference | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Dimension | 🦆 | | | | |
| ST_Disjoint | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Distance | 🧭 | 🦆 or 🔄 | 🦆 or 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_DWithin | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Envelope | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Equals | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_FlipCoordinates | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_GeomFromText | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_GeomFromWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_GeometryType | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_MakeLine | 🦆 | | 🦆 | | |
| ST_Perimeter | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Intersection | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Intersects | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsClosed | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsEmpty | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_IsRing | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsSimple | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsValid | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Length | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_Normalize | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_NumPoints/ST_NPoints | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Overlaps | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_PointOnSurface | 🧭 | 🧭 | 🧭 | 🧭 | 🔄 (as POLYGON) |
| ST_RemoveRepeatedPoints | 🧭 | | 🦆 | 🔄 | |
| ST_SimplifyPreserveTopology | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Simplify | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Touches | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Union | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Within | 🧭 | 🦆 or 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_X | 🧭 | 🦆 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Y | 🧭 | 🦆 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| Scalar functions | GEOMETRY | POINT_2D | LINESTRING_2D | POLYGON_2D | BOX_2D |
|-----------------------------|----------|----------|---------------|------------|-----------------|
| ST_Point | 🦆 | 🦆 | | | |
| ST_Area | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsGeoJSON | 🦆 | 🦆 | 🦆 | 🦆. | 🦆 |
| ST_AsHEXWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsText | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_AsWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Boundary | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Buffer | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Centroid | 🧭 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Collect | 🦆 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_CollectionExtract | 🦆 | | | | |
| ST_Contains | 🧭 | 🔄 | 🔄 | 🦆 or 🔄 | 🔄 (as POLYGON) |
| ST_ContainsProperly | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_ConvexHull | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_CoveredBy | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Covers | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Crosses | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Difference | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Dimension | 🦆 | | | | |
| ST_Disjoint | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Distance | 🧭 | 🦆 or 🔄 | 🦆 or 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Dump | 🦆 | | | | |
| ST_DWithin | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Envelope | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Equals | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_FlipCoordinates | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_GeomFromText | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_GeomFromWKB | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_GeometryType | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_MakeLine | 🦆 | | 🦆 | | |
| ST_Perimeter | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Intersection | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Intersects | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsClosed | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsEmpty | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_IsRing | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsSimple | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_IsValid | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Length | 🦆 | 🦆 | 🦆 | 🦆 | 🔄 (as POLYGON) |
| ST_LineMerge | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Normalize | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_NumPoints/ST_NPoints | 🦆 | 🦆 | 🦆 | 🦆 | 🦆 |
| ST_Overlaps | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_PointOnSurface | 🧭 | 🧭 | 🧭 | 🧭 | 🔄 (as POLYGON) |
| ST_RemoveRepeatedPoints | 🧭 | | 🦆 | 🔄 | |
| ST_SimplifyPreserveTopology | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Simplify | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Touches | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Union | 🧭 | 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Within | 🧭 | 🦆 or 🔄 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_X | 🧭 | 🦆 | 🔄 | 🔄 | 🔄 (as POLYGON) |
| ST_Y | 🧭 | 🦆 | 🔄 | 🔄 | 🔄 (as POLYGON) |
2 changes: 2 additions & 0 deletions spatial/include/spatial/geos/functions/scalar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct GEOSScalarFunctions {
RegisterStIsRing(db);
RegisterStIsSimple(db);
RegisterStIsValid(db);
RegisterStLineMerge(db);
RegisterStNormalize(db);
RegisterStOverlaps(db);
RegisterStPointOnSurface(db);
Expand Down Expand Up @@ -72,6 +73,7 @@ struct GEOSScalarFunctions {
static void RegisterStReducePrecision(DatabaseInstance &db);
static void RegisterStRemoveRepeatedPoints(DatabaseInstance &db);
static void RegisterStReverse(DatabaseInstance &db);
static void RegisterStLineMerge(DatabaseInstance &db);
static void RegisterStSimplifyPreserveTopology(DatabaseInstance &db);
static void RegisterStSimplify(DatabaseInstance &db);
static void RegisterStTouches(DatabaseInstance &db);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static void GeometryExteriorRingFunction(DataChunk &args, ExpressionState &state
auto num_points = shell.Count();

auto line = lstate.factory.CreateLineString(num_points);
for (auto i = 0; i < num_points; i++) {
for (uint32_t i = 0; i < num_points; i++) {
line.Vertices().Add(shell[i]);
}
return lstate.factory.Serialize(result, Geometry(line));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static void MakePolygonFromRingsFunction(DataChunk &args, ExpressionState &state

auto polygon = lstate.factory.CreatePolygon(rings_counts.size(), rings_counts.data());

for (auto ring_idx = 0; ring_idx < rings.size(); ring_idx++) {
for (idx_t ring_idx = 0; ring_idx < rings.size(); ring_idx++) {
auto &new_ring = rings[ring_idx];
auto &poly_ring = polygon.Ring(ring_idx);
for (auto &v : new_ring.Vertices()) {
Expand Down
2 changes: 1 addition & 1 deletion spatial/src/spatial/gdal/functions/st_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ void GdalTableFunction::Scan(ClientContext &context, TableFunctionInput &input,

if (!data.keep_wkb) {
// Find the geometry columns
for (auto col_idx = 0; col_idx < state.column_ids.size(); col_idx++) {
for (idx_t col_idx = 0; col_idx < state.column_ids.size(); col_idx++) {
auto mapped_idx = state.column_ids[col_idx];
if (data.geometry_column_ids.find(mapped_idx) != data.geometry_column_ids.end()) {
// Found a geometry column
Expand Down
1 change: 1 addition & 0 deletions spatial/src/spatial/geos/functions/scalar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(EXTENSION_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/st_is_ring.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_is_simple.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_is_valid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_linemerge.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_normalize.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_overlaps.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_pointonsurface.cpp
Expand Down
51 changes: 51 additions & 0 deletions spatial/src/spatial/geos/functions/scalar/st_linemerge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "spatial/common.hpp"
#include "spatial/core/types.hpp"
#include "spatial/geos/functions/scalar.hpp"
#include "spatial/geos/functions/common.hpp"
#include "spatial/geos/geos_wrappers.hpp"

#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
#include "duckdb/common/vector_operations/unary_executor.hpp"
#include "duckdb/common/vector_operations/binary_executor.hpp"

namespace spatial {

namespace geos {

using namespace spatial::core;

static void LineMergeFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &lstate = GEOSFunctionLocalState::ResetAndGet(state);
auto &ctx = lstate.ctx.GetCtx();
UnaryExecutor::Execute<string_t, string_t>(args.data[0], result, args.size(), [&](string_t &geometry_blob) {
auto geometry = lstate.ctx.Deserialize(geometry_blob);
auto convex_hull_geometry = make_uniq_geos(ctx, GEOSLineMerge_r(ctx, geometry.get()));
return lstate.ctx.Serialize(result, convex_hull_geometry);
});
}

static void LineMergeFunctionWithDirected(DataChunk &args, ExpressionState &state, Vector &result) {
auto &lstate = GEOSFunctionLocalState::ResetAndGet(state);
auto &ctx = lstate.ctx.GetCtx();
BinaryExecutor::Execute<string_t, bool, string_t>(args.data[0], args.data[1], result, args.size(), [&](string_t &geometry_blob, bool directed) {
auto geometry = lstate.ctx.Deserialize(geometry_blob);
auto convex_hull_geometry = directed ? make_uniq_geos(ctx, GEOSLineMergeDirected_r(ctx, geometry.get()))
: make_uniq_geos(ctx, GEOSLineMerge_r(ctx, geometry.get()));

return lstate.ctx.Serialize(result, convex_hull_geometry);
});
}

void GEOSScalarFunctions::RegisterStLineMerge(DatabaseInstance &db) {

ScalarFunctionSet set("ST_LineMerge");

set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY()}, GeoTypes::GEOMETRY(), LineMergeFunction, nullptr, nullptr, nullptr, GEOSFunctionLocalState::Init));
set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY(), LogicalType::BOOLEAN}, GeoTypes::GEOMETRY(), LineMergeFunctionWithDirected, nullptr, nullptr, nullptr, GEOSFunctionLocalState::Init));

ExtensionUtil::RegisterFunction(db, set);
}

} // namespace geos

} // namespace spatial
10 changes: 5 additions & 5 deletions spatial/src/spatial/geos/geos_wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ static void SerializePolygon(Cursor &writer, const GEOSGeometry *geom, const GEO
return;
}

auto num_holes = GEOSGetNumInteriorRings_r(ctx, geom);
uint32_t num_holes = GEOSGetNumInteriorRings_r(ctx, geom);
writer.Write<uint32_t>(num_holes + 1); // +1 for the shell

// Get shell
Expand Down Expand Up @@ -393,7 +393,7 @@ static void SerializePolygon(Cursor &writer, const GEOSGeometry *geom, const GEO

static void SerializeMultiPoint(Cursor &writer, const GEOSGeometry *geom, const GEOSContextHandle_t ctx) {
writer.Write<uint32_t>((uint32_t)GeometryType::MULTIPOINT);
auto num_points = GEOSGetNumGeometries_r(ctx, geom);
uint32_t num_points = GEOSGetNumGeometries_r(ctx, geom);
writer.Write<uint32_t>(num_points);
for (uint32_t i = 0; i < num_points; i++) {
auto point = GEOSGetGeometryN_r(ctx, geom, i);
Expand All @@ -403,7 +403,7 @@ static void SerializeMultiPoint(Cursor &writer, const GEOSGeometry *geom, const

static void SerializeMultiLineString(Cursor &writer, const GEOSGeometry *geom, const GEOSContextHandle_t ctx) {
writer.Write<uint32_t>((uint32_t)GeometryType::MULTILINESTRING);
auto num_linestrings = GEOSGetNumGeometries_r(ctx, geom);
uint32_t num_linestrings = GEOSGetNumGeometries_r(ctx, geom);
writer.Write<uint32_t>(num_linestrings);
for (uint32_t i = 0; i < num_linestrings; i++) {
auto linestring = GEOSGetGeometryN_r(ctx, geom, i);
Expand All @@ -413,7 +413,7 @@ static void SerializeMultiLineString(Cursor &writer, const GEOSGeometry *geom, c

static void SerializeMultiPolygon(Cursor &writer, const GEOSGeometry *geom, const GEOSContextHandle_t ctx) {
writer.Write<uint32_t>((uint32_t)GeometryType::MULTIPOLYGON);
auto num_polygons = GEOSGetNumGeometries_r(ctx, geom);
uint32_t num_polygons = GEOSGetNumGeometries_r(ctx, geom);
writer.Write<uint32_t>(num_polygons);
for (uint32_t i = 0; i < num_polygons; i++) {
auto polygon = GEOSGetGeometryN_r(ctx, geom, i);
Expand All @@ -423,7 +423,7 @@ static void SerializeMultiPolygon(Cursor &writer, const GEOSGeometry *geom, cons

static void SerializeGeometryCollection(Cursor &writer, const GEOSGeometry *geom, const GEOSContextHandle_t ctx) {
writer.Write<uint32_t>((uint32_t)GeometryType::GEOMETRYCOLLECTION);
auto num_geometries = GEOSGetNumGeometries_r(ctx, geom);
uint32_t num_geometries = GEOSGetNumGeometries_r(ctx, geom);
writer.Write<uint32_t>(num_geometries);
for (uint32_t i = 0; i < num_geometries; i++) {
auto geometry = GEOSGetGeometryN_r(ctx, geom, i);
Expand Down
Loading
Loading