Skip to content

Commit

Permalink
C API cluster functions: Return GEOSClusterInfo object
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaston committed Oct 2, 2024
1 parent 140130d commit 1a0d072
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 136 deletions.
57 changes: 42 additions & 15 deletions capi/geos_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <geos/io/GeoJSONReader.h>
#include <geos/io/GeoJSONWriter.h>
#include <geos/operation/buffer/BufferParameters.h>
#include <geos/operation/cluster/Clusters.h>
#include <geos/util/Interrupt.h>

#include <stdexcept>
Expand All @@ -40,6 +41,7 @@
// violations.
#define GEOSGeometry geos::geom::Geometry
#define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
#define GEOSClusterInfo geos::operation::cluster::Clusters
#define GEOSCoordSequence geos::geom::CoordinateSequence
#define GEOSBufferParams geos::operation::buffer::BufferParameters
#define GEOSSTRtree geos::index::strtree::TemplateSTRtree<void*>
Expand Down Expand Up @@ -343,34 +345,59 @@ extern "C" {
return GEOSNearestPoints_r(handle, g1, g2);
}

unsigned*
GEOSClusterDBSCAN(const GEOSGeometry* g, double eps, unsigned minPoints, unsigned* numClusters)
GEOSClusterInfo*
GEOSClusterDBSCAN(const GEOSGeometry* g, double eps, unsigned minPoints)
{
return GEOSClusterDBSCAN_r(handle, g, eps, minPoints, numClusters);
return GEOSClusterDBSCAN_r(handle, g, eps, minPoints);
}

unsigned*
GEOSClusterGeometryDistance(const GEOSGeometry* g, double d, unsigned* numClusters)
GEOSClusterInfo*
GEOSClusterGeometryDistance(const GEOSGeometry* g, double d)
{
return GEOSClusterGeometryDistance_r(handle, g, d, numClusters);
return GEOSClusterGeometryDistance_r(handle, g, d);
}

unsigned*
GEOSClusterGeometryIntersects(const GEOSGeometry* g, unsigned* numClusters)
GEOSClusterInfo*
GEOSClusterGeometryIntersects(const GEOSGeometry* g)
{
return GEOSClusterGeometryIntersects_r(handle, g, numClusters);
return GEOSClusterGeometryIntersects_r(handle, g);
}

unsigned*
GEOSClusterEnvelopeDistance(const GEOSGeometry* g, double d, unsigned* numClusters)
GEOSClusterInfo*
GEOSClusterEnvelopeDistance(const GEOSGeometry* g, double d)
{
return GEOSClusterEnvelopeDistance_r(handle, g, d, numClusters);
return GEOSClusterEnvelopeDistance_r(handle, g, d);
}

unsigned*
GEOSClusterEnvelopeIntersects(const GEOSGeometry* g, unsigned* numClusters)
GEOSClusterInfo*
GEOSClusterEnvelopeIntersects(const GEOSGeometry* g)
{
return GEOSClusterEnvelopeIntersects_r(handle, g, numClusters);
return GEOSClusterEnvelopeIntersects_r(handle, g);
}

std::size_t GEOSClusterInfo_getNumClusters(const GEOSClusterInfo* clusters)
{
return GEOSClusterInfo_getNumClusters_r(handle, clusters);
}

std::size_t GEOSClusterInfo_getClusterSize(const GEOSClusterInfo* clusters, size_t i)
{
return GEOSClusterInfo_getClusterSize_r(handle, clusters, i);
}

const std::size_t* GEOSClusterInfo_getInputsForClusterN(const GEOSClusterInfo* clusters, size_t i)
{
return GEOSClusterInfo_getInputsForClusterN_r(handle, clusters, i);
}

std::size_t* GEOSClusterInfo_getClustersForInputs(const GEOSClusterInfo* clusters)
{
return GEOSClusterInfo_getClustersForInputs_r(handle, clusters);
}

void GEOSClusterInfo_destroy(GEOSClusterInfo* info)
{
GEOSClusterInfo_destroy_r(handle, info);
}

Geometry*
Expand Down
129 changes: 97 additions & 32 deletions capi/geos_c.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ typedef struct GEOSBufParams_t GEOSBufferParams;
*/
typedef struct GEOSMakeValidParams_t GEOSMakeValidParams;

/**
* Object containing information about cluster of geometries.
* \see GEOSClusterInfo_destroy()
*/
typedef struct GEOSClusterInfo_t GEOSClusterInfo;

#endif

/** \cond */
Expand Down Expand Up @@ -1849,38 +1855,48 @@ extern GEOSGeometry GEOS_DLL *GEOSGeom_transformXYZ_r(
void* userdata);

/** \see GEOSClusterDBSCAN */
extern unsigned GEOS_DLL* GEOSClusterDBSCAN_r(
extern GEOSClusterInfo GEOS_DLL* GEOSClusterDBSCAN_r(
GEOSContextHandle_t handle,
const GEOSGeometry* g,
double eps,
unsigned minPoints,
unsigned* numClusters);
unsigned minPoints);

/** \see GEOSClusterGeometryDistance */
extern unsigned GEOS_DLL* GEOSClusterGeometryDistance_r(
extern GEOSClusterInfo GEOS_DLL* GEOSClusterGeometryDistance_r(
GEOSContextHandle_t handle,
const GEOSGeometry* g,
double d,
unsigned* numClusters);
double d);

/** \see GEOSClusterGeometryIntersects */
extern unsigned GEOS_DLL* GEOSClusterGeometryIntersects_r(
extern GEOSClusterInfo GEOS_DLL* GEOSClusterGeometryIntersects_r(
GEOSContextHandle_t handle,
const GEOSGeometry* g,
unsigned* numClusters);
const GEOSGeometry* g);

/** \see GEOSClusterEnvelopeDistance */
extern unsigned GEOS_DLL* GEOSClusterEnvelopeDistance_r(
extern GEOSClusterInfo GEOS_DLL* GEOSClusterEnvelopeDistance_r(
GEOSContextHandle_t handle,
const GEOSGeometry* g,
double d,
unsigned* numClusters);
double d);

/** \see GEOSClusterEnvelopeIntersects */
extern unsigned GEOS_DLL* GEOSClusterEnvelopeIntersects_r(
extern GEOSClusterInfo GEOS_DLL* GEOSClusterEnvelopeIntersects_r(
GEOSContextHandle_t handle,
const GEOSGeometry* g,
unsigned* numClusters);
const GEOSGeometry* g);

/** \see GEOSClusterInfo_getNumClusters */
extern size_t GEOS_DLL GEOSClusterInfo_getNumClusters_r(GEOSContextHandle_t, const GEOSClusterInfo* clusters);

/** \see GEOSClusterInfo_getClusterSize */
extern size_t GEOS_DLL GEOSClusterInfo_getClusterSize_r(GEOSContextHandle_t, const GEOSClusterInfo* clusters, size_t i);

/** \see GEOSClusterInfo_getClustersForInputs */
extern size_t GEOS_DLL* GEOSClusterInfo_getClustersForInputs_r(GEOSContextHandle_t, const GEOSClusterInfo* clusters);

/** \see GEOSClusterInfo_getInputsForClusterN */
extern const size_t GEOS_DLL* GEOSClusterInfo_getInputsForClusterN_r(GEOSContextHandle_t, const GEOSClusterInfo* clusters, size_t i);

/** \see GEOSClusterInfo_destroy */
extern void GEOS_DLL GEOSClusterInfo_destroy_r(GEOSContextHandle_t, GEOSClusterInfo* info);

/* ========= Algorithms ========= */

Expand Down Expand Up @@ -3800,7 +3816,10 @@ extern GEOSGeometry GEOS_DLL *GEOSSharedPaths(
* Functions for clustering geometries
*/

static const unsigned GEOS_CLUSTER_NONE = 4294967295;



static const size_t GEOS_CLUSTER_NONE = (size_t) -1;

///@{
/**
Expand All @@ -3811,11 +3830,9 @@ static const unsigned GEOS_CLUSTER_NONE = 4294967295;
* @param g a collection of geometries to be clustered
* @param eps distance parameter for clustering
* @param minPoints density parameter for clustering
* @param numClusters the number of clusters formed
* @return an array of cluster identifiers
* @return cluster information object
*/
extern unsigned GEOS_DLL* GEOSClusterDBSCAN(const GEOSGeometry* g, double eps, unsigned minPoints,
unsigned* numClusters);
extern GEOSClusterInfo GEOS_DLL* GEOSClusterDBSCAN(const GEOSGeometry* g, double eps, unsigned minPoints);

/**
* @brief GEOSClusterGeometryDistance
Expand All @@ -3824,21 +3841,19 @@ extern unsigned GEOS_DLL* GEOSClusterDBSCAN(const GEOSGeometry* g, double eps, u
*
* @param g a collection of geometries to be clustered
* @param d minimum distance between geometries in the same cluster
* @param numClusters the number of clusters formed
* @return an array of cluster identifiers
* @return cluster information object
*/
extern unsigned GEOS_DLL* GEOSClusterGeometryDistance(const GEOSGeometry* g, double d, unsigned* numClusters);
extern GEOSClusterInfo GEOS_DLL* GEOSClusterGeometryDistance(const GEOSGeometry* g, double d);

/**
* @brief GEOSClusterGeometryIntersects
*
* Cluster geometries that intersect
*
* @param g a collection of geometries to be clustered
* @param numClusters the number of clusters formed
* @return an array of cluster identifiers
* @return cluster information object
*/
extern unsigned GEOS_DLL* GEOSClusterGeometryIntersects(const GEOSGeometry* g, unsigned* numClusters);
extern GEOSClusterInfo GEOS_DLL* GEOSClusterGeometryIntersects(const GEOSGeometry* g);

/**
* @brief GEOSClusterEnvelopeDistance
Expand All @@ -3847,21 +3862,71 @@ extern unsigned GEOS_DLL* GEOSClusterGeometryIntersects(const GEOSGeometry* g, u
*
* @param g a collection of geometries to be clustered
* @param d minimum envelope distance between geometries in the same cluster
* @param numClusters the number of clusters formed
* @return an array of cluster identifiers
* @return cluster information object
*/
extern unsigned GEOS_DLL* GEOSClusterEnvelopeDistance(const GEOSGeometry* g, double d, unsigned* numClusters);
extern GEOSClusterInfo GEOS_DLL* GEOSClusterEnvelopeDistance(const GEOSGeometry* g, double d);

/**
* @brief GEOSClusterEnvelopeIntersects
*
* Cluster geometries whose envelopes intersect
*
* @param g
* @param numClusters the number of clusters formed
* @return an array of cluster identifiers
* @return cluster information object
*/
extern unsigned GEOS_DLL* GEOSClusterEnvelopeIntersects(const GEOSGeometry* g, unsigned* numClusters);
extern GEOSClusterInfo GEOS_DLL* GEOSClusterEnvelopeIntersects(const GEOSGeometry* g);

/**
* @brief GEOSClusterInfo_getNumClusters
*
* Get the number of clusters identifed by a clustering operation
*
* @param clusters cluster information object
* @return number of clusters identifed
*/
extern size_t GEOS_DLL GEOSClusterInfo_getNumClusters(const GEOSClusterInfo* clusters);

/**
* @brief GEOSClusterInfo_getSize
*
* Get the number of elements in the ith cluster (0-indexed)
*
* @param clusters cluster information object
* @param i cluster for which a size will be returned
* @return number of elements in the cluster
*/
extern size_t GEOS_DLL GEOSClusterInfo_getClusterSize(const GEOSClusterInfo* clusters, size_t i );

/**
* @brief GEOSClusterInfo_getClustersForInputs
*
* Get the cluster index associated with each input to the clustering operation.
* Inputs that do are not associated with any cluster will have an index of GEOS_CLUSTER_NONE.
*
* @param clusters cluster information object
* @return an array of cluster indices, to be freed by the caller.
*/
extern size_t GEOS_DLL* GEOSClusterInfo_getClustersForInputs(const GEOSClusterInfo* clusters);

/**
* @brief GEOSClusterInfo_getInputsForClusterN
* @param clusters cluster information object
* @param i index of the cluster for which indices should be retrieved
* @return a pointer to an array of cluster indices. Size of the array is indicated by
* GEOSClusterInfo_getNumElements. The array is owner by the cluster information
* object and should not be modified or freed by the caller.
*/
extern const size_t GEOS_DLL* GEOSClusterInfo_getInputsForClusterN(const GEOSClusterInfo* clusters, size_t i);

/**
* @brief GEOSClusterInfo_destroy
*
* Destroy a cluster information object.
*
* @param clusters cluster information object
*/
extern void GEOS_DLL GEOSClusterInfo_destroy(GEOSClusterInfo* clusters);

///@}

/* ========== Buffer related functions ========== */
Expand Down
Loading

0 comments on commit 1a0d072

Please sign in to comment.