[GRASS-SVN] r59171 - in grass/trunk/raster/r.li: r.li.mps r.li.patchdensity r.li.patchnum

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 2 14:37:13 PST 2014


Author: mmetz
Date: 2014-03-02 14:37:13 -0800 (Sun, 02 Mar 2014)
New Revision: 59171

Modified:
   grass/trunk/raster/r.li/r.li.mps/mps.c
   grass/trunk/raster/r.li/r.li.patchdensity/main.c
   grass/trunk/raster/r.li/r.li.patchnum/main.c
Log:
r.li: sync indices based on patch number

Modified: grass/trunk/raster/r.li/r.li.mps/mps.c
===================================================================
--- grass/trunk/raster/r.li/r.li.mps/mps.c	2014-03-02 22:35:34 UTC (rev 59170)
+++ grass/trunk/raster/r.li/r.li.mps/mps.c	2014-03-02 22:37:13 UTC (rev 59171)
@@ -52,6 +52,7 @@
     G_add_keyword(_("raster"));
     G_add_keyword(_("landscape structure analysis"));
     G_add_keyword(_("patch index"));
+
     /* define options */
 
     raster = G_define_standard_option(G_OPT_R_INPUT);
@@ -73,7 +74,7 @@
 
 int meanPatchSize(int fd, char **par, struct area_entry *ad, double *result)
 {
-    int ris = 0;
+    int ris = RLI_OK;
     double indice = 0;
 
     switch (ad->data_type) {
@@ -97,10 +98,11 @@
 	    G_fatal_error("data type unknown");
 	    return RLI_ERRORE;
 	}
+    }
 
+    if (ris != RLI_OK) {
+	return RLI_ERRORE;
     }
-    if (ris != RLI_OK)
-	return RLI_ERRORE;
 
     *result = indice;
 
@@ -128,10 +130,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -191,7 +193,7 @@
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
 		return 0;
 	}
-	
+
 	ltmp = pid_sup;
 	pid_sup = pid_corr;
 	pid_corr = ltmp;
@@ -200,7 +202,7 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
@@ -221,9 +223,9 @@
 	    }
 
 	    if (!Rast_is_c_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -231,7 +233,7 @@
 
 	    if (!Rast_is_c_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -242,20 +244,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -274,7 +276,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
@@ -351,10 +353,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -423,7 +425,7 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
@@ -444,9 +446,9 @@
 	    }
 
 	    if (!Rast_is_d_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -454,7 +456,7 @@
 
 	    if (!Rast_is_d_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -465,20 +467,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -497,7 +499,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
@@ -574,10 +576,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -646,7 +648,7 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
@@ -667,9 +669,9 @@
 	    }
 
 	    if (!Rast_is_f_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -677,7 +679,7 @@
 
 	    if (!Rast_is_f_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -688,20 +690,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -720,7 +722,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));

Modified: grass/trunk/raster/r.li/r.li.patchdensity/main.c
===================================================================
--- grass/trunk/raster/r.li/r.li.patchdensity/main.c	2014-03-02 22:35:34 UTC (rev 59170)
+++ grass/trunk/raster/r.li/r.li.patchdensity/main.c	2014-03-02 22:37:13 UTC (rev 59171)
@@ -79,17 +79,17 @@
     switch (ad->data_type) {
     case CELL_TYPE:
 	{
-	    calculate(fd, ad, &indice);
+	    ris = calculate(fd, ad, &indice);
 	    break;
 	}
     case DCELL_TYPE:
 	{
-	    calculateD(fd, ad, &indice);
+	    ris = calculateD(fd, ad, &indice);
 	    break;
 	}
     case FCELL_TYPE:
 	{
-	    calculateF(fd, ad, &indice);
+	    ris = calculateF(fd, ad, &indice);
 	    break;
 	}
     default:
@@ -129,10 +129,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -192,7 +192,7 @@
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
 		return 0;
 	}
-	
+
 	ltmp = pid_sup;
 	pid_sup = pid_corr;
 	pid_corr = ltmp;
@@ -201,30 +201,32 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
 		Rast_set_c_null_value(&corrCell, 1);
 	    }
+	    else {
+		/* total landscape area */
+		area++;
+	    }
 
 	    if (Rast_is_c_null_value(&corrCell)) {
 		connected = 0;
 		precCell = corrCell;
 		continue;
 	    }
-	    
-	    area++;
-	    
+
 	    supCell = buf_sup[j + ad->x];
 	    if (masked && (mask_sup[j] == 0)) {
 		Rast_set_c_null_value(&supCell, 1);
 	    }
 
 	    if (!Rast_is_c_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -232,7 +234,7 @@
 
 	    if (!Rast_is_c_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -243,20 +245,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -275,7 +277,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
@@ -294,7 +296,7 @@
 	}
     }
 
-    if (npatch > 0) {
+    if (area > 0) {
 	double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
 	double area_units;
 
@@ -309,13 +311,13 @@
 	/* NS Dist at West edge */
 	NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south);
 
-	area_units = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) *
-	    (((NS_DIST1 + NS_DIST2) / 2) / hd.rows) * (ad->rl * ad->cl);
+	area_units = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols)) *
+	             ((NS_DIST1 + NS_DIST2) / (2 * hd.rows)) * area;
 
 	*result = (npatch / area_units) * 1000000;
     }
     else {
-	*result = 0;
+	Rast_set_d_null_value(result, 1);
     }
 
     if (masked) {
@@ -352,10 +354,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -424,30 +426,32 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
 		Rast_set_d_null_value(&corrCell, 1);
 	    }
+	    else {
+		/* total landscape area */
+		area++;
+	    }
 
 	    if (Rast_is_d_null_value(&corrCell)) {
 		connected = 0;
 		precCell = corrCell;
 		continue;
 	    }
-	    
-	    area++;
-	    
+
 	    supCell = buf_sup[j + ad->x];
 	    if (masked && (mask_sup[j] == 0)) {
 		Rast_set_d_null_value(&supCell, 1);
 	    }
 
 	    if (!Rast_is_d_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -455,7 +459,7 @@
 
 	    if (!Rast_is_d_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -466,20 +470,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -498,7 +502,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
@@ -517,7 +521,7 @@
 	}
     }
 
-    if (npatch > 0) {
+    if (area > 0) {
 	double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
 	double area_units;
 
@@ -532,13 +536,13 @@
 	/* NS Dist at West edge */
 	NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south);
 
-	area_units = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) *
-	    (((NS_DIST1 + NS_DIST2) / 2) / hd.rows) * (ad->rl * ad->cl);
+	area_units = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols)) *
+	             ((NS_DIST1 + NS_DIST2) / (2 * hd.rows)) * area;
 
 	*result = (npatch / area_units) * 1000000;
     }
     else {
-	*result = 0;
+	Rast_set_d_null_value(result, 1);
     }
 
     if (masked) {
@@ -575,10 +579,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -647,30 +651,32 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
 		Rast_set_f_null_value(&corrCell, 1);
 	    }
+	    else {
+		/* total landscape area */
+		area++;
+	    }
 
 	    if (Rast_is_f_null_value(&corrCell)) {
 		connected = 0;
 		precCell = corrCell;
 		continue;
 	    }
-	    
-	    area++;
-	    
+
 	    supCell = buf_sup[j + ad->x];
 	    if (masked && (mask_sup[j] == 0)) {
 		Rast_set_f_null_value(&supCell, 1);
 	    }
 
 	    if (!Rast_is_f_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -678,7 +684,7 @@
 
 	    if (!Rast_is_f_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -689,20 +695,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -721,7 +727,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
@@ -740,7 +746,7 @@
 	}
     }
 
-    if (npatch > 0) {
+    if (area > 0) {
 	double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
 	double area_units;
 
@@ -755,13 +761,13 @@
 	/* NS Dist at West edge */
 	NS_DIST2 = G_distance(hd.west, hd.north, hd.west, hd.south);
 
-	area_units = (((EW_DIST1 + EW_DIST2) / 2) / hd.cols) *
-	    (((NS_DIST1 + NS_DIST2) / 2) / hd.rows) * (ad->rl * ad->cl);
+	area_units = ((EW_DIST1 + EW_DIST2) / (2 * hd.cols)) *
+	             ((NS_DIST1 + NS_DIST2) / (2 * hd.rows)) * area;
 
 	*result = (npatch / area_units) * 1000000;
     }
     else {
-	*result = 0;
+	Rast_set_d_null_value(result, 1);
     }
 
     if (masked) {

Modified: grass/trunk/raster/r.li/r.li.patchnum/main.c
===================================================================
--- grass/trunk/raster/r.li/r.li.patchnum/main.c	2014-03-02 22:35:34 UTC (rev 59170)
+++ grass/trunk/raster/r.li/r.li.patchnum/main.c	2014-03-02 22:37:13 UTC (rev 59171)
@@ -70,6 +70,7 @@
 			  output->answer);
 }
 
