PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

3049 {
3050  Datum* result_array_data;
3051  ArrayType *array, *result;
3052  int is3d = 0;
3053  uint32 nelems, nclusters, i;
3054  LWGEOM** lw_inputs;
3055  LWGEOM** lw_results;
3056  double tolerance;
3057  int32_t srid = SRID_UNKNOWN;
3058 
3059  /* Parameters used to construct a result array */
3060  int16 elmlen;
3061  bool elmbyval;
3062  char elmalign;
3063 
3064  /* Null array, null geometry (should be empty?) */
3065  if (PG_ARGISNULL(0))
3066  PG_RETURN_NULL();
3067 
3068  array = PG_GETARG_ARRAYTYPE_P(0);
3069 
3070  tolerance = PG_GETARG_FLOAT8(1);
3071  if (tolerance < 0)
3072  {
3073  lwpgerror("Tolerance must be a positive number.");
3074  PG_RETURN_NULL();
3075  }
3076 
3077  nelems = array_nelems_not_null(array);
3078 
3079  POSTGIS_DEBUGF(3, "cluster_within_distance_garray: number of non-null elements: %d", nelems);
3080 
3081  if ( nelems == 0 ) PG_RETURN_NULL();
3082 
3083  /* TODO short-circuit for one element? */
3084 
3085  /* Ok, we really need geos now ;) */
3086  initGEOS(lwpgnotice, lwgeom_geos_error);
3087 
3088  lw_inputs = ARRAY2LWGEOM(array, nelems, &is3d, &srid);
3089  if (!lw_inputs)
3090  {
3091  PG_RETURN_NULL();
3092  }
3093 
3094  if (cluster_within_distance(lw_inputs, nelems, tolerance, &lw_results, &nclusters) != LW_SUCCESS)
3095  {
3096  elog(ERROR, "cluster_within: Error performing clustering");
3097  PG_RETURN_NULL();
3098  }
3099  pfree(lw_inputs); /* don't need to destroy items because GeometryCollections have taken ownership */
3100 
3101  if (!lw_results) PG_RETURN_NULL();
3102 
3103  result_array_data = palloc(nclusters * sizeof(Datum));
3104  for (i=0; i<nclusters; ++i)
3105  {
3106  result_array_data[i] = PointerGetDatum(geometry_serialize(lw_results[i]));
3107  lwgeom_free(lw_results[i]);
3108  }
3109  lwfree(lw_results);
3110 
3111  get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign);
3112  result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign);
3113 
3114  if (!result)
3115  {
3116  elog(ERROR, "clusterwithin: Error constructing return-array");
3117  PG_RETURN_NULL();
3118  }
3119 
3120  PG_RETURN_POINTER(result);
3121 }
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: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)

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: