Bladeren bron

r.li: sync indices based on patch number

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@59171 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 11 jaren geleden
bovenliggende
commit
a40cf73144
3 gewijzigde bestanden met toevoegingen van 177 en 168 verwijderingen
  1. 51 49
      raster/r.li/r.li.mps/mps.c
  2. 76 70
      raster/r.li/r.li.patchdensity/main.c
  3. 50 49
      raster/r.li/r.li.patchnum/main.c

+ 51 - 49
raster/r.li/r.li.mps/mps.c

@@ -52,6 +52,7 @@ int main(int argc, char *argv[])
     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 main(int argc, char *argv[])
 
 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 @@ int meanPatchSize(int fd, char **par, struct area_entry *ad, double *result)
 	    G_fatal_error("data type unknown");
 	    return RLI_ERRORE;
 	}
-
     }
-    if (ris != RLI_OK)
+
+    if (ris != RLI_OK) {
 	return RLI_ERRORE;
+    }
 
     *result = indice;
 
@@ -128,10 +130,10 @@ int calculate(int fd, struct area_entry *ad, double *result)
     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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	    }
 
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 		/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
     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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	    }
 
 	    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 		/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
     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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	    }
 
 	    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 		/* 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));

+ 76 - 70
raster/r.li/r.li.patchdensity/main.c

@@ -79,17 +79,17 @@ int patch_density(int fd, char **par, struct area_entry *ad, double *result)
     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 @@ int calculate(int fd, struct area_entry *ad, double *result)
     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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 		/* 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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	}
     }
 
-    if (npatch > 0) {
+    if (area > 0) {
 	double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
 	double area_units;
 
@@ -309,13 +311,13 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
     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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 		/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	}
     }
 
-    if (npatch > 0) {
+    if (area > 0) {
 	double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
 	double area_units;
 
@@ -532,13 +536,13 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
     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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 		/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	}
     }
 
-    if (npatch > 0) {
+    if (area > 0) {
 	double EW_DIST1, EW_DIST2, NS_DIST1, NS_DIST2;
 	double area_units;
 
@@ -755,13 +761,13 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	/* 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) {

+ 50 - 49
raster/r.li/r.li.patchnum/main.c

@@ -70,6 +70,7 @@ int main(int argc, char *argv[])
 			  output->answer);
 }
 
+
 int patch_number(int fd, char **par, struct area_entry *ad, double *result)
 {
     int ris = RLI_OK;
@@ -78,17 +79,17 @@ int patch_number(int fd, char **par, struct area_entry *ad, double *result)
     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 @@ int calculate(int fd, struct area_entry *ad, double *result)
     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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 	    }
 
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculate(int fd, struct area_entry *ad, double *result)
 		/* 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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
     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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 	    }
 
 	    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculateD(int fd, struct area_entry *ad, double *result)
 		/* 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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
     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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 
 	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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 	    }
 
 	    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 
 	    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 			}
 		    }
 
-		    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 @@ int calculateF(int fd, struct area_entry *ad, double *result)
 		/* 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));