PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

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