PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

3022 {
3023  Datum* result_array_data;
3024  ArrayType *array, *result;
3025  int is3d = 0;
3026  uint32 nelems, nclusters, i;
3027  LWGEOM** lw_inputs;
3028  LWGEOM** lw_results;
3029  double tolerance;
3030  int32_t srid = SRID_UNKNOWN;
3031 
3032  /* Parameters used to construct a result array */
3033  int16 elmlen;
3034  bool elmbyval;
3035  char elmalign;
3036 
3037  /* Null array, null geometry (should be empty?) */
3038  if (PG_ARGISNULL(0))
3039  PG_RETURN_NULL();
3040 
3041  array = PG_GETARG_ARRAYTYPE_P(0);
3042 
3043  tolerance = PG_GETARG_FLOAT8(1);
3044  if (tolerance < 0)
3045  {
3046  lwpgerror("Tolerance must be a positive number.");
3047  PG_RETURN_NULL();
3048  }
3049 
3050  nelems = array_nelems_not_null(array);
3051 
3052  POSTGIS_DEBUGF(3, "cluster_within_distance_garray: number of non-null elements: %d", nelems);
3053 
3054  if ( nelems == 0 ) PG_RETURN_NULL();
3055 
3056  /* TODO short-circuit for one element? */
3057 
3058  /* Ok, we really need geos now ;) */
3059  initGEOS(lwpgnotice, lwgeom_geos_error);
3060 
3061  lw_inputs = ARRAY2LWGEOM(array, nelems, &is3d, &srid);
3062  if (!lw_inputs)
3063  {
3064  PG_RETURN_NULL();
3065  }
3066 
3067  if (cluster_within_distance(lw_inputs, nelems, tolerance, &lw_results, &nclusters) != LW_SUCCESS)
3068  {
3069  elog(ERROR, "cluster_within: Error performing clustering");
3070  PG_RETURN_NULL();
3071  }
3072  pfree(lw_inputs); /* don't need to destroy items because GeometryCollections have taken ownership */
3073 
3074  if (!lw_results) PG_RETURN_NULL();
3075 
3076  result_array_data = palloc(nclusters * sizeof(Datum));
3077  for (i=0; i<nclusters; ++i)
3078  {
3079  result_array_data[i] = PointerGetDatum(geometry_serialize(lw_results[i]));
3080  lwgeom_free(lw_results[i]);
3081  }
3082  lwfree(lw_results);
3083 
3084  get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign);
3085  result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign);
3086 
3087  if (!result)
3088  {
3089  elog(ERROR, "clusterwithin: Error constructing return-array");
3090  PG_RETURN_NULL();
3091  }
3092 
3093  PG_RETURN_POINTER(result);
3094 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
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:1155
#define LW_SUCCESS
Definition: liblwgeom.h:112
void lwfree(void *mem)
Definition: lwutil.c:242
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:230
uint32_t array_nelems_not_null(ArrayType *array)
LWGEOM ** ARRAY2LWGEOM(ArrayType *array, uint32_t nelems, int *is3d, int *srid)

References ARRAY2LWGEOM(), array_nelems_not_null(), cluster_within_distance(), LW_SUCCESS, lwfree(), lwgeom_free(), lwgeom_geos_error(), result, 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: