PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

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