int slope_length(int r, int c, int dr, int dc) { CELL top_alt, bot_alt, ridge; char asp_value; double res, top_ls, bot_ls; WAT_ALT wa; if (sides == 8) { if (r == dr) res = window.ns_res; else if (c == dc) res = window.ew_res; else res = diag; } else { /* sides == 4 */ bseg_get(&asp, &asp_value, dr, dc); if (r == dr) { if (asp_value == 2 || asp_value == 6) res = window.ns_res; else /* asp_value == 4, 8, -2, -4, -6, or -8 */ res = diag; /* how can res be diag with sides == 4??? */ } else { /* c == dc */ if (asp_value == 4 || asp_value == 8) res = window.ew_res; else /* asp_value == 2, 6, -2, -4, -6, or -8 */ res = diag; } } dseg_get(&s_l, &top_ls, r, c); if (top_ls == half_res) top_ls = res; else top_ls += res; dseg_put(&s_l, &top_ls, r, c); seg_get(&watalt, (char *) &wa, r, c); top_alt = wa.ele; seg_get(&watalt, (char *) &wa, dr, dc); bot_alt = wa.ele; if (top_alt > bot_alt) { dseg_get(&s_l, &bot_ls, dr, dc); if (top_ls > bot_ls) { bot_ls = top_ls + res; dseg_put(&s_l, &bot_ls, dr, dc); cseg_get(&r_h, &ridge, r, c); cseg_put(&r_h, &ridge, dr, dc); } } return 0; }
int sg_factor(void) { int r, c; CELL low_elev, hih_elev; double height, length, S, sin_theta; WAT_ALT wa; ASP_FLAG af; G_message(_("SECTION 5: RUSLE LS and/or S factor determination.")); for (r = nrows - 1; r >= 0; r--) { G_percent(nrows - r, nrows, 3); for (c = ncols - 1; c >= 0; c--) { seg_get(&aspflag, (char *)&af, r, c); if (FLAG_GET(af.flag, NULLFLAG)) continue; seg_get(&watalt, (char *) &wa, r, c); low_elev = wa.ele; cseg_get(&r_h, &hih_elev, r, c); dseg_get(&s_l, &length, r, c); height = 1.0 * (hih_elev - low_elev) / ele_scale; if (length > max_length) { height *= max_length / length; length = max_length; } sin_theta = height / sqrt(height * height + length * length); if (height / length < .09) S = 10.8 * sin_theta + .03; else S = 16.8 * sin_theta - .50; if (ls_flag) { length *= METER_TO_FOOT; len_slp_equ(length, sin_theta, S, r, c); } if (sg_flag) { dseg_put(&s_g, &S, r, c); } } } G_percent(nrows, nrows, 1); /* finish it */ return 0; }
int len_slp_equ(double slope_length, double sin_theta, double S, int r, int c) { double rill, s_l_exp, /* m */ rill_ratio, /* Beta */ LS; rill_ratio = (sin_theta / 0.0896) / (3.0 * pow(sin_theta, 0.8) + 0.56); if (ril_flag) { dseg_get(&ril, &rill, r, c); } else if (ril_value >= 0.0) { rill = ril_value; } else rill = 0.0; /* rill_ratio equation from Steve Warren */ rill_ratio *= .5 + .005 * rill + .0001 * rill * rill; s_l_exp = rill_ratio / (1 + rill_ratio); LS = S * pow((slope_length / 72.6), s_l_exp); dseg_put(&l_s, &LS, r, c); return 0; }
int find_con(int r, int c, double *d1, double *d2, DCELL * con1, DCELL * con2) { int ct, low_ct, node_ct; int rr, cc, dor, doc; double dd, shortest; DCELL value; G_set_d_null_value(con1, 1); G_set_d_null_value(con2, 1); *d1 = *d2 = 1.0; shortest = nrows * ncols; for (rr = minr; rr <= maxr; rr++) { for (cc = minc; cc <= maxc; cc++) FLAG_UNSET(seen, rr, cc); } minr = nrows; minc = ncols; maxr = maxc = -1; FLAG_SET(seen, r, c); if (r < minr) minr = r; if (r > maxr) maxr = r; if (c < minc) minc = c; if (c > maxc) maxc = c; node_ct = 0; zero = addpts(zero, r, c, r, c, &node_ct); low_ct = 0; while (1) { ct = low_ct++; if (node_ct <= ct) return 1; rr = zero[ct].r; cc = zero[ct].c; dor = ABS(rr - r); doc = ABS(cc - c); if (rr >= 0 && cc >= 0 && rr < nrows && cc < ncols && zero[ct].d < shortest && !flag_get(mask, rr, cc)) { dseg_get(&con, rr, cc, &value); if (G_is_d_null_value(&value)) zero = addpts(zero, r, c, rr, cc, &node_ct); else if (G_is_d_null_value(con1)) { *con1 = value; *d1 = MIN(dor, doc) * 1.414 + ABS(dor - doc); shortest = *d1 * 2.0 * i_val_l_f; } else if (*con1 == value) { dd = MIN(dor, doc) * 1.414 + ABS(dor - doc); if (dd < *d1) { *d1 = dd; shortest = dd * 2.0 * i_val_l_f; } } else if (G_is_d_null_value(con2)) { *con2 = value; *d2 = MIN(dor, doc) * 1.414 + ABS(dor - doc); shortest = *d2; } else { dd = MIN(dor, doc) * 1.414 + ABS(dor - doc); shortest = MIN(shortest, dd); } } } return 0; }