PostGIS  2.5.0dev-r@@SVN_REVISION@@
Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

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().

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