PostGIS  3.2.2dev-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 784 of file gserialized_estimate.c.

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