PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

Definition at line 2960 of file postgis/lwgeom_geos.c.

2961{
2962 Datum* result_array_data;
2963 ArrayType *array, *result;
2964 int is3d = 0;
2965 uint32 nelems, nclusters, i;
2966 LWGEOM** lw_inputs;
2967 LWGEOM** lw_results;
2968 double tolerance;
2969 int32_t srid = SRID_UNKNOWN;
2970
2971 /* Parameters used to construct a result array */
2972 int16 elmlen;
2973 bool elmbyval;
2974 char elmalign;
2975
2976 /* Null array, null geometry (should be empty?) */
2977 if (PG_ARGISNULL(0))
2978 PG_RETURN_NULL();
2979
2980 array = PG_GETARG_ARRAYTYPE_P(0);
2981
2982 tolerance = PG_GETARG_FLOAT8(1);
2983 if (tolerance < 0)
2984 {
2985 lwpgerror("Tolerance must be a positive number.");
2986 PG_RETURN_NULL();
2987 }
2988
2989 nelems = array_nelems_not_null(array);
2990
2991 POSTGIS_DEBUGF(3, "cluster_within_distance_garray: number of non-null elements: %d", nelems);
2992
2993 if ( nelems == 0 ) PG_RETURN_NULL();
2994
2995 /* TODO short-circuit for one element? */
2996
2997 /* Ok, we really need geos now ;) */
2998 initGEOS(lwpgnotice, lwgeom_geos_error);
2999
3000 lw_inputs = ARRAY2LWGEOM(array, nelems, &is3d, &srid);
3001 if (!lw_inputs)
3002 {
3003 PG_RETURN_NULL();
3004 }
3005
3006 if (cluster_within_distance(lw_inputs, nelems, tolerance, &lw_results, &nclusters) != LW_SUCCESS)
3007 {
3008 elog(ERROR, "cluster_within: Error performing clustering");
3009 PG_RETURN_NULL();
3010 }
3011 pfree(lw_inputs); /* don't need to destroy items because GeometryCollections have taken ownership */
3012
3013 if (!lw_results) PG_RETURN_NULL();
3014
3015 result_array_data = palloc(nclusters * sizeof(Datum));
3016 for (i=0; i<nclusters; ++i)
3017 {
3018 result_array_data[i] = PointerGetDatum(geometry_serialize(lw_results[i]));
3019 lwgeom_free(lw_results[i]);
3020 }
3021 lwfree(lw_results);
3022
3023 get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign);
3024 result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign);
3025
3026 if (!result)
3027 {
3028 elog(ERROR, "clusterwithin: Error constructing return-array");
3029 PG_RETURN_NULL();
3030 }
3031
3032 PG_RETURN_POINTER(result);
3033}
void lwgeom_geos_error(const char *fmt,...)
int cluster_within_distance(LWGEOM **geoms, uint32_t num_geoms, double tolerance, LWGEOM ***clusterGeoms, uint32_t *num_clusters)
Takes an array of LWGEOM* and constructs an array of LWGEOM*, where each element in the constructed a...
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1138
#define LW_SUCCESS
Definition liblwgeom.h:111
void lwfree(void *mem)
Definition lwutil.c:242
#define SRID_UNKNOWN
Unknown SRID value.
Definition liblwgeom.h:229
uint32_t array_nelems_not_null(ArrayType *array)
LWGEOM ** ARRAY2LWGEOM(ArrayType *array, uint32_t nelems, int *is3d, int *srid)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

References ARRAY2LWGEOM(), array_nelems_not_null(), cluster_within_distance(), geometry_serialize(), LW_SUCCESS, lwfree(), lwgeom_free(), lwgeom_geos_error(), and SRID_UNKNOWN.

Referenced by pgis_geometry_clusterwithin_finalfn().

Here is the call graph for this function:
Here is the caller graph for this function: