PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

References ARRAY2LWGEOM(), array_nelems_not_null(), cluster_within_distance(), gserialized_from_lwgeom(), linemerge(), LW_SUCCESS, lwgeom_free(), lwgeom_geos_error(), PG_FUNCTION_INFO_V1(), and SRID_UNKNOWN.

Referenced by clusterintersecting_garray(), and pgis_geometry_clusterwithin_finalfn().

3257 {
3258  Datum* result_array_data;
3259  ArrayType *array, *result;
3260  int is3d = 0;
3261  uint32 nelems, nclusters, i;
3262  LWGEOM** lw_inputs;
3263  LWGEOM** lw_results;
3264  double tolerance;
3265  int srid=SRID_UNKNOWN;
3266 
3267  /* Parameters used to construct a result array */
3268  int16 elmlen;
3269  bool elmbyval;
3270  char elmalign;
3271 
3272  /* Null array, null geometry (should be empty?) */
3273  if (PG_ARGISNULL(0))
3274  PG_RETURN_NULL();
3275 
3276  array = PG_GETARG_ARRAYTYPE_P(0);
3277 
3278  tolerance = PG_GETARG_FLOAT8(1);
3279  if (tolerance < 0)
3280  {
3281  lwpgerror("Tolerance must be a positive number.");
3282  PG_RETURN_NULL();
3283  }
3284 
3285  nelems = array_nelems_not_null(array);
3286 
3287  POSTGIS_DEBUGF(3, "cluster_within_distance_garray: number of non-null elements: %d", nelems);
3288 
3289  if ( nelems == 0 ) PG_RETURN_NULL();
3290 
3291  /* TODO short-circuit for one element? */
3292 
3293  /* Ok, we really need geos now ;) */
3294  initGEOS(lwpgnotice, lwgeom_geos_error);
3295 
3296  lw_inputs = ARRAY2LWGEOM(array, nelems, &is3d, &srid);
3297  if (!lw_inputs)
3298  {
3299  PG_RETURN_NULL();
3300  }
3301 
3302  if (cluster_within_distance(lw_inputs, nelems, tolerance, &lw_results, &nclusters) != LW_SUCCESS)
3303  {
3304  elog(ERROR, "cluster_within: Error performing clustering");
3305  PG_RETURN_NULL();
3306  }
3307  pfree(lw_inputs); /* don't need to destroy items because GeometryCollections have taken ownership */
3308 
3309  if (!lw_results) PG_RETURN_NULL();
3310 
3311  result_array_data = palloc(nclusters * sizeof(Datum));
3312  for (i=0; i<nclusters; ++i)
3313  {
3314  result_array_data[i] = PointerGetDatum(gserialized_from_lwgeom(lw_results[i], NULL));
3315  lwgeom_free(lw_results[i]);
3316  }
3317  pfree(lw_results);
3318 
3319  get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign);
3320  result = (ArrayType*) construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign);
3321 
3322  if (!result)
3323  {
3324  elog(ERROR, "clusterwithin: Error constructing return-array");
3325  PG_RETURN_NULL();
3326  }
3327 
3328  PG_RETURN_POINTER(result);
3329 }
uint32_t array_nelems_not_null(ArrayType *array)
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
#define LW_SUCCESS
Definition: liblwgeom.h:80
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...
LWGEOM ** ARRAY2LWGEOM(ArrayType *array, uint32_t nelems, int *is3d, int *srid)
void lwgeom_geos_error(const char *fmt,...)
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
Here is the call graph for this function:
Here is the caller graph for this function: