Skip to content

Commit

Permalink
Merge pull request #297 from and-pulc/add_nearestpoints
Browse files Browse the repository at this point in the history
Add NearestPoints scalar function.
  • Loading branch information
Maxxen authored Apr 3, 2024
2 parents 874a52a + 0d3d1c5 commit 3db68bc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
| [ST_ReducePrecision](#st_reduceprecision) | Returns the geometry with all vertices reduced to the target precision |
| [ST_RemoveRepeatedPoints](#st_removerepeatedpoints) | Returns a new geometry with repeated points removed, optionally within a target distance of eachother. |
| [ST_Reverse](#st_reverse) | Returns a new version of the input geometry with the order of its vertices reversed |
| [ST_ShortestLine](#st_shortestline) | Returns the line between the two closest points between geom1 and geom2 |
| [ST_Simplify](#st_simplify) | Simplifies the input geometry by collapsing edges smaller than 'distance' |
| [ST_SimplifyPreserveTopology](#st_simplifypreservetopology) | Returns a simplified geometry but avoids creating invalid topologies |
| [ST_StartPoint](#st_startpoint) | Returns the first point of a line geometry |
Expand Down Expand Up @@ -1677,6 +1678,22 @@ Returns a new version of the input geometry with the order of its vertices rever



### ST_ShortestLine

_Returns the line between the two closest points between geom1 and geom2_

#### Signature

```sql
GEOMETRY ST_ShortestLine (col0 GEOMETRY, col1 GEOMETRY)
```

#### Description

Returns the line between the two closest points between geom1 and geom2



### ST_Simplify

_Simplifies the input geometry by collapsing edges smaller than 'distance'_
Expand Down
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 @@ -37,6 +37,7 @@ struct GEOSScalarFunctions {
RegisterStReducePrecision(db);
RegisterStRemoveRepeatedPoints(db);
RegisterStReverse(db);
RegisterStShortestLine(db);
RegisterStSimplifyPreserveTopology(db);
RegisterStSimplify(db);
RegisterStTouches(db);
Expand Down Expand Up @@ -74,6 +75,7 @@ struct GEOSScalarFunctions {
static void RegisterStReverse(DatabaseInstance &db);
static void RegisterStLineMerge(DatabaseInstance &db);
static void RegisterStMakeValid(DatabaseInstance &db);
static void RegisterStShortestLine(DatabaseInstance &db);
static void RegisterStSimplifyPreserveTopology(DatabaseInstance &db);
static void RegisterStSimplify(DatabaseInstance &db);
static void RegisterStTouches(DatabaseInstance &db);
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 @@ -24,6 +24,7 @@ set(EXTENSION_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/st_linemerge.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_makevalid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_normalize.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_shortestline.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_overlaps.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_pointonsurface.cpp
${CMAKE_CURRENT_SOURCE_DIR}/st_reduceprecision.cpp
Expand Down
59 changes: 59 additions & 0 deletions spatial/src/spatial/geos/functions/scalar/st_shortestline.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#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/binary_executor.hpp"

namespace spatial {

namespace geos {

using namespace spatial::core;

static void ShortestLineFunction(DataChunk &args, ExpressionState &state, Vector &result) {
auto &lstate = GEOSFunctionLocalState::ResetAndGet(state);
auto &ctx = lstate.ctx.GetCtx();
BinaryExecutor::Execute<geometry_t, geometry_t, geometry_t>(
args.data[0], args.data[1], result, args.size(), [&](geometry_t left, geometry_t right) {
auto left_geom = lstate.ctx.Deserialize(left);
auto right_geom = lstate.ctx.Deserialize(right);

auto coord_seq = GEOSNearestPoints_r(ctx, left_geom.get(), right_geom.get());
auto result_geom = make_uniq_geos(ctx, GEOSGeom_createLineString_r(ctx, coord_seq));

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

//------------------------------------------------------------------------------
// Documentation
//------------------------------------------------------------------------------
static constexpr const char *DOC_DESCRIPTION = R"(
Returns the line between the two closest points between geom1 and geom2
)";

static constexpr const char *DOC_EXAMPLE = R"(
)";

static constexpr DocTag DOC_TAGS[] = {{"ext", "spatial"}, {"category", "construction"}};
//------------------------------------------------------------------------------
// Register Functions
//------------------------------------------------------------------------------
void GEOSScalarFunctions::RegisterStShortestLine(DatabaseInstance &db) {

ScalarFunctionSet set("ST_ShortestLine");

set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY(), GeoTypes::GEOMETRY()}, GeoTypes::GEOMETRY(),
ShortestLineFunction, nullptr, nullptr, nullptr, GEOSFunctionLocalState::Init));

ExtensionUtil::RegisterFunction(db, set);
DocUtil::AddDocumentation(db, "ST_ShortestLine", DOC_DESCRIPTION, DOC_EXAMPLE, DOC_TAGS);
}

} // namespace geos

} // namespace spatial

0 comments on commit 3db68bc

Please sign in to comment.