Esempio n. 1
0
void CWet_hView::OnTopographiccalculationMeanwatershedelevation()
   {
   MapLayer *pFlowDir = gpMapWnd->m_pMap->GetLayer( "FLOWDIR" );
   MapLayer *pStreamGrid = gpMapWnd->m_pMap->GetLayer( "STRGRID" );
   MapLayer *pDEM = gpMapWnd->m_pMap->GetLayer( "DEM" );
   MapLayer *pFPDistGrid = gpMapWnd->m_pMap->CloneLayer( *pDEM );
   pFPDistGrid->m_name = "FP DistGrad";
   MapLayer *pWatershed = gpMapWnd->m_pMap->GetLayer( "Cells" );
   MapLayer *pStreamV = gpMapWnd->m_pMap->GetLayer("Streams");
   
   int method = 2;
  // the third parameter is the method to use.  a 1 simply returns the average fpdist for each polygon
   // a two returns the entire fp distribution
   int watershedCount   = pWatershed->GetRecordCount();
   int reachCount = pStreamV->GetRecordCount();
   gpHydroModel->m_fpDistributionArray.SetSize(reachCount);
   gpHydroModel->m_fpDistributionArrayStream.SetSize(reachCount);
   for (int i=0; i < reachCount; i++ )
		{
      Poly *pStreamPoly = pStreamV->m_polyArray[i];
      int streamID;
      gpHydroModel->m_pStreamLayer->GetData(i,pStreamV->m_pData->GetCol("HYDRO_ID"), streamID);
      for ( int j=0; j < watershedCount; j++ )//this should be for each reach
         {
		   Poly *pThisPoly = pWatershed->m_polyArray[j];
         int watershedID;
         gpHydroModel->m_pCellLayer->GetData(j,pWatershed->m_pData->GetCol("HYDRO_ID"), watershedID);
         if (watershedID==streamID) // this polygon is in the current reach
            {
		      Poly *pThisPoly = pWatershed->m_polyArray[j];
            float flowPathDistance=0.0f;
            int flowPathCount=0;
            
            float flowPathGradient = 0.001f;
            int rows = pDEM->GetRowCount();
            int cols = pDEM->GetColCount();
            for ( int row=0; row < rows; row++ )
               {
               for ( int col=0; col < cols; col++ )
                  {
                  float x = 0;
                  float y = 0;
                  pDEM->GetGridCellCenter(row, col, x, y);
                  
                  Vertex point;
                  point.x = x;
                  point.y = y;       ///the stream grid is smaller than the flowdir grid
                  if (pThisPoly->IsPointInPoly(point))
                     {
                     int flowPathCellCount=0;
                     float distance = pFlowDir->GetDownFlowPathDistance( row, col, pStreamGrid );
                     float gradient = pFlowDir->GetDownFlowPathGradient(row,col,pStreamGrid,pDEM,flowPathCellCount);
                     gradient=gradient/flowPathCellCount;        
                     float dist_grad = distance/gradient;
                     if (distance==pFlowDir->GetNoDataValue())
                        {
                        dist_grad = pFlowDir->GetNoDataValue();
                        gradient = pFlowDir->GetNoDataValue();
                        }
                     pFPDistGrid->m_pData->Set(col, row, (distance/gradient)); 
                     if (method==1)// we just want the average fpdistance
                        {
                        flowPathGradient +=gradient;
                        flowPathDistance +=distance;
                        flowPathCount++;
                        }
                     if (method==2)// we want to maintain the entire distribution
                        {
                        int dist = (int)(distance/gradient);
                        gpHydroModel->m_fpDistributionArray[i].Add(dist);
                        }
                     }
                  }  // end of:  for ( col < cols )
               }  // end of:  for ( row < rows )
            } // end of :  if watershed==streamID
         }// end of j (watershedCount)
   //  we have found all the flowpaths in the jth reach, but want to now find the median value of that distribution
      float median=-1;
      if (method==2)
         {
         int  temp=-1;
         for (int bottom=gpHydroModel->m_fpDistributionArray[i].GetCount()-1; bottom>0;bottom--)
            {
            for (int position=0;position<bottom;position++)
               {
               int val = gpHydroModel->m_fpDistributionArray[i][position+1];
               int val2 = gpHydroModel->m_fpDistributionArray[i][position];
               if (val < val2)
                  {
                  int temp2 = gpHydroModel->m_fpDistributionArray[i][position];
                  gpHydroModel->m_fpDistributionArray[i][position] = gpHydroModel->m_fpDistributionArray[i][position+1];
                  gpHydroModel->m_fpDistributionArray[i][position+1] = temp2;
                  }
               }
            }
         if (gpHydroModel->m_fpDistributionArray[i].GetCount()<=1)//there were no watersheds for this reach
            {
            median = 0.0f;
            }
         else
            {
            int midValue = gpHydroModel->m_fpDistributionArray[i].GetCount()/2;
            median = (float)gpHydroModel->m_fpDistributionArray[i][midValue];
            }
         }//end of method
   //  float rt = (0.00881f*averageFPDistGrad) + 0.00314f;
 		gpHydroModel->m_pMap->Notify( NT_CALCDIST, i, reachCount );   
      pStreamV->SetData(i,pStreamV->m_pData->GetCol("EXTRA_1"), median);
      
      int cellID = -1;
      pStreamV->GetData(i,pStreamV->m_pData->GetCol("HYDRO_ID"), cellID);
      for (int k=0;k<pStreamV->GetRecordCount();k++)
         {
	      ReachNode *pNode = gpHydroModel->m_reachTree.m_nodeArray[k];
	      REACH_INFO_HYDRO *pHydro1 = (REACH_INFO_HYDRO*) pNode->m_reachInfo.pData; 
         if (pNode->m_reachInfo.index == i)
            pHydro1->cumSedimentOutflow = median;
         }
      }// end of this REACHs


      //Now are finished finding the median fpdist/gradient for each subwatershed and need to accumulate them downslope

      ReachNode *pNode = gpHydroModel->m_reachTree.FindLeftLeaf();  // find leftmost leaf of entire tree (no params defaults to m_pRoot )
	   REACH_INFO_HYDRO *pHydro = gpHydroModel->GetReachInfoHydroFromReachID( pNode->m_reachInfo.reachID );
      gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)pHydro->cumSedimentOutflow);
 	   int count=0;
      while ( pNode->m_pDown->m_reachInfo.index != ROOT_NODE )
         {
         ReachNode *pDown = pNode->m_pDown;
         if (( pDown->m_pLeft == pNode ) && ( pDown->m_pRight != NULL ))
            {
            pNode = gpHydroModel->m_reachTree.FindLeftLeaf( pDown->m_pRight );
			   REACH_INFO_HYDRO *pHydro = gpHydroModel->GetReachInfoHydroFromReachID( pNode->m_reachInfo.reachID );
            gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)pHydro->cumSedimentOutflow);
            count++;
            }

         else // We have all upstream orders, so can move downstream.
            {
		      pNode = pDown;
			   int reachID = pNode->m_reachInfo.reachID;
			   if ( pNode->m_reachInfo.index == PHANTOM_NODE)  //phantom node - we need our right's right and left pointers!
				//if (  pNode->m_reachInfo.reachID==-1)
            {
				   pNode = pNode->m_pDown;    // just move downstream one node
				   REACH_INFO_HYDRO *pHydro = gpHydroModel->GetReachInfoHydroFromReachID( pNode->m_reachInfo.reachID );
               gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)pHydro->cumSedimentOutflow);
               count++;
               int size = gpHydroModel->m_fpDistributionArray[pNode->m_pRight->m_pRight->m_reachInfo.index].GetSize();
               
               for (int i=0; i<gpHydroModel->m_fpDistributionArrayStream[pNode->m_pRight->m_pRight->m_reachInfo.index].GetSize();i++) 
                  gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)gpHydroModel->m_fpDistributionArrayStream[pNode->m_pRight->m_pRight->m_reachInfo.index][i]);
               
               for (int i=0; i<gpHydroModel->m_fpDistributionArrayStream[pNode->m_pRight->m_pLeft->m_reachInfo.index].GetSize();i++) 
                   gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)gpHydroModel->m_fpDistributionArrayStream[pNode->m_pRight->m_pLeft->m_reachInfo.index][i]);
               
               for (int i=0; i<gpHydroModel->m_fpDistributionArrayStream[pNode->m_pLeft->m_reachInfo.index].GetSize();i++) 
                   gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)gpHydroModel->m_fpDistributionArrayStream[pNode->m_pLeft->m_reachInfo.index][i]);

               }
			   else //Not a phantom so just add up the left, right and current area.
				   {
				   REACH_INFO_HYDRO *pHydro = gpHydroModel->GetReachInfoHydroFromReachID( pNode->m_reachInfo.reachID );
				   if ( pNode->m_pRight != NULL )
                  {
                  gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)pHydro->cumSedimentOutflow);
                  count++;
                  for (int i=0; i<gpHydroModel->m_fpDistributionArrayStream[pNode->m_pRight->m_reachInfo.index].GetSize();i++) 
                     gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)gpHydroModel->m_fpDistributionArrayStream[pNode->m_pRight->m_reachInfo.index][i]);
                  
                  for (int i=0; i<gpHydroModel->m_fpDistributionArrayStream[pNode->m_pLeft->m_reachInfo.index].GetSize();i++) 
                     gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)gpHydroModel->m_fpDistributionArrayStream[pNode->m_pLeft->m_reachInfo.index][i]);                                 
                  }
               else
                  {
                  gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)pHydro->cumSedimentOutflow); 
                  count++;
                  for (int i=0; i<gpHydroModel->m_fpDistributionArrayStream[pNode->m_pLeft->m_reachInfo.index].GetSize();i++) 
                     gpHydroModel->m_fpDistributionArrayStream[pNode->m_reachInfo.index].Add((int)gpHydroModel->m_fpDistributionArrayStream[pNode->m_pLeft->m_reachInfo.index][i]);
                  }
               }
         }//end of : else
      }//end of While (search for the root node...)

//Now go through the m_fpDistributionArray and again find the median value.

 for (int j=0;j<pStreamV->GetRecordCount();j++)
    {  
   float median=-1.0f;
   float rt=0.0f;
   if (method==2)
      {
      int  temp=-1;
      int count = gpHydroModel->m_fpDistributionArrayStream[j].GetCount();
      for (int bottom=gpHydroModel->m_fpDistributionArrayStream[j].GetCount()-1; bottom>0;bottom--)
         {
         for (int position=0;position<bottom;position++)
            {
            int val = gpHydroModel->m_fpDistributionArrayStream[j][position+1];
            int val2 = gpHydroModel->m_fpDistributionArrayStream[j][position];
            if (val < val2)
               {
               int temp2 = gpHydroModel->m_fpDistributionArrayStream[j][position];
               gpHydroModel->m_fpDistributionArrayStream[j][position] = gpHydroModel->m_fpDistributionArrayStream[j][position+1];
               gpHydroModel->m_fpDistributionArrayStream[j][position+1] = temp2;
               }
            }
         }
     // if (gpHydroModel->m_fpDistributionArrayStream[j].GetCount()<=1)//there were no watersheds for this reach
       //  median = 0;
     // else
       //  {
         
         int midValue = gpHydroModel->m_fpDistributionArrayStream[j].GetCount();
         if (midValue<0)
            median=0.0f;
         else
            median = (float)gpHydroModel->m_fpDistributionArrayStream[j][(int)(midValue/2)];

         rt = (0.002347f*median) + 0.186f;
       //  }
      }//end of j - now on to the next stream reach in the array  

      // Then add the median value to somewhere that can be mapped...
    gpHydroModel->m_pMap->Notify( NT_CALCDIST, j, reachCount );   
    pStreamV->SetData(j,pStreamV->m_pData->GetCol("EXTRA_1"), median);
    int streamID=-1;
    pStreamV->GetData(j,pStreamV->m_pData->GetCol("HYDRO_ID"), streamID);

      for ( int jj=0; jj < watershedCount; jj++ )//this should be for each reach
         {
		   Poly *pThisPoly = pWatershed->m_polyArray[jj];
         int watershedID;
         gpHydroModel->m_pCellLayer->GetData(jj,pWatershed->m_pData->GetCol("HYDRO_ID"), watershedID);
         if (watershedID==streamID) // this polygon is in the current reach
            pWatershed->SetData(jj,pWatershed->m_pData->GetCol("EXTRA_1"), rt);
            
         }


    }   
    float minVal=0.0f;
    float maxVal=0.0f;
    pWatershed->GetDataMinMax( pWatershed->m_pData->GetCol("EXTRA_1"), &minVal, &maxVal );
    pWatershed->SetBins(minVal,maxVal,10);
    pWatershed->ClassifyData(pWatershed->m_pData->GetCol("EXTRA_1"));
    if ( gpMapWnd != NULL )
       gpMapWnd->RefreshList();  
	gpMapWnd->m_pMapList->RedrawWindow();
	CDC *dc = gpMapWnd->m_pMap->GetDC();
	gpMapWnd->m_pMap->DrawMap(*dc);
	gpMapWnd->m_pMap->ReleaseDC(dc);
    
    pWatershed->SaveShapeFile("c:\\temp\\test.shp",false);

   }//end of method...
Esempio n. 2
0
		/*
	//read Willamette Shapefile
    //read data with 365 rows and 9 cols.  1 column for each major subwatershed (minus the middle Willamette, for which we don't have data. 1 row for each day in year
    //for each day, classify map according to data
    //append to avi file
    //finish
    BOOL m_saveEmfOutput = TRUE;
    if (m_saveEmfOutput)
         { 
         numMovies = 1;
         movieFrameRate = 30;
         AVIGenerators = new CAVIGenerator*[numMovies];  // array of pointers to AVI generators
         auxMovieStuff = new AuxMovieStuff[numMovies];
         CDC* dc = gpView->GetWindowDC();
         // get the window's dimensions
         CRect rect;
         gpView->GetWindowRect(rect);
         for(int i = 0; i < numMovies; i++)
            {
            // get output filename from user
            CFileDialog movieSaveDlg(FALSE, "avi", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Movie Files (*.avi)|*.avi");
            CString movieFileName;
            if (movieSaveDlg.DoModal() == IDOK)
               {
               movieFileName = movieSaveDlg.GetPathName();
               }
            // need to handle canceling out of the file dialog box somehow

            AVIGenerators[i] = new CAVIGenerator(movieFileName, (CView*)gpView, movieFrameRate);

            AVIGenerators[i]->InitEngine();
            auxMovieStuff[i].bitmapDC.CreateCompatibleDC(dc);
            auxMovieStuff[i].bitmap.CreateCompatibleBitmap(dc, rect.Width(), rect.Height());
            auxMovieStuff[i].bitmapDC.SelectObject(&auxMovieStuff[i].bitmap);
            auxMovieStuff[i].bi.bmiHeader = *AVIGenerators[i]->GetBitmapHeader();
            auxMovieStuff[i].lpbi = &auxMovieStuff[i].bi;
            auxMovieStuff[i].bitAddress = new BYTE[3*rect.Width()*rect.Height()];
            }
         gpView->ReleaseDC(dc);
         }
	


    ASSERT ( gpMapWnd != NULL );
    ASSERT ( gpMapWnd->m_pMap != NULL );
	gpMapWnd->m_pMap->AddShapeLayer("c:\\research\\willamette\\will2.shp", true);
	MapLayer *pWill = gpMapWnd->m_pMap->GetLayer(0);
	FDataObj pAsciiData = FDataObj();
	pAsciiData.ReadAscii("c:\\research\\willamette\\willINDI.csv", ',');
	int numDays = pAsciiData.GetRowCount();
	int numWshed = pAsciiData.GetColCount()-1;
	float ti = -1.0f;
    
	gpMapWnd->m_pScatterPrecip = gpMapWnd->AllocateScatter( &pAsciiData, 0 );
	gpMapWnd->m_pScatterPrecip->RedrawWindow();

    for (int i=0;i<numDays;i++)           // for each day of the year (or row in the data)
		{
		int numSheds = pWill->m_pData->GetRowCount();
		float time = -1.0f;
		pAsciiData.Get(0,i,time);
        gpMapWnd->m_pScatterPrecip->UpdateCurrentPos( time );
		//gpMapWnd->m_pScatterPrecip->RedrawWindow();
		for (int j=0;j<numSheds;j++)  // go through each watershed (there are 12 of them)
			{
			float value = -1.0f;                              // and stick the data from pAsciiData into the datatable
			pAsciiData.Get(j+1,i,value);                         //dischargeValue
			pWill->SetData(j,pWill->GetFieldCol("EXTRA_2"),value); //add the value to the correct column in the Maplayer
			
			}
        CString msg;
		COleDateTime t( time );
		CString s = t.Format("%m/%d" );
		msg.Format( "Completed %s" , s );
        gpMain->SetStatusMsg( msg );

		pWill->SetBinColorFlag( BCF_BLUEGREEN );
		pWill->SetBins( -0.2, 0.4, 20 );
		pWill->SetActiveField( pWill->GetFieldCol( "EXTRA_2" ) );
	    pWill->ClassifyData();
        
		if ( gpMapWnd != NULL )
     	   gpMapWnd->RefreshList();

	    gpMapWnd->m_pMapList->RedrawWindow();
	    CDC *dc = gpMapWnd->m_pMap->GetDC();
	    gpMapWnd->m_pMap->DrawMap(*dc);
	    gpMapWnd->m_pMap->ReleaseDC(dc);
   
		if (m_saveEmfOutput)
			{
			CDC* dc = gpView->GetWindowDC();
			CRect rect;
			gpView->GetWindowRect(rect);

			for(int i = 0; i < numMovies; i++)
				{
				// copy from the application window to the new device context (and thus the bitmap)
				BOOL blitSuc = auxMovieStuff[i].bitmapDC.BitBlt(0, 0,
																rect.Width(),
																rect.Height(),
																dc, 0, 0, SRCCOPY);

				GetDIBits(auxMovieStuff[i].bitmapDC, HBITMAP(auxMovieStuff[i].bitmap),
							0, rect.Height(), auxMovieStuff[i].bitAddress,
							auxMovieStuff[i].lpbi, DIB_RGB_COLORS);

				AVIGenerators[i]->AddFrame((BYTE*)auxMovieStuff[i].bitAddress);
				}
			gpView->ReleaseDC(dc);
			}
		}
		

	if(m_saveEmfOutput)
		{
		for(int i = 0; i < numMovies; i++)
			{
			AVIGenerators[i]->ReleaseEngine();
			delete AVIGenerators[i];
			AVIGenerators[i] = 0;

			//ReleaseDC(NULL,auxMovieStuff[i].bitmapDC);
			delete [] auxMovieStuff[i].bitAddress;
			}
		delete [] AVIGenerators;
		delete [] auxMovieStuff;
		AVIGenerators = 0;
		}
	}
   */
