Your question is poorly stated. It as at once general, as it asks if anyone has ideas about how to implement MFDs, and specific, as it seems to ask what is wrong with you code. I'll try to answer this second conceptualization of your question here.
You claim that the code you've posted is not optimized. I have rewritten the code below as I think you should have written it. The changes I have introduced include making good use of loops and offset arrays. These changes are not optimizations, they are helpful abstractions that allow you to work with the code and review it in meaningful ways. Without such abstractions you are not coding, you are producing functional gibberish.
const int di[10]={0,-1,-1,-1, 0,0,0, 1,1,1};
const int dj[10]={0,-1, 0, 1,-1,0,1,-1,0,1};
static void rainfallAlgo() {
double total, percentage, transfer;
double[] tile = new double[10];
//Consider declaring -500 as a const so that it reads intelligbly
//For instance, you could write "#define NoData -500"
//Then say, "if elev[i,j]!=NoData"
for (int i = 0; i != Convert.ToInt32(totalRow); i++){
for (int j = 0; j != Convert.ToInt32(totalCol); j++){
if ((elev[i, j] != -500)&&(elevrain[i,j]>=elev[i,j])){
Console.WriteLine();
total = 0;
for (int k = 1; k <= 9; k++){
if (k != 5){
tile[k] = compare(i, j, k);
total += tile[k];
}
}
for (int k = 1; k <= 9; k++){
if (total == 0){
Console.WriteLine("I am ZERO");
total = 1;
}
if (k != 5){
percentage = tile[k] / total;
Console.Write(k+ ":");
Console.WriteLine(percentage);
transfer = percentage * (elevrain[i,j]-elev[i,j]);
elevrain[i+di[k],j+dj[k]]+=transfer;
}
}
if(total!=1)
elevrain[i, j] = elev[i, j];
Console.WriteLine(elevrain[i,j]);
}
}
}
}
//This function isn't called from anywhere; I don't know what it does
static void addRain(){
rainfall = new double[Convert.ToInt32(totalRow), Convert.ToInt32(totalCol)];
elevrain = new double[Convert.ToInt32(totalRow), Convert.ToInt32(totalCol)];
for(int i=0;i != Convert.ToInt32(totalRow);++i)
for(int j=0;j != Convert.ToInt32(totalCol);++j){
rainfall[i, j] = 2;
elevrain[i, j] = elev[i, j] + rainfall[i,j];
}
static double compare(int i, int j, int k){
const int dr[9]={0,1.414,0,1.414,0,0,1.414,0,1.414,0,1.414,0};
double total=0;
if ( (elevrain[i, j] > elevrain[ i+di[k], j+dj[k] ] ) && (elev[ i+di[k], j+dj[k] ]!=-500.0) )
total = (elevrain[i, j] - elevrain[ i+di[k], j+dj[k] ]) / dr[k];
return total;
}
With the introduction of the arrays di
and dj
it becomes more reasonable to analyze your code.
I am left wondering why you have chosen to label your central cell as 5
as this makes your loops much more painful. It is better to use 0
as the central label so that you can loop sequentially over all of the neighbouring cells.
I have also used an array dr
to store the offset distances between the centers of the central cell and its neighbours.
I think you will find that the judicious use of arrays such as those I've introduced here will lead to better code and that better code will lead you more quickly to the solutions you seek.
You can also wrap your grids into classes which provide meaningful accessor names to help you follow what's going on. You can take a look at some of my code here and here to get ideas.
Happy coding.
As long as you 'drew' the lines from upstream to downstream in a consequent fashion, you can do this through the styling options.
Right-click the shapefile layer, select Properties. Then select 'style' from the left hand side menu. Where you set the linestyle, on the top right you can choose a symbol line.
Then you can select which symbol (I used the default 'arrow'), and set rotation to 90 degrees to tilt it sideways. You can then adjust intervals, size, .. Note you can also set them to only be on the start or endpoint of a line, in case you made individual lines where you want arrows, rather then replacing a full line with an 'arrow line'.
I applied this on an overview of waterways in my area, with ortho imagery in the back, gives me this result:
Best Answer
Values other than the D8 values indicate that you have sinks in your DEM. Try the work flow shown here..
http://resources.arcgis.com/en/help/main/10.2/index.html#/Deriving_runoff_characteristics/009z0000005p000000/