PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ rt_band_get_quantiles()

rt_quantile rt_band_get_quantiles ( rt_bandstats  stats,
double *  quantiles,
int  quantiles_count,
uint32_t *  rtn_count 
)

Compute the default set of or requested quantiles for a set of data the quantile formula used is same as Excel and R default method.

Parameters
stats: a populated stats struct for processing
quantiles: the quantiles to be computed
quantiles_count: the number of quantiles to be computed
rtn_count: set to the number of quantiles being returned
Returns
the default set of or requested quantiles for a band or NULL

Definition at line 673 of file rt_statistics.c.

677 {
678 rt_quantile rtn;
679 int init_quantiles = 0;
680 int i = 0;
681 double h;
682 int hl;
683
684#if POSTGIS_DEBUG_LEVEL > 0
685 clock_t start, stop;
686 double elapsed = 0;
687#endif
688
689 RASTER_DEBUG(3, "starting");
690#if POSTGIS_DEBUG_LEVEL > 0
691 start = clock();
692#endif
693
694 assert(NULL != stats);
695 assert(NULL != rtn_count);
696
697 if (stats->count < 1 || NULL == stats->values) {
698 rterror("rt_band_get_quantiles: rt_bandstats object has no value");
699 return NULL;
700 }
701
702 /* quantiles not provided */
703 if (NULL == quantiles) {
704 /* quantile count not specified, default to quartiles */
705 if (quantiles_count < 2)
706 quantiles_count = 5;
707
708 quantiles = rtalloc(sizeof(double) * quantiles_count);
709 init_quantiles = 1;
710 if (NULL == quantiles) {
711 rterror("rt_band_get_quantiles: Could not allocate memory for quantile input");
712 return NULL;
713 }
714
715 quantiles_count--;
716 for (i = 0; i <= quantiles_count; i++)
717 quantiles[i] = ((double) i) / quantiles_count;
718 quantiles_count++;
719 }
720
721 /* check quantiles */
722 for (i = 0; i < quantiles_count; i++) {
723 if (quantiles[i] < 0. || quantiles[i] > 1.) {
724 rterror("rt_band_get_quantiles: Quantile value not between 0 and 1");
725 if (init_quantiles) rtdealloc(quantiles);
726 return NULL;
727 }
728 }
729 quicksort(quantiles, quantiles + quantiles_count - 1);
730
731 /* initialize rt_quantile */
732 rtn = rtalloc(sizeof(struct rt_quantile_t) * quantiles_count);
733 if (NULL == rtn) {
734 rterror("rt_band_get_quantiles: Could not allocate memory for quantile output");
735 if (init_quantiles) rtdealloc(quantiles);
736 return NULL;
737 }
738
739 /* sort values */
740 if (!stats->sorted) {
741 quicksort(stats->values, stats->values + stats->count - 1);
742 stats->sorted = 1;
743 }
744
745 /*
746 make quantiles
747
748 formula is that used in R (method 7) and Excel from
749 http://en.wikipedia.org/wiki/Quantile
750 */
751 for (i = 0; i < quantiles_count; i++) {
752 rtn[i].quantile = quantiles[i];
753
754 h = ((stats->count - 1.) * quantiles[i]) + 1.;
755 hl = floor(h);
756
757 /* h greater than hl, do full equation */
758 if (h > hl)
759 rtn[i].value = stats->values[hl - 1] + ((h - hl) * (stats->values[hl] - stats->values[hl - 1]));
760 /* shortcut as second part of equation is zero */
761 else
762 rtn[i].value = stats->values[hl - 1];
763 }
764
765#if POSTGIS_DEBUG_LEVEL > 0
766 stop = clock();
767 elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
768 RASTER_DEBUGF(3, "elapsed time = %0.4f", elapsed);
769#endif
770
771 *rtn_count = quantiles_count;
772 if (init_quantiles) rtdealloc(quantiles);
773 RASTER_DEBUG(3, "done");
774 return rtn;
775}
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition rt_context.c:191
#define RASTER_DEBUG(level, msg)
Definition librtcore.h:304
#define RASTER_DEBUGF(level, msg,...)
Definition librtcore.h:308
void rtdealloc(void *mem)
Definition rt_context.c:206
static void quicksort(double *left, double *right)
uint32_t count
Definition librtcore.h:2562
double * values
Definition librtcore.h:2570
double quantile
Definition librtcore.h:2588

References rt_bandstats_t::count, rt_quantile_t::quantile, quicksort(), RASTER_DEBUG, RASTER_DEBUGF, rtalloc(), rtdealloc(), rterror(), rt_bandstats_t::sorted, rt_quantile_t::value, and rt_bandstats_t::values.

Referenced by RASTER_quantile(), and test_band_stats().

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