+
 int patch_number(int fd, char **par, struct area_entry *ad, double *result)
 {
     int ris = RLI_OK;
@@ -78,17 +79,17 @@
     switch (ad->data_type) {
     case CELL_TYPE:
 	{
-	    calculate(fd, ad, &indice);
+	    ris = calculate(fd, ad, &indice);
 	    break;
 	}
     case DCELL_TYPE:
 	{
-	    calculateD(fd, ad, &indice);
+	    ris = calculateD(fd, ad, &indice);
 	    break;
 	}
     case FCELL_TYPE:
 	{
-	    calculateF(fd, ad, &indice);
+	    ris = calculateF(fd, ad, &indice);
 	    break;
 	}
     default:
@@ -128,10 +129,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -191,7 +192,7 @@
 	    if (read(mask_fd, mask_buf, (ad->cl * sizeof(int))) < 0)
 		return 0;
 	}
-	
+
 	ltmp = pid_sup;
 	pid_sup = pid_corr;
 	pid_corr = ltmp;
@@ -200,7 +201,7 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
@@ -221,9 +222,9 @@
 	    }
 
 	    if (!Rast_is_c_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -231,7 +232,7 @@
 
 	    if (!Rast_is_c_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -242,20 +243,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -274,7 +275,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
@@ -329,10 +330,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -401,7 +402,7 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
@@ -422,9 +423,9 @@
 	    }
 
 	    if (!Rast_is_d_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -432,7 +433,7 @@
 
 	    if (!Rast_is_d_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -443,20 +444,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -475,7 +476,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));
@@ -530,10 +531,10 @@
     buf_sup = buf_null;
 
     /* initialize patch ids */
-    pid_corr = G_malloc(Rast_window_cols() * sizeof(long));
-    pid_sup = G_malloc(Rast_window_cols() * sizeof(long));
+    pid_corr = G_malloc(ad->cl * sizeof(long));
+    pid_sup = G_malloc(ad->cl * sizeof(long));
 
-    for (j = 0; j < Rast_window_cols(); j++) {
+    for (j = 0; j < ad->cl; j++) {
 	pid_corr[j] = 0;
 	pid_sup[j] = 0;
     }
@@ -602,7 +603,7 @@
 
 	connected = 0;
 	for (j = 0; j < ad->cl; j++) {
-	    pid_corr[j + ad->x] = 0;
+	    pid_corr[j] = 0;
 	    
 	    corrCell = buf[j + ad->x];
 	    if (masked && (mask_buf[j] == 0)) {
@@ -623,9 +624,9 @@
 	    }
 
 	    if (!Rast_is_f_null_value(&precCell) && corrCell == precCell) {
-		pid_corr[j + ad->x] = pid_corr[j - 1 + ad->x];
+		pid_corr[j] = pid_corr[j - 1];
 		connected = 1;
-		pst[pid_corr[j + ad->x]].count++;
+		pst[pid_corr[j]].count++;
 	    }
 	    else {
 		connected = 0;
@@ -633,7 +634,7 @@
 
 	    if (!Rast_is_f_null_value(&supCell) && corrCell == supCell) {
 
-		if (pid_corr[j + ad->x] != pid_sup[j + ad->x]) {
+		if (pid_corr[j] != pid_sup[j]) {
 		    /* connect or merge */
 		    /* after r.clump */
 		    if (connected) {
@@ -644,20 +645,20 @@
 			}
 		    }
 
-		    old_pid = pid_corr[j + ad->x];
-		    new_pid = pid_sup[j + ad->x];
-		    pid_corr[j + ad->x] = new_pid;
+		    old_pid = pid_corr[j];
+		    new_pid = pid_sup[j];
+		    pid_corr[j] = new_pid;
 		    if (old_pid > 0) {
 			/* merge */
 			/* update left side of the current row */
 			for (k = 0; k < j; k++) {
-			    if (pid_corr[k + ad->x] == old_pid)
-				pid_corr[k + ad->x] = new_pid;
+			    if (pid_corr[k] == old_pid)
+				pid_corr[k] = new_pid;
 			}
 			/* update right side of the previous row */
 			for (k = j + 1; k < ad->cl; k++) {
-			    if (pid_sup[k + ad->x] == old_pid)
-				pid_sup[k + ad->x] = new_pid;
+			    if (pid_sup[k] == old_pid)
+				pid_sup[k] = new_pid;
 			}
 			pst[new_pid].count += pst[old_pid].count;
 			pst[old_pid].count = 0;
@@ -676,7 +677,7 @@
 		/* start new patch */
 		npatch++;
 		pid++;
-		pid_corr[j + ad->x] = pid;
+		pid_corr[j] = pid;
 
 		if (pid >= nalloc) {
 		    pst = (struct pst *)G_realloc(pst, (pid + incr) * sizeof(struct pst));



More information about the grass-commit mailing list