PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ nd_box_array_distribution()

static int nd_box_array_distribution ( const ND_BOX **  nd_boxes,
int  num_boxes,
const ND_BOX extent,
int  ndims,
double *  distribution 
)
static

Calculate how much a set of boxes is homogenously distributed or contentrated within one dimension, returning the range_quintile of of the overlap counts per cell in a uniform partition of the extent of the dimension.

A uniform distribution of counts will have a small range and will require few cells in a selectivity histogram. A diverse distribution of counts will have a larger range and require more cells in a selectivity histogram (to distinguish between areas of feature density and areas of feature sparseness. This measurement should help us identify cases like X/Y/Z data where there is lots of variability in density in X/Y (diversely in a multi-kilometer range) and far less in Z (in a few-hundred meter range).

Definition at line 786 of file gserialized_estimate.c.

787 {
788  int d, i, k, range;
789  int counts[NUM_BINS];
790  double smin, smax; /* Spatial min, spatial max */
791  double swidth; /* Spatial width of dimension */
792 #if POSTGIS_DEBUG_LEVEL >= 3
793  double average, sdev, sdev_ratio;
794 #endif
795  int bmin, bmax; /* Bin min, bin max */
796  const ND_BOX *ndb;
797 
798  /* For each dimension... */
799  for ( d = 0; d < ndims; d++ )
800  {
801  /* Initialize counts for this dimension */
802  memset(counts, 0, sizeof(counts));
803 
804  smin = extent->min[d];
805  smax = extent->max[d];
806  swidth = smax - smin;
807 
808  /* Don't try and calculate distribution of overly narrow */
809  /* or overly wide dimensions. Here we're being pretty geographical, */
810  /* expecting "normal" planar or geographic coordinates. */
811  /* Otherwise we have to "handle" +/- Inf bounded features and */
812  /* the assumptions needed for that are as bad as this hack. */
813  if ( swidth < MIN_DIMENSION_WIDTH || swidth > MAX_DIMENSION_WIDTH )
814  {
815  distribution[d] = 0;
816  continue;
817  }
818 
819  /* Sum up the overlaps of each feature with the dimensional bins */
820  for ( i = 0; i < num_boxes; i++ )
821  {
822  double minoffset, maxoffset;
823 
824  /* Skip null entries */
825  ndb = nd_boxes[i];
826  if ( ! ndb ) continue;
827 
828  /* Where does box fall relative to the working range */
829  minoffset = ndb->min[d] - smin;
830  maxoffset = ndb->max[d] - smin;
831 
832  /* Skip boxes that our outside our working range */
833  if ( minoffset < 0 || minoffset > swidth ||
834  maxoffset < 0 || maxoffset > swidth )
835  {
836  continue;
837  }
838 
839  /* What bins does this range correspond to? */
840  bmin = floor(NUM_BINS * minoffset / swidth);
841  bmax = floor(NUM_BINS * maxoffset / swidth);
842 
843  /* Should only happen when maxoffset==swidth */
844  if (bmax >= NUM_BINS)
845  bmax = NUM_BINS-1;
846 
847  POSTGIS_DEBUGF(4, " dimension %d, feature %d: bin %d to bin %d", d, i, bmin, bmax);
848 
849  /* Increment the counts in all the bins this feature overlaps */
850  for ( k = bmin; k <= bmax; k++ )
851  {
852  counts[k] += 1;
853  }
854 
855  }
856 
857  /* How dispersed is the distribution of features across bins? */
858  range = range_quintile(counts, NUM_BINS);
859 
860 #if POSTGIS_DEBUG_LEVEL >= 3
861  average = avg(counts, NUM_BINS);
862  sdev = stddev(counts, NUM_BINS);
863  sdev_ratio = sdev/average;
864 
865  POSTGIS_DEBUGF(3, " dimension %d: range = %d", d, range);
866  POSTGIS_DEBUGF(3, " dimension %d: average = %.6g", d, average);
867  POSTGIS_DEBUGF(3, " dimension %d: stddev = %.6g", d, sdev);
868  POSTGIS_DEBUGF(3, " dimension %d: stddev_ratio = %.6g", d, sdev_ratio);
869 #endif
870 
871  distribution[d] = range;
872  }
873 
874  return true;
875 }
static int range_quintile(int *vals, int nvals)
The difference between the fourth and first quintile values, the "inter-quintile range".
#define NUM_BINS
#define MAX_DIMENSION_WIDTH
Maximum width of a dimension that we'll bother trying to compute statistics on.
float4 max[ND_DIMS]
float4 min[ND_DIMS]
N-dimensional box type for calculations, to avoid doing explicit axis conversions from GBOX in all ca...

References ND_BOX_T::max, MAX_DIMENSION_WIDTH, ND_BOX_T::min, NUM_BINS, and range_quintile().

Referenced by compute_gserialized_stats_mode().

Here is the call graph for this function:
Here is the caller graph for this function: