Exemplo n.º 1
0
// Sets only flowDir only where there is a positive slope
// Returns number of cells which are flat
int setPosDir(linearpart<float>& elevDEM, linearpart<short>& flowDir)
{
    double dxA = elevDEM.getdxA();
    double dyA = elevDEM.getdyA();
    int nx = elevDEM.getnx();
    int ny = elevDEM.getny();
    int numFlat = 0;

    double tempdxc, tempdyc;

    fact = new double*[ny]; // initialize 2d array by Nazmus 2/16

    for(int m = 0; m<ny; m++)
        fact[m] = new double[9];

    for (int m = 0; m<ny; m++) {
        for (int k = 1; k <= 8; k++) {
            elevDEM.getdxdyc(m, tempdxc, tempdyc);
            fact[m][k] = (double) (1./sqrt(d1[k]*d1[k]*tempdxc*tempdxc + d2[k]*d2[k]*tempdyc*tempdyc));
        }
    }

    for (int j = 0; j < ny; j++) {
        for (int i=0; i < nx; i++) {
            //FlowDir is nodata if it is on the border OR elevDEM has no data
            if (elevDEM.isNodata(i,j) || !elevDEM.hasAccess(i-1,j) || !elevDEM.hasAccess(i+1,j) ||
                    !elevDEM.hasAccess(i,j-1) || !elevDEM.hasAccess(i,j+1)) {
                //do nothing
                continue;
            }

            //Check if cell is "contaminated" (neighbors have no data)
            //  set flowDir to noData if contaminated
            bool contaminated = false;
            for (int k=1; k<=8; k++) {
                int in=i+d1[k];
                int jn=j+d2[k];

                if (elevDEM.isNodata(in,jn)) {
                    contaminated = true;
                    break;
                }
            }

            if (contaminated) {
                flowDir.setToNodata(i,j);
            } else {
                // If cell is not contaminated,
                flowDir.setData(i, j, 0);
                setFlow(i,j, flowDir, elevDEM);

                if (flowDir.getData(i,j) == 0) {
                    numFlat++;
                }
            }
        }
    }

    return numFlat;
}
long setPosDirDinf(linearpart<float>& elevDEM, linearpart<float>& flowDir, linearpart<float>& slope, int useflowfile) {
    double dxA = elevDEM.getdxA();
    double dyA = elevDEM.getdyA();
    long nx = elevDEM.getnx();
    long ny = elevDEM.getny();
    float tempFloat;
    double tempdxc, tempdyc;
    int i, j, k, in, jn, con;
    long numFlat = 0;

    tempFloat = 0;
    for (j = 0; j < ny; j++) {
        for (i = 0; i < nx; i++) {


            //FlowDir is nodata if it is on the border OR elevDEM has no data
            if (elevDEM.isNodata(i, j) || !elevDEM.hasAccess(i - 1, j) || !elevDEM.hasAccess(i + 1, j) ||
                    !elevDEM.hasAccess(i, j - 1) || !elevDEM.hasAccess(i, j + 1)) {
                //do nothing			
            } else {
                //Check if cell is "contaminated" (neighbors have no data)
                //  set flowDir to noData if contaminated
                con = 0;
                for (k = 1; k <= 8 && con != -1; k++) {
                    in = i + d1[k];
                    jn = j + d2[k];
                    if (elevDEM.isNodata(in, jn)) con = -1;
                }
                if (con == -1) 
                    flowDir.setToNodata(i, j);
                    //If cell is not contaminated,
                else {
                    tempFloat = -1.;
                    flowDir.setData(i, j, tempFloat); //set to -1
                    elevDEM.getdxdyc(j, tempdxc, tempdyc);


                    float DXX[3] = {0, tempdxc, tempdyc}; //tardemlib.cpp ln 1291
                    float DD = sqrt(tempdxc * tempdxc + tempdyc * tempdyc); //tardemlib.cpp ln 1293
                    SET2(j, i, DXX, DD, elevDEM, flowDir, slope); //i=y in function form old code j is x switched on purpose
                    //  Use SET2 from serial code here modified to get what it has as felevg.d from elevDEM partition
                    //  Modify to return 0 if there is a 0 slope.  Modify SET2 to output flowDIR as no data (do nothing 
                    //  if verified initialization to nodata) and 
                    //  slope as 0 if a positive slope is not found

                    //setFlow( i,j, flowDir, elevDEM, area, useflowfile);
                    if (flowDir.getData(i, j, tempFloat) == -1)
                        numFlat++;
                }
            }
        }
    }
    return numFlat;
}
void SET2(int I, int J, float *DXX, float DD, linearpart<float>& elevDEM, linearpart<float>& flowDir, linearpart<float>& slope) {
    double dxA = elevDEM.getdxA();
    double dyA = elevDEM.getdyA();
    float SK[9];
    float ANGLE[9];
    float SMAX;
    float tempFloat;
    int K;
    int KD;

    int ID1[] = {0, 1, 2, 2, 1, 1, 2, 2, 1};
    int ID2[] = {0, 2, 1, 1, 2, 2, 1, 1, 2};
    int I1[] = {0, 0, -1, -1, 0, 0, 1, 1, 0};
    int I2[] = {0, -1, -1, -1, -1, 1, 1, 1, 1};
    int J1[] = {0, 1, 0, 0, -1, -1, 0, 0, 1};
    int J2[] = {0, 1, 1, -1, -1, -1, -1, 1, 1};
    float ANGC[] = {0, 0., 1., 1., 2., 2., 3., 3., 4.};
    float ANGF[] = {0, 1., -1., 1., -1., 1., -1., 1., -1.};


    for (K = 1; K <= 8; K++) {
        VSLOPE(
                elevDEM.getData(J, I, tempFloat), //felevg.d[J][I],
                elevDEM.getData(J + J1[K], I + I1[K], tempFloat), //[felevg.d[J+J1[K]][I+I1[K]],
                elevDEM.getData(J + J2[K], I + I2[K], tempFloat), //felevg.d[J+J2[K]][I+I2[K]],
                DXX[ID1[K]],
                DXX[ID2[K]],
                DD,
                &SK[K],
                &ANGLE[K]
                );
    }
    tempFloat = -1;
    SMAX = 0.;
    KD = 0;
    flowDir.setData(J, I, tempFloat); //USE -1 TO INDICATE DIRECTION NOT YET SET 
    for (K = 1; K <= 8; K++) {
        if (SK[K] > SMAX) {
            SMAX = SK[K];
            KD = K;
        }
    }

    if (KD > 0) {
        tempFloat = (float) (ANGC[KD]*(PI / 2) + ANGF[KD] * ANGLE[KD]);
        flowDir.setData(J, I, tempFloat); //set to angle
    }
    slope.setData(J, I, SMAX);
}