|
@@ -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) {
|