PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

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