Selaa lähdekoodia

r.topmodel: fixed topidxstats file generation (merge from https://trac.osgeo.org/grass/changeset/60856)

git-svn-id: https://svn.osgeo.org/grass/grass/branches/releasebranch_7_0@60857 15284696-431f-4ddb-bdfa-cd5b030d7da7
Huidae Cho 11 vuotta sitten
vanhempi
commit
d1894fba0e
1 muutettua tiedostoa jossa 17 lisäystä ja 1 poistoa
  1. 17 1
      raster/r.topmodel/topmodel.c

+ 17 - 1
raster/r.topmodel/topmodel.c

@@ -10,7 +10,7 @@ void create_topidxstats(char *topidx, int ntopidxclasses, char *outtopidxstats)
     const char *args[5];
     struct Popen child;
     FILE *fp;
-    double *atb, *Aatb_r;
+    double *atb, *Aatb_r, delta, prev_atb2;
     int i;
     int total_ncells;
 
@@ -33,15 +33,31 @@ void create_topidxstats(char *topidx, int ntopidxclasses, char *outtopidxstats)
     Aatb_r = (double *)G_malloc(ntopidxclasses * sizeof(double));
 
     total_ncells = 0;
+    delta = -1.0;
+    prev_atb2 = 0.0;
+
     for (i = 0; i < ntopidxclasses - 1 && !feof(fp);) {
 	double atb1, atb2;
 	int ncells;
 
 	get_line(fp, buf);
 	if (sscanf(buf, "%lf-%lf %d", &atb1, &atb2, &ncells) == 3) {
+	    if (delta < 0)
+		delta = atb2 - atb1;
+	    else if (atb1 > prev_atb2 + 0.5 * delta) {
+		/* r.stats doesn't report non-existing ranges at all. Use 0.5 *
+		 * delta to avoid comparing two almost same double numbers. */
+		while (prev_atb2 < atb1 - 0.5 * delta) {
+		    atb[i] = prev_atb2;
+		    Aatb_r[i++] = 0.0;
+		    prev_atb2 += delta;
+		}
+	    }
+
 	    atb[i] = atb1;
 	    Aatb_r[i] = (double)ncells;
 	    total_ncells += ncells;
+	    prev_atb2 = atb2;
 
 	    if (++i == ntopidxclasses - 1) {
 		atb[i] = atb2;