import java.awt.*; import java.io.*; import java.lang.*; class Complete { // Copyleft Harvey Greenberg, UW hgreen@u.washington.edu Fri Jan 24 09:49 1997 // Error handling is neither complete nor elegant // calls class Bil // Makes graphic calls only if arguments are omitted // Imagine a landscape represented by a lattice, on which the position of // streams has been calculated by a method such as contributing area times // slope squared is greater than a threshold. The river network would seem // to be incomplete, fading out in flat or divergent areas. Assuming that // the streams are continuous, we want to trace the water downhill to complete // the network. // The first argument is the root name of a flowdirection bil (see class Bil) // file. It follows the old TOPMODEL/ARCINFO convention that 1 = to the right // (column++), 2 = right and down (column++,row++), 4 = down ... 128 = right // and up. The second argument is the root name of a byte file in which // river cells are integers between 1 and 127, and the background is 0. // If the arguments are omitted, you can use a widget to enter them. // You may click on the hdr or bil file; the extension gets stripped off. // The output file is a representation of the complete river network, with // all cells coded 1. The original river file is overwritten. public static void main (String args[]) throws IOException { Bil valgrd,dirgrd,rivgrd; int idot,nrows,ncols,nrowsm1,ncolsm1,fixes,dir,r=0,c=0; String flowroot, rivroot, fullname,fullname2; Frame window = new Frame(); System.out.println("Complete: version 1.0"); if (args.length < 1){ System.out.println("Select a direction bil file: "); FileDialog fd = new FileDialog(window,"Direction file"); fd.show(); fullname = fd.getDirectory() + fd.getFile(); idot = fullname.lastIndexOf("."); flowroot = fullname.substring(0,idot); } else flowroot = args[0]; if (args.length < 2){ System.out.println("Select a stream bil file (IT WILL BE OVERWRITTEN): "); FileDialog fd2 = new FileDialog(window,"Stream file"); fd2.show(); fullname2 = fd2.getDirectory() + fd2.getFile(); idot = fullname2.lastIndexOf("."); rivroot = fullname2.substring(0,idot); } else rivroot = args[1]; dirgrd = new Bil(flowroot); rivgrd = new Bil(rivroot); nrows = dirgrd.nr; ncols = dirgrd.nc; if(nrows != rivgrd.nr || (ncols != rivgrd.nc)){ System.out.println("ERROR: rows and columns do not match: " + nrows + "×" + ncols + " v. " + rivgrd.nr + "×" + rivgrd.nc); return; } if(dirgrd.nb != 8 || rivgrd.nb != 8){ System.out.println("ERROR: This class reqires that both input files be byte arrays"); return; } nrowsm1 = nrows - 1; ncolsm1 = ncols - 1; for(int i=0;i < nrows;i++){ for(int j=0;j < ncols;j++){ if (rivgrd.barray[i][j] > 0){ // We assume all river cells are > 0 r = i; c = j; tracy: while(true){ //Trace to sink or edge of grid dir = dirgrd.barray[r][c]; // We assume topmodel/arcinfo coding. rivgrd.barray[r][c] = -1; // Flag as done. switch(dir){ case 1: // check grid bounds and increment column if(c++ == ncolsm1) break tracy; break; case 2: if(c++ == ncolsm1 | r++ == nrowsm1) break tracy; break; case 4: if(r++ == nrowsm1) break tracy; break; case 8: if(c-- == 0 | r++ == nrowsm1) break tracy; break; case 16: if(c-- == 0 ) break tracy; break; case 32: if(c-- == 0 | r-- == 0) break tracy; break; case 64: if( r-- == 0) break tracy; break; case -128: // arcinfo unsigned value is read as signed if(c++ == ncolsm1 | r-- == 0) break tracy; break; default: System.out.println("flat: " + r + "," + c); } if(rivgrd.barray[r][c] == -1) break; } // while tracing from one source } // if a cell to trace from } // i } // j for(int i=0;i < nrows;i++){ for(int j=0;j < ncols;j++){ if(rivgrd.barray[i][j] == -1) rivgrd.barray[i][j] = 1; // 1 is a nicer flag than -1 } // i } // j rivgrd.write(rivroot + ".bil"); // overwrite the bil file } // main } // class Complete