瀏覽代碼

Fix bug with sparse data (one value per slot)
Allow calculation of 100th percentile


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@35846 15284696-431f-4ddb-bdfa-cd5b030d7da7

Glynn Clements 16 年之前
父節點
當前提交
ab2881ea5f
共有 1 個文件被更改,包括 22 次插入11 次删除
  1. 22 11
      raster/r.quantile/main.c

+ 22 - 11
raster/r.quantile/main.c

@@ -51,7 +51,7 @@ static inline int get_slot(DCELL c)
 
 
 static inline double get_quantile(int n)
 static inline double get_quantile(int n)
 {
 {
-    return (double)total *quants[n];
+    return (double)total * quants[n];
 }
 }
 
 
 static void get_slot_counts(int infile)
 static void get_slot_counts(int infile)
@@ -190,26 +190,37 @@ static void sort_bins(void)
 
 
 static void compute_quantiles(int recode)
 static void compute_quantiles(int recode)
 {
 {
-    struct bin *b = &bins[0];
+    int bin = 0;
     double prev_v = min;
     double prev_v = min;
     int quant;
     int quant;
 
 
     for (quant = 0; quant < num_quants; quant++) {
     for (quant = 0; quant < num_quants; quant++) {
+	struct bin *b;
 	double next = get_quantile(quant);
 	double next = get_quantile(quant);
 	double k, v;
 	double k, v;
 	int i0, i1;
 	int i0, i1;
 
 
-	while (b->origin + b->count < next)
-	    b++;
+	for (; bin < num_bins; bin++) {
+	    b = &bins[bin];
+	    if (b->origin + b->count >= next)
+		break;
+	}
+
+	if (bin < num_bins) {
+	    k = next - b->origin;
+	    i0 = (int)floor(k);
+	    i1 = (int)ceil(k);
 
 
-	k = next - b->origin;
-	i0 = (int)floor(k);
-	i1 = (int)ceil(k);
+	    if (i1 > b->count - 1)
+		i1 = b->count - 1;
 
 
-	v = (i0 == i1)
-	    ? values[b->base + i0]
-	    : values[b->base + i0] * (i1 - k) + values[b->base + i1] * (k -
-									i0);
+	    v = (i0 == i1)
+		? values[b->base + i0]
+		: values[b->base + i0] * (i1 - k) +
+		  values[b->base + i1] * (k - i0);
+	}
+	else
+	    v = max;
 
 
 	if (recode)
 	if (recode)
 	    printf("%f:%f:%i\n", prev_v, v, quant + 1);
 	    printf("%f:%f:%i\n", prev_v, v, quant + 1);