PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ cluster_within_distance_garray()

Datum cluster_within_distance_garray ( PG_FUNCTION_ARGS  )

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

2094 {
2095  Datum* result_array_data;
2096  ArrayType *array, *result;
2097  int is3d = 0;
2098  uint32 nelems, nclusters, i;
2099  LWGEOM** lw_inputs;
2100  LWGEOM** lw_results;
2101  double tolerance;
2102  int32_t srid = SRID_UNKNOWN;
2103 
2104  /* Parameters used to construct a result array */
2105  int16 elmlen;
2106  bool elmbyval;
2107  char elmalign;
2108 
2109  /* Null array, null geometry (should be empty?) */
2110  if (PG_ARGISNULL(0))
2111  PG_RETURN_NULL();
2112 
2113  array = PG_GETARG_ARRAYTYPE_P(0);
2114 
2115  tolerance = PG_GETARG_FLOAT8(1);
2116  if (tolerance < 0)
2117  {
2118  lwpgerror("Tolerance must be a positive number.");
2119  PG_RETURN_NULL();
2120  }
2121 
2122  nelems = array_nelems_not_null(array);
2123 
2124  POSTGIS_DEBUGF(3, "cluster_within_distance_garray: number of non-null elements: %d", nelems);
2125 
2126  if ( nelems == 0 ) PG_RETURN_NULL();
2127 
2128  /* TODO short-circuit for one element? */
2129 
2130  /* Ok, we really need geos now ;) */
2131  initGEOS(lwpgnotice, lwgeom_geos_error);
2132 
2133  lw_inputs = ARRAY2LWGEOM(array, nelems, &is3d, &srid);
2134  if (!lw_inputs)
2135  {
2136  PG_RETURN_NULL();
2137  }
2138 
2139  if (cluster_within_distance(lw_inputs, nelems, tolerance, &lw_results, &nclusters) != LW_SUCCESS)
2140  {
2141  elog(ERROR, "cluster_within: Error performing clustering");
2142  PG_RETURN_NULL();
2143  }
2144  pfree(lw_inputs); /* don't need to destroy items because GeometryCollections have taken ownership */
2145 
2146  if (!lw_results) PG_RETURN_NULL();
2147 
2148  result_array_data = palloc(nclusters * sizeof(Datum));
2149  for (i=0; i<nclusters; ++i)
2150  {
2151  result_array_data[i] = PointerGetDatum(geometry_serialize(lw_results[i]));
2152  lwgeom_free(lw_results[i]);
2153  }
2154  lwfree(lw_results);
2155 
2156  get_typlenbyvalalign(array->elemtype, &elmlen, &elmbyval, &elmalign);
2157  result = construct_array(result_array_data, nclusters, array->elemtype, elmlen, elmbyval, elmalign);
2158 
2159  if (!result)
2160  {
2161  elog(ERROR, "clusterwithin: Error constructing return-array");
2162  PG_RETURN_NULL();
2163  }
2164 
2165  PG_RETURN_POINTER(result);
2166 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
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:1218
#define LW_SUCCESS
Definition: liblwgeom.h:97
void lwfree(void *mem)
Definition: lwutil.c:248
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
static 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: