PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

3119 {
3120  Datum* result_array_data;
3121  ArrayType *array, *result;
3122  int is3d = 0;
3123  uint32 nelems, nclusters, i;
3124  LWGEOM** lw_inputs;
3125  LWGEOM** lw_results;
3126  double tolerance;
3127  int32_t srid = SRID_UNKNOWN;
3128 
3129  /* Parameters used to construct a result array */
3130  int16 elmlen;
3131  bool elmbyval;
3132  char elmalign;
3133 
3134  /* Null array, null geometry (should be empty?) */
3135  if (PG_ARGISNULL(0))
3136  PG_RETURN_NULL();
3137 
3138  array = PG_GETARG_ARRAYTYPE_P(0);
3139 
3140  tolerance = PG_GETARG_FLOAT8(1);
3141  if (tolerance < 0)
3142  {
3143  lwpgerror("Tolerance must be a positive number.");
3144  PG_RETURN_NULL();
3145  }
3146 
3147  nelems = array_nelems_not_null(array);
3148 
3149  POSTGIS_DEBUGF(3, "cluster_within_distance_garray: number of non-null elements: %d", nelems);
3150 
3151  if ( nelems == 0 ) PG_RETURN_NULL();
3152 
3153  /* TODO short-circuit for one element? */
3154 
3155  /* Ok, we really need geos now ;) */
3156  initGEOS(lwpgnotice, lwgeom_geos_error);
3157 
3158  lw_inputs = ARRAY2LWGEOM(array, nelems, &is3d, &srid);
3159  if (!lw_inputs)
3160  {
3161  PG_RETURN_NULL();
3162  }
3163 
3164  if (cluster_within_distance(lw_inputs, nelems, tolerance, &lw_results, &nclusters) != LW_SUCCESS)
3165  {
3166  elog(ERROR, "cluster_within: Error performing clustering");
3167  PG_RETURN_NULL();
3168  }
3169  pfree(lw_inputs); /* don't need to destroy items because GeometryCollections have taken ownership */
3170 
3171  if (!lw_results) PG_RETURN_NULL();
3172 
3173  result_array_data = palloc(nclusters * sizeof(Datum));
3174  for (i=0; i<nclusters; ++i)
3175  {
3176  result_array_data[i] = PointerGetDatum(geometry_serialize(lw_results[i]));
3177  lwgeom_free(lw_results[i]);
3178  }
3179  lwfree(lw_results);
3180 
3181  get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign);
3182  result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign);
3183 
3184  if (!result)
3185  {
3186  elog(ERROR, "clusterwithin: Error constructing return-array");
3187  PG_RETURN_NULL();
3188  }
3189 
3190  PG_RETURN_POINTER(result);
3191 }
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: