PostGIS  2.5.2dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

Definition at line 2955 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(), pgis_abs_out(), and pgis_geometry_clusterwithin_finalfn().

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