PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

2954 {
2955  Datum* result_array_data;
2956  ArrayType *array, *result;
2957  int is3d = 0;
2958  uint32 nelems, nclusters, i;
2959  LWGEOM** lw_inputs;
2960  LWGEOM** lw_results;
2961  double tolerance;
2962  int srid=SRID_UNKNOWN;
2963 
2964  /* Parameters used to construct a result array */
2965  int16 elmlen;
2966  bool elmbyval;
2967  char elmalign;
2968 
2969  /* Null array, null geometry (should be empty?) */
2970  if (PG_ARGISNULL(0))
2971  PG_RETURN_NULL();
2972 
2973  array = PG_GETARG_ARRAYTYPE_P(0);
2974 
2975  tolerance = PG_GETARG_FLOAT8(1);
2976  if (tolerance < 0)
2977  {
2978  lwpgerror("Tolerance must be a positive number.");
2979  PG_RETURN_NULL();
2980  }
2981 
2982  nelems = array_nelems_not_null(array);
2983 
2984  POSTGIS_DEBUGF(3, "cluster_within_distance_garray: number of non-null elements: %d", nelems);
2985 
2986  if ( nelems == 0 ) PG_RETURN_NULL();
2987 
2988  /* TODO short-circuit for one element? */
2989 
2990  /* Ok, we really need geos now ;) */
2991  initGEOS(lwpgnotice, lwgeom_geos_error);
2992 
2993  lw_inputs = ARRAY2LWGEOM(array, nelems, &is3d, &srid);
2994  if (!lw_inputs)
2995  {
2996  PG_RETURN_NULL();
2997  }
2998 
2999  if (cluster_within_distance(lw_inputs, nelems, tolerance, &lw_results, &nclusters) != LW_SUCCESS)
3000  {
3001  elog(ERROR, "cluster_within: Error performing clustering");
3002  PG_RETURN_NULL();
3003  }
3004  pfree(lw_inputs); /* don't need to destroy items because GeometryCollections have taken ownership */
3005 
3006  if (!lw_results) PG_RETURN_NULL();
3007 
3008  result_array_data = palloc(nclusters * sizeof(Datum));
3009  for (i=0; i<nclusters; ++i)
3010  {
3011  result_array_data[i] = PointerGetDatum(gserialized_from_lwgeom(lw_results[i], NULL));
3012  lwgeom_free(lw_results[i]);
3013  }
3014  pfree(lw_results);
3015 
3016  get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign);
3017  result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign);
3018 
3019  if (!result)
3020  {
3021  elog(ERROR, "clusterwithin: Error constructing return-array");
3022  PG_RETURN_NULL();
3023  }
3024 
3025  PG_RETURN_POINTER(result);
3026 }
GSERIALIZED * gserialized_from_lwgeom(LWGEOM *geom, size_t *size)
Allocate a new GSERIALIZED from an LWGEOM.
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:1144
#define LW_SUCCESS
Definition: liblwgeom.h:80
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
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(), gserialized_from_lwgeom(), LW_SUCCESS, lwgeom_free(), lwgeom_geos_error(), 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: