//--------------------------------------------------------- // Runs on module execution. //--------------------------------------------------------- bool CExercise_04::On_Execute(void) { CSG_Grid *grid, *result, *accumulation; grid = Parameters("GRID")->asGrid(); result = Parameters("RESULT")->asGrid(); accumulation = Parameters("ACC")->asGrid(); accumulation->Assign(1.0); for (int i = 0; i < Get_NCells() && Set_Progress_NCells(i); i++) { int x, y; if (grid->Get_Sorted(i, x, y)) { int direction = Get_Flow_Direction(grid, x, y); if (direction > -1) { result->Set_Value(x, y, direction); int destX = accumulation->Get_System().Get_xTo(direction, x); int destY = accumulation->Get_System().Get_yTo(direction, y); accumulation->Set_Value(destX, destY, accumulation->asDouble(destX, destY) + accumulation->asDouble(x, y)); } } } result->Set_Name("Flow Direction Map"); return true; }
//--------------------------------------------------------- bool CViGrA_Morphology::On_Execute(void) { bool bRescale; int Type, Radius; double Rank; CSG_Grid *pInput, *pOutput, Rescaled; pInput = Parameters("INPUT") ->asGrid(); pOutput = Parameters("OUTPUT") ->asGrid(); Type = Parameters("TYPE") ->asInt(); Radius = Parameters("RADIUS") ->asInt(); Rank = Parameters("RANK") ->asDouble(); bRescale = Parameters("RESCALE") ->asBool(); //----------------------------------------------------- if( bRescale ) { Rescaled.Create(*Get_System(), SG_DATATYPE_Byte); for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++) { Rescaled.Set_Value(i, 0.5 + (pInput->asDouble(i) - pInput->Get_ZMin()) * 255.0 / pInput->Get_ZRange()); } pInput = &Rescaled; } //----------------------------------------------------- vigra::BImage Input, Output(Get_NX(), Get_NY()); Copy_Grid_SAGA_to_VIGRA(*pInput, Input, true); switch( Type ) { case 0: // Dilation discDilation (srcImageRange(Input), destImage(Output), Radius); break; case 1: // Erosion discErosion (srcImageRange(Input), destImage(Output), Radius); break; case 2: // Median discMedian (srcImageRange(Input), destImage(Output), Radius); break; case 3: // User defined rank discRankOrderFilter (srcImageRange(Input), destImage(Output), Radius, Rank); break; } //----------------------------------------------------- Copy_Grid_VIGRA_to_SAGA(*pOutput, Output, false); pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pInput->Get_Name(), Get_Name().c_str())); return( true ); }
//--------------------------------------------------------- bool CTopographic_Correction::Get_Model(void) { //----------------------------------------------------- m_pOriginal = Parameters("ORIGINAL") ->asGrid(); m_pCorrected = Parameters("CORRECTED") ->asGrid(); m_pCorrected ->Set_Name(CSG_String::Format(SG_T("%s [%s]"), m_pOriginal->Get_Name(), _TL("Topographic Correction"))); m_Method = Parameters("METHOD") ->asInt(); m_Minnaert = Parameters("MINNAERT") ->asDouble(); switch( Parameters("MAXVALUE")->asInt() ) { default: m_maxValue = 255; break; case 1: m_maxValue = 65535; break; } switch( m_Method ) { //----------------------------------------------------- case 5: // C Correction { Process_Set_Text(_TL("Regression Analysis")); CSG_Regression R; sLong n = Parameters("MAXCELLS")->asInt(); int nStep = Get_NCells() < n ? 1 : (int)(Get_NCells() / n); for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n+=nStep) { R.Add_Values(m_pOriginal->asDouble(n), m_Illumination.asDouble(n)); } if( !R.Calculate() || !R.Get_Constant() ) { return( false ); } m_C = R.Get_Coefficient() / R.Get_Constant(); Message_Add(R.asString()); } break; //----------------------------------------------------- case 6: // Normalization (after Civco, modified by Law & Nichol) { m_C = 1.0; } break; } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CSADO_SolarRadiation::Get_Shade(double Decline, double Azimuth) { //----------------------------------------------------- m_Shade.Assign(0.0); if( !m_bBending ) { int i, x, y; double dx, dy, dz; Get_Shade_Params(Decline, Azimuth, dx, dy, dz); for(i=0; i<Get_NCells() && Set_Progress_NCells(i); i++) { if( m_pDEM->Get_Sorted(i, x, y) && !Get_Shade_Complete(x, y) ) { Set_Shade(x, y, dx, dy, dz); } } } //----------------------------------------------------- else { int i, x, y, iLock; for(i=0, iLock=1; i<Get_NCells() && Set_Progress_NCells(i); i++, iLock++) { if( m_pDEM->Get_Sorted(i, x, y) && !Get_Shade_Complete(x, y) ) { if( iLock >= 255 ) iLock = 1; if( iLock == 1 ) Lock_Create(); Set_Shade_Bended(x, y, iLock); } } } return( true ); }
//--------------------------------------------------------- bool CFlow_Distance::On_Execute(void) { bool bSeeds; int x, y, Method; CSG_Grid *pSeed; //------------------------------------------------- m_pDTM = Parameters("ELEVATION") ->asGrid(); pSeed = Parameters("SEED") ->asGrid(); m_pLength = Parameters("LENGTH") ->asGrid(); m_Converge = Parameters("CONVERGENCE") ->asDouble(); bSeeds = Parameters("SEEDS_ONLY") ->asBool(); Method = Parameters("METHOD") ->asInt(); m_pWeight = SG_Create_Grid(m_pLength, SG_DATATYPE_Float); m_pWeight ->Assign(0.0); m_pLength ->Assign(0.0); //------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { m_pDTM->Get_Sorted(n, x, y, true, false); if( pSeed && !pSeed->is_NoData(x, y) ) { m_pLength->Set_Value(x, y, 0.0); m_pWeight->Set_Value(x, y, 0.0); } else if( m_pWeight->asDouble(x, y) > 0.0 ) { m_pLength->Set_Value(x, y, m_pLength->asDouble(x, y) / m_pWeight->asDouble(x, y)); } else if( bSeeds ) { m_pLength->Set_NoData(x, y); continue; } switch( Method ) { case 0: Set_Length_D8 (x, y); break; case 1: Set_Length_MFD (x, y); break; } } //------------------------------------------------- delete(m_pWeight); DataObject_Set_Colors(m_pLength, 100, SG_COLORS_WHITE_BLUE); return( true ); }
//--------------------------------------------------------- bool CMelton_Ruggedness::On_Execute(void) { CSG_Grid *pDEM, *pArea, *pMRN, *pZMax; //----------------------------------------------------- pDEM = Parameters("DEM" )->asGrid(); pArea = Parameters("AREA")->asGrid(); pZMax = Parameters("ZMAX")->asGrid(); pMRN = Parameters("MRN" )->asGrid(); if( !pDEM->Set_Index() ) { Error_Set(_TL("index creation failed")); return( false ); } pArea->Set_NoData_Value(0.0); pArea->Assign_NoData(); pZMax->Assign_NoData(); pMRN ->Assign_NoData(); //------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y, i, ix, iy; if( pDEM->Get_Sorted(n, x, y, true, true) ) { pArea->Add_Value(x, y, Get_Cellsize()); if( pZMax->is_NoData(x, y) ) { pZMax->Set_Value(x, y, pDEM->asDouble(x, y)); } if( (i = pDEM->Get_Gradient_NeighborDir(x, y, true)) >= 0 && Get_System().Get_Neighbor_Pos(i, x, y, ix, iy) ) { pArea->Add_Value(ix, iy, pArea->asDouble(x, y)); if( pZMax->is_NoData(ix, iy) || pZMax->asDouble(ix, iy) < pZMax->asDouble(x, y) ) { pZMax->Set_Value(ix, iy, pZMax->asDouble(x, y)); } } pMRN->Set_Value(x, y, (pZMax->asDouble(x, y) - pDEM->asDouble(x, y)) / sqrt(pArea->asDouble(x, y))); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CFlow_by_Slope::On_Execute(void) { m_Slope_Min = Parameters("SLOPE_MIN")->asDouble() * M_DEG_TO_RAD; m_Slope_Max = Parameters("SLOPE_MAX")->asDouble() * M_DEG_TO_RAD; if( m_Slope_Max <= 0.0 ) { Error_Set(_TL("slope threshold must not be zero!")); return( false ); } if( Parameters("B_FLOW")->asBool() ) { m_Flow_Min = Parameters("T_FLOW")->asRange()->Get_LoVal() * Get_Cellarea(); m_Flow_Max = Parameters("T_FLOW")->asRange()->Get_HiVal() * Get_Cellarea(); } else { m_Flow_Min = m_Flow_Max = 0.0; } //----------------------------------------------------- m_pDEM = Parameters("DEM" )->asGrid(); m_pFlow = Parameters("FLOW" )->asGrid(); m_pFlow->Assign(Get_Cellarea()); if( Parameters("WEIGHT")->asGrid() ) { m_pFlow->Multiply(*Parameters("WEIGHT")->asGrid()); } DataObject_Set_Colors(m_pFlow, 11, SG_COLORS_WHITE_BLUE, false); //----------------------------------------------------- for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++) { int x, y; if( !m_pDEM->Get_Sorted(i, x, y, true) || m_pDEM->is_NoData(x, y) ) { m_pFlow->Set_NoData(x, y); } else { Set_Area(x, y); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CSlopeLength::On_Execute(void) { int x, y; //----------------------------------------------------- m_pDEM = Parameters("DEM" )->asGrid(); m_pLength = Parameters("LENGTH")->asGrid(); if( !m_pDEM->Set_Index() ) { Error_Set(_TL("index creation failed")); return( false ); } //----------------------------------------------------- m_Slope.Create(*Get_System()); for(y=0; y<Get_NY() && Set_Progress(y); y++) { #pragma omp parallel for private(x) for(x=0; x<Get_NX(); x++) { double Slope, Aspect; if( m_pDEM->Get_Gradient(x, y, Slope, Aspect) ) { m_Slope .Set_Value(x, y, Slope); m_pLength->Set_Value(x, y, 0.0); } else { m_Slope .Set_NoData(x, y); m_pLength->Set_NoData(x, y); } } } //----------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { if( m_pDEM->Get_Sorted(n, x, y) ) { Get_Length(x, y); } } //----------------------------------------------------- m_Slope.Destroy(); return( true ); }
//--------------------------------------------------------- bool CDiffuse_Pollution_Risk::Set_Flow(void) { Process_Set_Text(_TL("initialization")); CSG_Grid *pWeight = Parameters("WEIGHT")->asGrid (); double Weight = Parameters("WEIGHT")->asDouble(); CSG_Grid *pRain = Parameters("RAIN" )->asGrid (); double Rain = Parameters("RAIN" )->asDouble(); m_FlowDir.Create(*Get_System(), SG_DATATYPE_Char); m_RainAcc.Create(*Get_System()); m_TWI .Create(*Get_System()); for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; if( !m_pDEM->Get_Sorted(n, x, y, true) || (pRain && pRain->is_NoData(x, y)) || !Set_Flow(x, y, pRain ? pRain->asDouble(x, y) : Rain) ) { m_FlowDir .Set_NoData(x, y); m_RainAcc .Set_NoData(x, y); m_TWI .Set_NoData(x, y); m_pRisk_Point->Set_NoData(x, y); } else { double s, a; m_pDEM->Get_Gradient(x, y, s, a); s = tan(s); // tangens of slope a = (fabs(sin(a)) + fabs(cos(a))) * Get_Cellsize(); // flow width double SCA = m_RainAcc.asDouble(x, y) / a; // rain * specific catchment area m_TWI.Set_Value(x, y, log(SCA / (s < M_ALMOST_ZERO ? M_ALMOST_ZERO : s))); if( pWeight && pWeight->is_NoData(x, y) ) { m_pRisk_Point->Set_NoData(x, y); } else { m_pRisk_Point->Set_Value(x, y, SCA * s * (pWeight ? pWeight->asDouble(x, y) : Weight)); // Point Scale Risk Calculation according to Milledge et al. 2012 } } } return( true ); }
//--------------------------------------------------------- void CExercise_14::Find_Channels(void) { int n, x, y; for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { if( m_pDTM->Get_Sorted(n, x, y, true) && m_pChnl->asInt(x, y) == SPRING ) { m_pChnl ->Set_Value(x, y, SPRING); Find_Channels(x, y); } } }
//--------------------------------------------------------- // Calculation according to Milledge et al. 2012 //--------------------------------------------------------- bool CDiffuse_Pollution_Risk::Get_Risk_Diffuse(void) { Process_Set_Text(_TL("Difuse Pollution Risk")); m_pRisk_Diffuse->Assign(0.0); //----------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; if( !m_pDEM->Get_Sorted(n, x, y, true) || m_pDelivery->is_NoData(x, y) || m_pRisk_Point->is_NoData(x, y) || m_RainAcc.asDouble(x, y) <= 0.0 ) { m_pRisk_Diffuse->Set_NoData(x, y); } else { double d[8], Risk; m_pRisk_Point->Mul_Value(x, y, m_pDelivery->asDouble(x, y)); // locational risk = generation risk * connection risk Risk = m_pRisk_Diffuse->asDouble(x, y) + m_pRisk_Point->asDouble(x, y); // risk load = sum of upslope locational risk m_pRisk_Diffuse->Set_Value(x, y, Risk / m_RainAcc.asDouble(x, y)); // risk concentration = risk load / sum of upslope rain if( m_bSingle ) { int i = !m_FlowDir.is_NoData(x, y) ? m_FlowDir.asInt(x, y) : -1; if( i > 0 && m_pDEM->is_InGrid(Get_xTo(i, x), Get_yTo(i, y)) ) { m_pRisk_Diffuse->Add_Value(Get_xTo(i, x), Get_yTo(i, y), Risk); } } else if( Get_Flow_Proportions(x, y, d) ) { for(int i=0; i<8; i++) { if( d[i] > 0.0 ) { m_pRisk_Diffuse->Add_Value(Get_xTo(i, x), Get_yTo(i, y), Risk * d[i]); } } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CRGA_Basic::On_Execute(void) { bool bRefresh; int x, y, i, Segment; CSG_Grid *pSeeds; //----------------------------------------------------- m_pSegments = Parameters("SEGMENTS" )->asGrid(); m_pFeatures = Parameters("FEATURES" )->asGridList(); m_nFeatures = m_pFeatures->Get_Count(); pSeeds = Parameters("SEEDS" )->asGrid(); m_pSeeds = Parameters("TABLE" )->asTable(); m_pSimilarity = Parameters("SIMILARITY")->asGrid(); m_dNeighbour = Parameters("NEIGHBOUR" )->asInt() == 0 ? 2 : 1; m_Var_1 = SG_Get_Square(Parameters("SIG_1")->asDouble()); m_Var_2 = SG_Get_Square(Parameters("SIG_2")->asDouble()); m_Threshold = Parameters("THRESHOLD" )->asDouble(); m_bNormalize = Parameters("NORMALIZE" )->asBool(); m_Method = Parameters("METHOD" )->asInt(); bRefresh = Parameters("REFRESH" )->asBool(); //----------------------------------------------------- m_pSegments ->Assign(-1); m_pSegments ->Set_NoData_Value(-1); m_pSimilarity ->Assign(-1); m_pSimilarity ->Set_NoData_Value(-1); //----------------------------------------------------- m_pSeeds->Destroy(); m_pSeeds->Add_Field(_TL("ID" ), SG_DATATYPE_Int); m_pSeeds->Add_Field(_TL("AREA"), SG_DATATYPE_Double); m_pSeeds->Add_Field(_TL("X" ), SG_DATATYPE_Double); m_pSeeds->Add_Field(_TL("Y" ), SG_DATATYPE_Double); for(i=0; i<m_pFeatures->Get_Count(); i++) { m_pSeeds->Add_Field(m_pFeatures->asGrid(i)->Get_Name(), SG_DATATYPE_Double); } m_Candidates.Create(Parameters("LEAFSIZE")->asInt()); //----------------------------------------------------- for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( !pSeeds->is_NoData(x, y) ) { CSG_Table_Record *pRec = m_pSeeds->Add_Record(); pRec->Set_Value(0, m_pSeeds->Get_Count() - 1); pRec->Set_Value(SEEDFIELD_X, x); pRec->Set_Value(SEEDFIELD_Y, y); for(i=0; i<m_pFeatures->Get_Count(); i++) { pRec->Set_Value(SEEDFIELD_Z + i, Get_Feature(x, y, i)); } m_pSimilarity->Set_Value(x, y, 1.0); Add_To_Segment(x, y, m_pSeeds->Get_Count() - 1); } } } //----------------------------------------------------- if( m_pSeeds->Get_Count() > 1 ) { sLong n = 0; while( n++ < Get_NCells() && Set_Progress_NCells(n) && Get_Next_Candidate(x, y, Segment) ) { Add_To_Segment(x, y, Segment); if( bRefresh && (n % Get_NX()) == 0 ) { DataObject_Update(m_pSegments, 0, m_pSeeds->Get_Count()); Process_Set_Text(CSG_String::Format(SG_T("%.2f"), 100.0 * m_Candidates.Get_Count() / Get_NCells())); } } m_Candidates.Destroy(); return( true ); } //----------------------------------------------------- m_Candidates.Destroy(); return( false ); }
//--------------------------------------------------------- bool CGridsFromTableAndGrid::On_Execute(void) { int iField, iRecord, iAttribute, nAttributes, *Attribute; sLong iCell, jCell; CSG_Parameter_Grid_List *pGrids; CSG_Grid *pClasses; CSG_Table *pTable; //----------------------------------------------------- pClasses = Parameters("CLASSES" )->asGrid(); pGrids = Parameters("GRIDS" )->asGridList(); pTable = Parameters("TABLE" )->asTable(); iField = Parameters("ID_FIELD")->asInt(); pGrids->Del_Items(); if( !pClasses->Set_Index() ) { Error_Set(_TL("index creation failed")); return( false ); } //----------------------------------------------------- if( pTable->Get_Field_Count() == 0 || pTable->Get_Count() == 0 ) { Message_Add(_TL("selected table contains no valid records")); return( false ); } //----------------------------------------------------- if( !pTable->Set_Index(iField, TABLE_INDEX_Ascending) ) { Message_Add(_TL("failed to create index for table")); return( false ); } //----------------------------------------------------- Attribute = new int[pTable->Get_Field_Count()]; for(iAttribute=0, nAttributes=0; iAttribute<pTable->Get_Field_Count(); iAttribute++) { if( iAttribute != iField && pTable->Get_Field_Type(iAttribute) != SG_DATATYPE_String ) { Attribute[nAttributes++] = iAttribute; CSG_Grid *pGrid = SG_Create_Grid(*Get_System()); pGrid->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pClasses->Get_Name(), pTable->Get_Field_Name(iAttribute))); pGrids->Add_Item(pGrid); } } if( nAttributes == 0 ) { delete[](Attribute); Message_Add(_TL("selected table does not have numeric attributes")); return( false ); } //----------------------------------------------------- CSG_Table_Record *pRecord = pTable->Get_Record_byIndex(0); for(iCell=0, iRecord=0; iCell<Get_NCells() && pRecord && Set_Progress_NCells(iCell); iCell++) { if( pClasses->Get_Sorted(iCell, jCell, false, true) ) { double valClass = pClasses->asDouble(jCell); while( pRecord && pRecord->asDouble(iField) < valClass ) { pRecord = pTable->Get_Record_byIndex(++iRecord); } if( !pRecord || pRecord->asDouble(iField) > valClass ) { for(iAttribute=0; iAttribute<nAttributes; iAttribute++) { pGrids->asGrid(iAttribute)->Set_NoData(jCell); } } else { for(iAttribute=0; iAttribute<nAttributes; iAttribute++) { pGrids->asGrid(iAttribute)->Set_Value(jCell, pRecord->asDouble(Attribute[iAttribute])); } } } } //----------------------------------------------------- delete[](Attribute); return true; }
//--------------------------------------------------------- bool CChannelNetwork_Distance::On_Execute(void) { //----------------------------------------------------- m_pDEM = Parameters("ELEVATION")->asGrid(); m_pChannels = Parameters("CHANNELS" )->asGrid(); m_pRoute = Parameters("ROUTE" )->asGrid(); m_pDistance = Parameters("DISTANCE" )->asGrid(); m_pDistVert = Parameters("DISTVERT" )->asGrid(); m_pDistHorz = Parameters("DISTHORZ" )->asGrid(); m_pTime = Parameters("TIME" )->asGrid(); m_pSDR = Parameters("SDR" )->asGrid(); m_Flow_B = Parameters("FLOW_B" )->asDouble(); m_Flow_K = Parameters("FLOW_K" )->asDouble(); m_Flow_R = Parameters("FLOW_R" )->asDouble(); m_pFlow_K = Parameters("FLOW_K" )->asGrid(); m_pFlow_R = Parameters("FLOW_R" )->asGrid(); m_pFields = Parameters("FIELDS" )->asGrid(); m_pPasses = Parameters("PASSES" )->asGrid(); //----------------------------------------------------- m_pDistance->Assign_NoData(); m_pDistVert->Assign_NoData(); m_pDistHorz->Assign_NoData(); if( m_pTime ) { m_pTime->Assign_NoData(); m_pTime->Set_Unit("hours"); } if( m_pSDR ) { m_pSDR ->Assign_NoData(); } if( m_pFields && m_pPasses ) { m_pPasses->Set_NoData_Value(-1.0); m_pPasses->Assign_NoData(); } if( !m_pDEM->Set_Index() ) { Error_Set(_TL("index creation failed")); return( false ); } //----------------------------------------------------- int Method = Parameters("METHOD")->asInt(); bool bBoundary = Parameters("BOUNDARY")->asBool(); for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; if( m_pDEM->Get_Sorted(n, x, y, false) ) // ascending, only valid dem cells { if( is_Channel(x, y, bBoundary) ) { m_pDistance->Set_Value(x, y, 0.0); m_pDistVert->Set_Value(x, y, 0.0); m_pDistHorz->Set_Value(x, y, 0.0); if( m_pTime ) m_pTime ->Set_Value(x, y, 0.0); if( m_pFields ) m_pPasses->Set_Value(x, y, 0.0); } else // if( m_pDEM->is_InGrid(x, y) ) { switch( Method ) { default: Set_D8 (x, y); break; case 1: Set_MFD(x, y); break; } if( m_pSDR && !m_pTime->is_NoData(x, y) ) { m_pSDR->Set_Value(x, y, exp(-m_Flow_B * m_pTime->asDouble(x, y))); } } } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGrid_Fill::On_Execute_Position(CSG_Point ptWorld, TSG_Tool_Interactive_Mode Mode) { //----------------------------------------------------- if( Mode == TOOL_INTERACTIVE_LDOWN ) { int x, y, i, ix, iy, nReplaced; double z, zMin, zMax; x = Get_System()->Get_xWorld_to_Grid(ptWorld.Get_X()); y = Get_System()->Get_yWorld_to_Grid(ptWorld.Get_Y()); if( m_pGrid && m_pGrid->is_InGrid(x, y, m_bNoData) ) { Message_Add(_TL("Starting flood fill...")); switch( m_Method ) { case 0: z = m_pGrid->asDouble(x, y); break; // value at mouse position case 1: z = m_zFixed; break; // fixed value case 2: z = 0.0; break; // tolerance as absolute values } zMin = z + m_zTolerance_Min; zMax = z + m_zTolerance_Max; m_iStack = 0; nReplaced = 1; Push(x, y); //--------------------------------------------- while( m_iStack > 0 && Set_Progress_NCells(nReplaced) ) { Pop(x, y); for(i=0; i<8; i+=2) { ix = Get_xTo(i, x); iy = Get_yTo(i, y); if( m_pGrid->is_InGrid(ix, iy, m_bNoData) ) { z = m_pGrid->asDouble(ix, iy); if( z != m_zFill && z >= zMin && z <= zMax ) { Push(ix, iy); m_pGrid->Set_Value(ix, iy, m_zFill); nReplaced++; } } } } //--------------------------------------------- Message_Add(_TL("ready"), false); Message_Add(CSG_String::Format(SG_T("%d %s"), nReplaced, _TL("replacements"))); DataObject_Update(m_pGrid, m_pGrid->Get_ZMin(), m_pGrid->Get_ZMax()); return( true ); } } return( false ); }
//--------------------------------------------------------- void CFlow::Finalize(void) { long n; double z, CellSize, Catch, Contour, dContour, G, H; //----------------------------------------------------- CellSize = Get_Cellsize() * Get_Cellsize(); Contour = 1.0; dContour = 0.02 * M_PI * sqrt(CellSize / M_PI); //----------------------------------------------------- for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { if( pDTM->is_NoData(n) ) { if( pCatch ) { pCatch ->Set_NoData(n); } if( pCatch_Height ) { pCatch_Height ->Set_NoData(n); } if( pCatch_Slope ) { pCatch_Slope ->Set_NoData(n); } if( pCatch_Aspect ) { pCatch_Aspect ->Set_NoData(n); } if( pFlowPath ) { pFlowPath ->Set_NoData(n); } } else { z = pDTM->asDouble(n); //--------------------------------------------- Catch = 1.0 / pCatch->asDouble(n); if( pCatch_Height ) { pCatch_Height ->Set_Value(n, Catch * pCatch_Height->asDouble(n) - z); } if( pCatch_Slope ) { pCatch_Slope ->Mul_Value(n, Catch); } if( pFlowPath ) { pFlowPath ->Mul_Value(n, Catch); } //--------------------------------------------- Catch = CellSize / Catch; if( pCatch ) { pCatch ->Set_Value(n, Catch); } //--------------------------------------------- if( pCatch_Aspect && pCatch_AspectY ) { G = pCatch_Aspect ->asDouble(n); H = pCatch_AspectY->asDouble(n); pCatch_Aspect ->Set_Value(n, G ? fmod(M_PI_270 + atan2(H, G), M_PI_360) : (H > 0 ? M_PI_270 : (H < 0 ? M_PI_090 : -1))); } } } //----------------------------------------------------- if( pCatch_AspectY ) { delete(pCatch_AspectY); pCatch_AspectY = NULL; } }
//--------------------------------------------------------- bool CChannelNetwork_Distance::On_Execute(void) { CSG_Grid *pChannels; //----------------------------------------------------- m_pDEM = Parameters("ELEVATION")->asGrid(); m_pRoute = Parameters("ROUTE" )->asGrid(); pChannels = Parameters("CHANNELS" )->asGrid(); m_pDistance = Parameters("DISTANCE" )->asGrid(); m_pDistVert = Parameters("DISTVERT" )->asGrid(); m_pDistHorz = Parameters("DISTHORZ" )->asGrid(); m_pTime = Parameters("TIME" )->asGrid(); m_pSDR = Parameters("SDR" )->asGrid(); m_Flow_B = Parameters("FLOW_B" )->asDouble(); m_Flow_K = Parameters("FLOW_K" )->asDouble(); m_Flow_R = Parameters("FLOW_R" )->asDouble(); m_pFlow_K = Parameters("FLOW_K" )->asGrid(); m_pFlow_R = Parameters("FLOW_R" )->asGrid(); int Method = Parameters("METHOD" )->asInt(); //----------------------------------------------------- if( m_pDistance ) m_pDistance->Assign_NoData(); if( m_pDistVert ) m_pDistVert->Assign_NoData(); if( m_pDistHorz ) m_pDistHorz->Assign_NoData(); if( m_pTime ) m_pTime ->Assign_NoData(); if( m_pSDR ) m_pSDR ->Assign_NoData(); switch( Method ) { default: Initialize_D8 (); break; case 1: Initialize_MFD(); break; } m_pDEM->Set_Index(true); //----------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; if( m_pDEM->Get_Sorted(n, x, y, false, true) && !(pChannels->is_NoData(x, y) && m_pDistance->is_NoData(x, y)) ) { if( !pChannels->is_NoData(x, y) ) { if( m_pDistance ) m_pDistance->Set_Value(x, y, 0.0); if( m_pDistVert ) m_pDistVert->Set_Value(x, y, 0.0); if( m_pDistHorz ) m_pDistHorz->Set_Value(x, y, 0.0); if( m_pTime ) m_pTime ->Set_Value(x, y, 0.0); if( m_pSDR ) m_pSDR ->Set_Value(x, y, 0.0); if( m_pFields ) m_pPasses ->Set_Value(x, y, 0.0); } switch( Method ) { default: Execute_D8 (x, y); break; case 1: Execute_MFD(x, y); break; } } } //----------------------------------------------------- m_Dir.Destroy(); for(int i=0; i<=8; i++) { m_Flow[i].Destroy(); } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CGrid_Cluster_Analysis::On_Execute(void) { if( Parameters("OLDVERSION")->asBool() ) { return( _On_Execute() ); } //----------------------------------------------------- bool bNormalize; int iFeature; sLong iElement, nElements; CSG_Cluster_Analysis Analysis; CSG_Grid *pCluster; CSG_Parameter_Grid_List *pGrids; //----------------------------------------------------- pGrids = Parameters("GRIDS" )->asGridList(); pCluster = Parameters("CLUSTER" )->asGrid(); bNormalize = Parameters("NORMALISE")->asBool(); if( !Analysis.Create(pGrids->Get_Count()) ) { return( false ); } //----------------------------------------------------- pCluster->Set_NoData_Value(0.0); for(iElement=0, nElements=0; iElement<Get_NCells() && Set_Progress_NCells(iElement); iElement++) { bool bNoData = false; for(iFeature=0; iFeature<pGrids->Get_Count() && !bNoData; iFeature++) { if( pGrids->asGrid(iFeature)->is_NoData(iElement) ) { bNoData = true; } } if( bNoData || !Analysis.Add_Element() ) { pCluster->Set_Value(iElement, 0); } else { pCluster->Set_Value(iElement, 1); for(iFeature=0; iFeature<pGrids->Get_Count(); iFeature++) { double d = pGrids->asGrid(iFeature)->asDouble(iElement); if( bNormalize ) { d = (d - pGrids->asGrid(iFeature)->Get_Mean()) / pGrids->asGrid(iFeature)->Get_StdDev(); } Analysis.Set_Feature(nElements, iFeature, d); } nElements++; } } if( nElements <= 1 ) { return( false ); } //----------------------------------------------------- bool bResult = Analysis.Execute( Parameters("METHOD" )->asInt(), Parameters("NCLUSTER")->asInt(), Parameters("MAXITER" )->asInt() ); for(iElement=0, nElements=0; iElement<Get_NCells(); iElement++) { Set_Progress_NCells(iElement); if( !pCluster->is_NoData(iElement) ) { pCluster->Set_Value(iElement, 1 + Analysis.Get_Cluster(nElements++)); } } Save_Statistics(pGrids, bNormalize, Analysis); Save_LUT(pCluster); return( bResult ); }
//--------------------------------------------------------- bool CWatershed_Segmentation::Get_Segments(void) { Process_Set_Text(_TL("Segments")); double Threshold = Parameters("THRESHOLD")->asDouble(); int Join = Threshold > 0.0 ? Parameters("JOIN")->asInt() : 0; //----------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y, i, ID, iID; if( m_pGrid->Get_Sorted(n, x, y, m_bDown) && (i = m_Dir.asInt(x, y)) >= 0 ) { m_pSegments->Set_Value(x, y, ID = m_pSegments->asInt(Get_xTo(i, x), Get_yTo(i, y))); if( Join != 0 && ID >= 0 ) { double z = m_pGrid->asDouble(x, y); for(int i=0; i<8; i++) { int ix = Get_xTo(i, x); int iy = Get_yTo(i, y); if( m_pSegments->is_InGrid(ix, iy) && (iID = m_pSegments->asInt(ix, iy)) >= 0 ) // Border < 0, Segment >= 0 { if( ID != iID ) { bool bJoin; if( Join == 1 ) { bJoin = (Threshold >= fabs(m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) - z)) || (Threshold >= fabs(m_pSeeds->Get_Shape( ID)->asDouble(SEED_Z) - z)); } else { bJoin = Threshold >= fabs(m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) - m_pSeeds->Get_Shape(ID)->asDouble(SEED_Z)); } if( bJoin ) { if( (m_bDown == true && m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) < m_pSeeds->Get_Shape(ID)->asDouble(SEED_Z)) || (m_bDown == false && m_pSeeds->Get_Shape(iID)->asDouble(SEED_Z) > m_pSeeds->Get_Shape(ID)->asDouble(SEED_Z)) ) { Segment_Change(iID, ID); } else { Segment_Change(ID, iID); ID = iID; } } } } } } } } return( true ); }
//--------------------------------------------------------- bool CErosion_LS_Fields::Get_Flow(void) { //----------------------------------------------------- if( !m_pDEM->Set_Index() ) // create index ... { return( false ); } Process_Set_Text(_TL("Flow Accumulation")); m_pUp_Area ->Assign(0.0); m_pUp_Length->Assign(0.0); m_pUp_Slope ->Assign(0.0); for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; double dzSum, dz[8], Slope, Aspect; if( m_pDEM->Get_Sorted(n, x, y) && !m_Fields.is_NoData(x, y) && m_pDEM->Get_Gradient(x, y, Slope, Aspect) ) { double Up_Area = m_pUp_Area ->asDouble(x, y) + Get_Cellarea(); double Up_Length = m_pUp_Length->asDouble(x, y) + log(Up_Area); double Up_Slope = m_pUp_Slope ->asDouble(x, y) + log(Up_Area) * Slope; //--------------------------------------------- if( (dzSum = Get_Flow(x, y, dz)) > 0.0 ) { for(int i=0; i<8; i++) { if( dz[i] > 0.0 ) { int ix = Get_xTo(i, x); int iy = Get_yTo(i, y); m_pUp_Area ->Add_Value(ix, iy, Up_Area * dz[i] / dzSum); m_pUp_Length->Add_Value(ix, iy, Up_Length * dz[i] / dzSum); m_pUp_Slope ->Add_Value(ix, iy, Up_Slope * dz[i] / dzSum); } } } //--------------------------------------------- switch( m_Method_Area ) { case 0: // specific catchment area (contour length simply as cell size) m_pUp_Area->Set_Value(x, y, Up_Area / (Get_Cellsize())); break; case 1: // specific catchment area (contour length dependent on aspect) m_pUp_Area->Set_Value(x, y, Up_Area / (Get_Cellsize() * (fabs(sin(Aspect)) + fabs(cos(Aspect))))); break; case 2: // catchment length (square root of catchment area) m_pUp_Area->Set_Value(x, y, sqrt(Up_Area)); break; case 3: // effective flow length m_pUp_Area->Set_Value(x, y, Up_Length); break; case 4: // total catchment area m_pUp_Area->Set_Value(x, y, Up_Area); break; } m_pUp_Length->Set_Value(x, y, Up_Length); m_pUp_Slope ->Set_Value(x, y, Up_Slope / (Up_Length > M_ALMOST_ZERO ? Up_Length : M_ALMOST_ZERO)); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CCost_Accumulated::Get_Cost(CPoints &Points) { CPoints Next; CSG_Grid Next_Index(Get_System(), SG_DATATYPE_Int); double Threshold = Parameters("THRESHOLD")->asDouble(); sLong nProcessed = Points.Get_Count(); while( Points.Get_Count() > 0 && Set_Progress_NCells(nProcessed) ) { Process_Set_Text("%s: %d", _TL("cells in process"), Points.Get_Count()); int iPoint; //------------------------------------------------- Next.Clear(); for(iPoint=0; iPoint<Points.Get_Count(); iPoint++) { TSG_Point_Int p = Points[iPoint]; double Cost = m_pCost ->asDouble(p.x, p.y); double Accu = m_pAccumulated->asDouble(p.x, p.y); for(int i=0; i<8; i++) { int ix = Get_xTo(i, p.x); int iy = Get_yTo(i, p.y); if( m_pCost->is_InGrid(ix, iy) ) { double iCost = Get_UnitLength(i); if( m_pDirection && m_pDirection->is_InGrid(p.x, p.y) && m_pDirection->is_InGrid(ix, iy) ) { static const double dAngles[8] = { 0.0, M_PI_045, M_PI_090, M_PI_135, M_PI_180, M_PI_225, M_PI_270, M_PI_315 }; double d1 = m_pDirection->asDouble(p.x, p.y); d1 = pow(cos(fabs((m_bDegree ? M_DEG_TO_RAD * d1 : d1) - dAngles[i])), m_dK); double d2 = m_pDirection->asDouble( ix, iy); d2 = pow(cos(fabs((m_bDegree ? M_DEG_TO_RAD * d2 : d2) - dAngles[i])), m_dK); iCost *= (d1 + d2) / 2.0; } iCost = Accu + iCost * (Cost + m_pCost->asDouble(ix, iy)) / 2.0; //------------------------------------- bool bProcessed = !m_pAccumulated->is_NoData(ix, iy); if( !bProcessed || m_pAccumulated->asDouble(ix, iy) > iCost + Threshold ) { if( !bProcessed ) { nProcessed++; } Next_Index .Set_Value(ix, iy, Next.Get_Count()); // remember last point (least cost!) added at position ix/iy Next .Add (ix, iy); m_pAccumulated->Set_Value(ix, iy, iCost); } } } } //------------------------------------------------- Points.Clear(); for(iPoint=0; iPoint<Next.Get_Count(); iPoint++) { TSG_Point_Int p = Next[iPoint]; if( Next_Index.asInt(p.x, p.y) == iPoint ) { Points.Add(p.x, p.y); } } } return( true ); }
//--------------------------------------------------------- // Calculation according to Lane et al. 2004, p. 198. //--------------------------------------------------------- bool CDiffuse_Pollution_Risk::Set_Delivery_Index(void) { //----------------------------------------------------- CSG_Grid Channel, *pChannel = Parameters("CHANNEL")->asGrid(); if( !pChannel ) // no channel network is provided, so create a tempory channel network out of the box! { Process_Set_Text(_TL("Channel Network")); int Threshold = Parameters("CHANNEL_START")->asInt(); pChannel = &Channel; Channel.Create(*Get_System(), SG_DATATYPE_Word); Channel.Assign(0.0); Channel.Set_NoData_Value(0.0); for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y, ix, iy; if( m_pDEM->Get_Sorted(n, x, y, true) ) { if( Get_System()->Get_Neighbor_Pos(m_FlowDir.asInt(x, y), x, y, ix, iy) && m_FlowDir.is_InGrid(ix, iy) ) { Channel.Add_Value(ix, iy, 1.0 + Channel.asDouble(x, y)); } Channel.Set_Value(x, y, Channel.asInt(x, y) > Threshold ? 1.0 : 0.0); } } } //----------------------------------------------------- Process_Set_Text(_TL("Delivery Index")); for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y, ix, iy; double TWI_min = -1.0; if( m_pDEM->Get_Sorted(n, x, y, false) ) // bottom up { if( m_TWI.is_InGrid(x, y) ) { TWI_min = m_TWI.asDouble(x, y); } if( m_FlowDir.is_InGrid(x, y) && Get_System()->Get_Neighbor_Pos(m_FlowDir.asInt(x, y), x, y, ix, iy) && m_TWI.is_InGrid(ix, iy) ) { if( TWI_min < 0.0 || TWI_min > m_TWI.asDouble(ix, iy) ) { TWI_min = m_TWI.asDouble(ix, iy); } } } if( TWI_min < 0.0 ) { m_pDelivery->Set_NoData(x, y); } else { m_pDelivery->Set_Value(x, y, TWI_min); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CChange_Detection::Get_Classes(CSG_Table &Classes, CSG_Grid *pGrid, bool bInitial) { CSG_Table *pClasses; Classes.Destroy(); Classes.Add_Field(_TL("NAME") , SG_DATATYPE_String); Classes.Add_Field(_TL("MIN") , SG_DATATYPE_Double); Classes.Add_Field(_TL("MAX") , SG_DATATYPE_Double); //----------------------------------------------------- if( (pClasses = Parameters(bInitial ? "INI_LUT" : "FIN_LUT")->asTable()) != NULL ) { int fNam = Parameters(bInitial ? "INI_LUT_NAM" : "FIN_LUT_NAM")->asInt(); int fMin = Parameters(bInitial ? "INI_LUT_MIN" : "FIN_LUT_MIN")->asInt(); int fMax = Parameters(bInitial ? "INI_LUT_MAX" : "FIN_LUT_MAX")->asInt(); if( fNam < 0 || fNam >= pClasses->Get_Field_Count() ) { fNam = fMin; } if( fMax < 0 || fMax >= pClasses->Get_Field_Count() ) { fMax = fMin; } for(int iClass=0; iClass<pClasses->Get_Count(); iClass++) { CSG_Table_Record *pClass = Classes.Add_Record(); pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(fNam)); pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(fMin)); pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(fMax)); } } //----------------------------------------------------- else if( DataObject_Get_Parameter(pGrid, "LUT") ) { pClasses = DataObject_Get_Parameter(pGrid, "LUT")->asTable(); for(int iClass=0; iClass<pClasses->Get_Count(); iClass++) { CSG_Table_Record *pClass = Classes.Add_Record(); pClass->Set_Value(CLASS_NAM, pClasses->Get_Record(iClass)->asString(1)); pClass->Set_Value(CLASS_MIN, pClasses->Get_Record(iClass)->asDouble(3)); pClass->Set_Value(CLASS_MAX, pClasses->Get_Record(iClass)->asDouble(4)); } } //----------------------------------------------------- else { double z; for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++) { double iz = pGrid->asDouble(pGrid->Get_Sorted(i, false, false)); if( i == 0 || iz != z ) { CSG_Table_Record *pClass = Classes.Add_Record(); pClass->Set_Value(CLASS_NAM, z = iz); pClass->Set_Value(CLASS_MIN, z); pClass->Set_Value(CLASS_MAX, z); } } } //----------------------------------------------------- return( Classes.Get_Count() > 0 ); }
//--------------------------------------------------------- bool CWatersheds::On_Execute(void) { int x, y, nCells, nCells_Min, nBasins; sLong n; CSG_Grid *pDTM, *pSeed, *pRoute; //----------------------------------------------------- pDTM = Parameters("ELEVATION")->asGrid(); pSeed = Parameters("CHANNELS" )->asGrid(); pRoute = Parameters("SINKROUTE")->asGrid(); nCells_Min = Parameters("MINSIZE" )->asInt(); m_pBasins = Parameters("BASINS" )->asGrid(); m_pBasins->Set_NoData_Value(NO_BASIN); m_pBasins->Assign_NoData(); if( !pDTM->Set_Index() ) { Error_Set(_TL("index creation failed")); return( false ); } m_Direction.Create(m_pBasins, SG_DATATYPE_Char); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( pDTM->is_NoData(x, y) ) { m_Direction.Set_NoData(x, y); } else { if( !pRoute || (n = pRoute->asChar(x, y)) <= 0 ) { n = pDTM->Get_Gradient_NeighborDir(x, y); } m_Direction.Set_Value(x, y, (int)(n < 0 ? -1 : (n + 4) % 8)); } } } //----------------------------------------------------- for(n=0, m_nBasins=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { pDTM->Get_Sorted(n, x, y, true, false); if( !pSeed->is_NoData(x, y) && pSeed->asInt(x, y) < 0 ) { m_nBasins++; if( (nCells = Get_Basin(x, y)) < nCells_Min ) { nBasins = m_nBasins - 1; m_nBasins = NO_BASIN; Get_Basin(x, y); m_nBasins = nBasins; } } } //----------------------------------------------------- m_Direction.Destroy(); return( true ); }
//--------------------------------------------------------- bool CChannelNetwork::On_Execute(void) { int x, y, ID, Trace_Method, Init_Method; long n; double Init_Threshold; CSG_Grid *Trace_pRoute, *Trace_pWeight, *Init_pGrid; //----------------------------------------------------- pDTM = Parameters("ELEVATION") ->asGrid(); pConvergence = Parameters("DIV_GRID") ->asGrid(); pChannels = Parameters("CHNLNTWRK") ->asGrid(); pChannelRoute = Parameters("CHNLROUTE") ->asGrid(); pShapes = Parameters("SHAPES") ->asShapes(); minLength = Parameters("MINLEN") ->asInt(); maxDivCells = Parameters("DIV_GRID")->asGrid() ? Parameters("DIV_CELLS")->asInt() : -1; //----------------------------------------------------- // 1. Flow Direction... Process_Set_Text(_TL("Channel Network: Pass 1")); pChannels->Assign(); Trace_pRoute = Parameters("SINKROUTE") ->asGrid(); Trace_pWeight = Parameters("TRACE_WEIGHT")->asGrid(); Trace_Method = Trace_pWeight ? 1 : 0; for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { if( Trace_pRoute && (ID = Trace_pRoute->asChar(x, y)) >= 1 && ID <= 8 ) { pChannels->Set_Value(x, y, ID); } else { switch( Trace_Method ) { default: Set_Route_Standard(x, y); break; case 1: Set_Route_Weighted(x, y, Trace_pWeight, 0.0); break; } } } } //----------------------------------------------------- // 2. Initiation... Process_Set_Text(_TL("Channel Network: Pass 2")); pStart = SG_Create_Grid(pDTM, SG_DATATYPE_Char); Init_pGrid = Parameters("INIT_GRID") ->asGrid(); Init_Method = Parameters("INIT_METHOD") ->asInt(); Init_Threshold = Parameters("INIT_VALUE") ->asDouble(); for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { switch( Init_Method ) { case 0: if( Init_pGrid->asDouble(n) <= Init_Threshold ) pStart->Set_Value(n, 1); break; case 1: if( Init_pGrid->asDouble(n) == Init_Threshold ) pStart->Set_Value(n, 1); break; case 2: if( Init_pGrid->asDouble(n) >= Init_Threshold ) pStart->Set_Value(n, 1); break; } } //----------------------------------------------------- // 3. Trace Channel Routes... Process_Set_Text(_TL("Channel Network: Pass 3")); pChannelRoute->Assign(); Direction = NULL; Direction_Buffer = 0; for(n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { if( pDTM->Get_Sorted(n,x,y) ) { Set_Channel_Route(x,y); } } if( Direction ) { SG_Free( Direction ); } pChannels->Assign(); delete(pStart); //----------------------------------------------------- Process_Set_Text(_TL("Channel Network: Pass 4")); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { Set_Channel_Order(x,y); } } //----------------------------------------------------- Process_Set_Text(_TL("Channel Network: Pass 5")); for(y=0; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { Set_Channel_Mouth(x,y); } } //----------------------------------------------------- if( pShapes ) { Process_Set_Text(_TL("Channel Network: Pass 6")); pShapes->Create(SHAPE_TYPE_Line, _TL("Channel Network")); pShapes->Add_Field("SegmentID" ,SG_DATATYPE_Int); pShapes->Add_Field("Order" ,SG_DATATYPE_Int); pShapes->Add_Field("Length" ,SG_DATATYPE_Double); Lock_Create(); for(y=0, ID=1; y<Get_NY() && Set_Progress(y); y++) { for(x=0; x<Get_NX(); x++) { Set_Vector(x, y, ID++); } } Lock_Destroy(); } //----------------------------------------------------- for(n=0; n<Get_NCells(); n++) { if( pChannels->asInt(n) == 0 ) { pChannels->Set_NoData(n); pChannelRoute->Set_NoData(n); } } //----------------------------------------------------- return( true ); }
//--------------------------------------------------------- bool CFlow_Parallel::Set_Flow(void) { //----------------------------------------------------- if( !m_pDTM->Set_Index() ) { return( false ); } //----------------------------------------------------- int Method = Parameters("METHOD")->asInt(); if( Method == 2 ) { BRM_Init(); } //----------------------------------------------------- double dLinear = Parameters("LINEAR_DO")->asBool() ? Parameters("LINEAR_MIN")->asDouble() : -1.0; CSG_Grid *pLinear_Val = Parameters("LINEAR_VAL")->asGrid(); CSG_Grid *pLinear_Dir = Parameters("LINEAR_DIR")->asGrid(); //----------------------------------------------------- bool bNoNegatives = m_pWeights ? Parameters("NO_NEGATIVES")->asBool() : false; CSG_Grid *pLoss = Parameters("WEIGHT_LOSS")->asGrid(); if( bNoNegatives && pLoss ) { pLoss->Assign_NoData(); } //----------------------------------------------------- for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; if( m_pDTM->Get_Sorted(n, x, y) ) { if( bNoNegatives && m_pFlow->asDouble(x, y) < 0.0 ) { if( pLoss ) { pLoss->Set_Value(x, y, fabs(m_pFlow->asDouble(x, y))); } m_pFlow->Set_Value(x, y, 0.0); } if( pLinear_Dir && !pLinear_Dir->is_NoData(x, y) ) { Set_D8(x, y, pLinear_Dir->asInt(x, y)); } else if( dLinear > 0.0 && dLinear <= (pLinear_Val && !pLinear_Val->is_NoData(x, y) ? pLinear_Val->asDouble(x, y) : m_pFlow->asDouble(x, y)) ) { Set_D8(x, y, pLinear_Dir && !pLinear_Dir->is_NoData(x, y) ? pLinear_Dir->asInt(x, y) : -1); } else switch( Method ) { case 0: Set_D8 (x, y); break; case 1: Set_Rho8 (x, y); break; case 2: Set_BRM (x, y); break; case 3: Set_DInf (x, y); break; case 4: Set_MFD (x, y); break; case 5: Set_MDInf (x, y); break; case 6: Set_MMDGFD(x, y); break; } } } //----------------------------------------------------- if( m_pRoute ) { for(sLong n=0; n<Get_NCells() && Set_Progress_NCells(n); n++) { int x, y; if( m_pDTM->Get_Sorted(n, x, y, false) ) { Check_Route(x, y); } } } //----------------------------------------------------- return( true ); }