void CWet_hView::OnTopographiccalculationDisgradientandquotientforgrid()
   {
   MapLayer *pFlowDir = gpMapWnd->m_pMap->GetLayer( "FLOWDIR" );
   MapLayer *pStreamGrid = gpMapWnd->m_pMap->GetLayer( "STRGRID" );
   MapLayer *pFPDistGrid = gpMapWnd->m_pMap->GetLayer( "FLOWDIRX" );
   MapLayer *pFPDistGrid1 = gpMapWnd->m_pMap->CloneLayer( *pFPDistGrid );
   MapLayer *pFPDistGrid2 = gpMapWnd->m_pMap->CloneLayer( *pFPDistGrid );
   MapLayer *pDEM = gpMapWnd->m_pMap->GetLayer("DEM");
   /*
   float flowPathDistance=0.0f;
   int flowPathCount=0;
   
   float flowPathGradient = 0.0001f;
   int rows = pFlowDir->GetRowCount();
   int cols = pFlowDir->GetColCount();
   int cellCount = rows*cols;
   int sofar=0;
   for ( int row=0; row < rows; row++ )
      {
      for ( int col=0; col < cols; col++ )
         {
         int flowPathCellCount=0;
         float distance = pFlowDir->GetDownFlowPathDistance( row, col, pStreamGrid );
         float gradient = pFlowDir->GetDownFlowPathGradient(row,col,pStreamGrid,pDEM,flowPathCellCount);
         gradient=gradient/flowPathCellCount;        
         float dist_grad = distance/gradient;
         if (distance==pFlowDir->GetNoDataValue())
            {
            dist_grad = pFlowDir->GetNoDataValue();
            gradient = pFlowDir->GetNoDataValue();
            }

         pFPDistGrid->m_pData->Set(col, row, gradient);  
         pFPDistGrid1->m_pData->Set(col, row, distance);  
         pFPDistGrid2->m_pData->Set(col, row, dist_grad);  
         sofar = sofar++;
         gpHydroModel->m_pMap->Notify( NT_CALCDIST, sofar, cellCount ); 
         }  // end of:  for ( col < cols )
      }  // end of:  for ( row < rows )
*/
   float flowPathDistance=0.0f;
   int flowPathCount=0;
    float flowPathGradient = 0.0001f;
   MapLayer *pWatershed = gpMapWnd->m_pMap->GetLayer( "Cells" );
   int polygonCount   = pWatershed->GetRecordCount();
   for ( int i=0; i < polygonCount; i++ )
		{    
		Poly *pThisPoly = pWatershed->m_polyArray[ i ];
      float flowPathDistance=0.0f;
      int flowPathCount=0;
      float flowPathGradient = 0.0001f;
      int rows = pDEM->GetRowCount();
      int cols = pDEM->GetColCount();
      for ( int row=0; row < rows; row++ )
         {
         for ( int col=0; col < cols; col++ )
            {
               float x = 0;
               float y = 0;
               pDEM->GetGridCellCenter(row, col, x, y);
              
               Vertex point;
               point.x = x;
               point.y = y;       
               if (pThisPoly->IsPointInPoly(point))
                  {
                  int flowPathCellCount=0;
                  float distance = pFlowDir->GetDownFlowPathDistance( row, col, pStreamGrid );
                  float gradient = pFlowDir->GetDownFlowPathGradient(row,col,pStreamGrid,pDEM,flowPathCellCount);
                  gradient=gradient/flowPathCellCount;        
                  float dist_grad = distance/gradient;
                  if (distance==pFlowDir->GetNoDataValue())
                     {
                     dist_grad = pFlowDir->GetNoDataValue();
                     gradient = pFlowDir->GetNoDataValue();
                     }
                  
                  pFPDistGrid->m_pData->Set(col, row, (distance/gradient));
                  flowPathGradient +=gradient;
                  flowPathDistance +=distance;

                  }
               }
            }  // end of:  for ( col < cols )
         }  // end of:  for ( row < rows )
      
      float averageFPDistance = flowPathDistance/flowPathCount;
      float averageFPGrad = flowPathGradient/flowPathCount;
      float averageFPDistGrad = averageFPDistance/averageFPGrad;
      //float rt = (0.00881f*averageFPDistGrad) + 0.00314f;
 		gpHydroModel->m_pMap->Notify( NT_CALCDIST, i, polygonCount );   
      pWatershed->SetData(i,pWatershed->m_pData->GetCol("EXTRA_1"), averageFPDistGrad);
















   pFPDistGrid2->SaveGridFile("c:\\temp\\Dist_Grad.asc");
   pFPDistGrid1->SaveGridFile("c:\\temp\\Dist.asc");
   pFPDistGrid->SaveGridFile("c:\\temp\\Grad.asc");
   }