DlgSlope.cpp
上传用户:juying163
上传日期:2014-09-24
资源大小:5867k
文件大小:10k
源码类别:

GIS编程

开发平台:

Visual C++

  1. // DlgSlope.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "3D0214.h"
  5. #include "DlgSlope.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDlgSlope dialog
  13. CDlgSlope::CDlgSlope(CWnd* pParent /*=NULL*/)
  14. : CDialog(CDlgSlope::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CDlgSlope)
  17. m_edit_inputSurf_slope = _T("");
  18. m_edit_OutputCellsizeSlope = 0.0;
  19. m_edit_outputRaster_slope = _T("");
  20. m_degree=-1;
  21. //}}AFX_DATA_INIT
  22. }
  23. void CDlgSlope::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(CDlgSlope)
  27. DDX_Text(pDX, IDC_EDIT_INPUT_SURF_SLOPE, m_edit_inputSurf_slope);
  28. DDX_Text(pDX, IDC_EDIT_OUTPUT_CELLSIZE_SLOPE, m_edit_OutputCellsizeSlope);
  29. DDX_Text(pDX, IDC_EDIT_OUTPUT_RASTER_SLOPE, m_edit_outputRaster_slope);
  30. DDX_Text(pDX, IDC_RADIO_DEGREE_SLOPE, m_degree);
  31. //}}AFX_DATA_MAP
  32. }
  33. BEGIN_MESSAGE_MAP(CDlgSlope, CDialog)
  34. //{{AFX_MSG_MAP(CDlgSlope)
  35. ON_BN_CLICKED(IDC_BUTTON_INPUTSURF_SLOPE, OnButtonInputsurfSlope)
  36. ON_BN_CLICKED(IDC_BUTTON_OUTPUTRASTER_SLOPE, OnButtonOutputrasterSlope)
  37. //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CDlgSlope message handlers
  41. void CDlgSlope::OnButtonInputsurfSlope() 
  42. {
  43. // TODO: Add your control notification handler code here
  44. IGxDialogPtr ipGxDialog(CLSID_GxDialog);
  45.     IGxObjectFilterPtr ipGxObjectFilter( CLSID_GxFilterSurfaceDatasets);
  46.     HRESULT hr=ipGxDialog->putref_ObjectFilter(ipGxObjectFilter);
  47.     if(FAILED(hr)) return;
  48.     hr = ipGxDialog->put_Title(CComBSTR("选择一个DEM表面数据"));
  49.     if(FAILED(hr)) return;
  50. IEnumGxObjectPtr ipEnumGxObject;
  51. VARIANT_BOOL bResult;
  52. hr=ipGxDialog->DoModalOpen(0,&ipEnumGxObject,&bResult);
  53. if(FAILED(hr)) return;
  54.   if(bResult==VARIANT_FALSE) return;
  55. hr=ipEnumGxObject->Reset();
  56. if(FAILED(hr)) return;
  57. IGxObjectPtr ipGxObject;
  58. ipGxDialog->get_FinalLocation( &ipGxObject);
  59.  
  60. while(!ipEnumGxObject->Next(&ipGxObject))
  61. {
  62.  
  63. IGxDatasetPtr ipGxDataset(ipGxObject);
  64. if(ipGxDataset!=0)
  65. {
  66. IDatasetPtr ipDataset;
  67. hr=ipGxDataset->get_Dataset(&ipDataset);
  68. if(FAILED(hr)) return;
  69. IRasterDatasetPtr ipRasterDataset(ipDataset);
  70. if(ipRasterDataset!=NULL)
  71. {
  72. m_ipR=ipRasterDataset;
  73. CComBSTR bstrName;
  74. hr=ipRasterDataset->get_CompleteName(&bstrName);
  75. if(FAILED(hr)) return;
  76. m_edit_inputSurf_slope=bstrName;
  77. IRasterPtr ipRas;
  78. //获得输人Raster表面的网格大小,
  79. //作为输出Raster网格大小的默认值
  80. hr=ipRasterDataset->CreateDefaultRaster(&ipRas);
  81. IRasterAnalysisPropsPtr ipRA(ipRas);
  82. double defaultcellsize;
  83. hr=ipRA->get_PixelHeight( &defaultcellsize);
  84. m_edit_OutputCellsizeSlope=defaultcellsize;
  85. UpdateData(FALSE);
  86. }
  87. ITinPtr ipTin(ipDataset);
  88. if(ipTin!=NULL)
  89. {
  90.   
  91. m_ipT=ipTin ;
  92. CComBSTR bstrName;  
  93. hr= ipDataset->get_Name(&bstrName);  
  94. if(FAILED(hr)) return;  
  95. m_edit_inputSurf_slope=CString(bstrName);    
  96. IGeoDatasetPtr plnTin(m_ipT);    
  97. //通过获得Tin的范围设置默认。ellsize大小    
  98. IEnvelopePtr pExtent;    
  99. hr=plnTin->get_Extent(&pExtent);    
  100. double width,height;  
  101. hr=pExtent->get_Width(&width);    
  102. hr=pExtent->get_Height(&height);    
  103. double defaultcellsize;    
  104. if ( width>height)        
  105. defaultcellsize=height/250;      
  106. else        
  107. defaultcellsize=width/250;    
  108. m_edit_OutputCellsizeSlope=defaultcellsize;    
  109. UpdateData(FALSE);
  110. }
  111. UpdateData(FALSE);
  112. }
  113. }
  114. }
  115. void CDlgSlope::OnButtonOutputrasterSlope() //输出栅格数据路径按钮按下消息响应函数
  116. {
  117. // TODO: Add your control notification handler code here
  118.  IGxDialogPtr ipGxDialog(CLSID_GxDialog);
  119.     //Set the Filter to FeatureDatasets and FeatureClasses
  120.     IGxObjectFilterPtr ipGxObjectFilter(CLSID_GxFilterRasterDatasets);
  121.     HRESULT hr = ipGxDialog->putref_ObjectFilter(ipGxObjectFilter);
  122.     if(FAILED(hr))return;
  123.     hr = ipGxDialog->put_Title(CComBSTR("保存坡度图"));
  124.     if(FAILED(hr)) return;
  125. VARIANT_BOOL bResult;
  126. OLE_HANDLE hwnd=(OLE_HANDLE)this->GetSafeHwnd();
  127. hr=ipGxDialog->DoModalSave(hwnd,&bResult);
  128. if(FAILED(hr))return;
  129. IGxObjectPtr ipGxObject;
  130. ipGxDialog->get_FinalLocation(&ipGxObject);
  131. CComBSTR path;
  132. ipGxObject->get_FullName(&path);
  133. m_OutputPath=CString(path);
  134. CComBSTR name;
  135. ipGxDialog->get_Name(&name);
  136. m_OutputRasterName_Slop=CString(name);
  137. UpdateData(TRUE);
  138. m_edit_outputRaster_slope=m_OutputPath+"\"+m_OutputRasterName_Slop;
  139. UpdateData(FALSE);
  140. }
  141. void CDlgSlope::OnOK() 
  142. {
  143. // TODO: Add extra validation here
  144. ::OleInitialize(NULL);//组件操作初始化 
  145. if(m_ipR!=NULL)
  146. {
  147. UpdateData(TRUE);
  148. CreateSlope(m_ipR);
  149. }
  150. if(m_ipT!=NULL)
  151. {
  152. UpdateData(TRUE);
  153. CreateSlope(m_ipT);
  154. }
  155. CDialog::OnOK();
  156. }
  157. void CDlgSlope::CreateSlope(ITin* ipT) //为TIN创建坡度过程函数。
  158.  ::OleInitialize(NULL);//组件操作初始化 
  159.   HRESULT hr;
  160.     IGeoDatasetPtr pInTin(ipT);
  161. IEnvelopePtr pExtent;
  162. hr=pInTin->get_Extent(&pExtent);
  163.     IPointPtr pOrigin;
  164. hr=pExtent->get_LowerLeft(&pOrigin);
  165. double x,y;
  166. hr=pOrigin->get_X(&x);
  167. hr=pOrigin->get_Y(&y); 
  168. hr=pOrigin->put_X(x-m_edit_OutputCellsizeSlope*0.5);
  169. hr=pOrigin->put_Y(y-m_edit_OutputCellsizeSlope*0.5);
  170. long nCol,nRow;
  171. double width,height;
  172. hr=pExtent->get_Width(&width);
  173. hr=pExtent->get_Height(&height);
  174. nCol=int(width/m_edit_OutputCellsizeSlope)+1;
  175. nRow=int(height/m_edit_OutputCellsizeSlope)+1;
  176. ISpatialReferencePtr pSR;
  177. hr=pInTin->get_SpatialReference(&pSR);
  178. IRasterDatasetPtr pRDS;
  179. IWorkspaceFactoryPtr rWksFac(CLSID_RasterWorkspaceFactory);
  180. IWorkspacePtr wks;
  181. hr = rWksFac->OpenFromFile(CComBSTR(m_OutputPath),0,&wks);
  182. IRasterWorkspace2Ptr rWks(wks);
  183. long numbands=1;
  184.   rWks->CreateRasterDataset((CComBSTR)m_OutputRasterName_Slop,
  185.                                CComBSTR("GRID"),
  186.                                pOrigin,
  187.                                nCol,
  188.                                nRow,
  189.                                m_edit_OutputCellsizeSlope,
  190.                                m_edit_OutputCellsizeSlope,
  191.                                numbands,
  192.                                PT_FLOAT,
  193.                                pSR,
  194.                                VARIANT_TRUE,     
  195.                                &pRDS);
  196. if(FAILED(hr))
  197. {
  198.       AfxMessageBox ( " Raster文件生成失败!");
  199.       return;
  200. }
  201.  AfxMessageBox ( " 1文件生成失败!");
  202. //得到RawPixels
  203. IRasterBandCollectionPtr pBandCollection(pRDS);
  204. long cont;
  205.     CString str;
  206.  hr=pBandCollection->get_Count(&cont);
  207.         str.Format("(%fn",(double) cont);
  208. AfxMessageBox(str); 
  209. IRasterBandPtr pRasterBand;
  210.   AfxMessageBox ( " 2r文件生成失败!");
  211. hr=pBandCollection->Item(0,&pRasterBand);
  212.  AfxMessageBox ( " 3文件生成失败!");
  213.  
  214. IRawPixelsPtr pRawPixels(pRasterBand);
  215.  
  216. IPntPtr pBlockSize(CLSID_DblPnt);
  217. hr=pBlockSize->put_X(nCol);
  218. hr=pBlockSize->put_Y(nRow);
  219. IPixelBlockPtr pPixelBlock;
  220. hr=pRawPixels->CreatePixelBlock(pBlockSize,&pPixelBlock);
  221. VARIANT val;
  222. val.vt = VT_SAFEARRAY ;//要指定类型
  223. hr=pPixelBlock->get_SafeArray(0,&val);
  224. ITinSurfacePtr pTinSurf(ipT); 
  225. IRasterPropsPtr pRasterProps(pRawPixels);
  226. VARIANT nodatafloat;
  227. nodatafloat.vt = VT_R4 ;//要指定类型
  228. // QueryPixelBlock使用左上角点的中心为原点
  229. //下面为计算中心坐标
  230.  
  231. hr=pOrigin->get_X(&x);
  232. hr=pOrigin->get_Y(&y);
  233. hr=pOrigin->put_X(x+m_edit_OutputCellsizeSlope*0.5);
  234. hr = pOrigin->put_Y(y+m_edit_OutputCellsizeSlope*nRow-m_edit_OutputCellsizeSlope*0.5);
  235. hr=pRasterProps->get_NoDataValue(&nodatafloat);
  236. hr = pOrigin->get_X(&x);
  237. hr=pOrigin->get_Y(&y);
  238. esriRasterizationType Type;
  239. UpdateData(TRUE);
  240. m_degree=0;
  241. if ( m_degree==0)
  242. Type=esriDegreeSlopeAsRaster;
  243. if(m_degree==1)
  244. Type=esriPercentageSlopeAsRaster;
  245. if(m_degree ==-1)
  246. {
  247. AfxMessageBox("请选择输出单位!");
  248. return;
  249. }
  250. // AfxMessageBox ( " 2....!");
  251. hr=pTinSurf->QueryPixelBlock(x,y,
  252.                                   m_edit_OutputCellsizeSlope,
  253.                                   m_edit_OutputCellsizeSlope,
  254.                                   Type,
  255.                                   nodatafloat,
  256.                                   val);
  257. IPntPtr pOffset(CLSID_DblPnt);
  258. hr=pOffset->put_X(0);
  259. hr = pOffset->put_Y(0);
  260. hr=pRawPixels->Write(pOffset,pPixelBlock);
  261.     //注意:只有指针pRDS被释放,Raster才会从内存写人硬盘。
  262. }
  263. void CDlgSlope::CreateSlope(IRasterDataset* ipR)  //为Raster创建坡度图过程函数。
  264. {
  265.     HRESULT hr;
  266.    ISurfaceOpPtr pSurfaceOp(CLSID_RasterSurfaceOp);
  267.    IGeoDatasetPtr pInputDataset(ipR);
  268.    //声明输出对象
  269.    IGeoDatasetPtr pOutputRaster;
  270.    //设置输出工作区
  271.    IRasterAnalysisEnvironmentPtr pEnv(pSurfaceOp);
  272.    hr=pEnv->put_VerifyType(esriRasterVerifyOn);
  273.    VARIANT vCellsize;
  274.    vCellsize.vt=VT_R8;
  275.    vCellsize.dblVal=m_edit_OutputCellsizeSlope;
  276.    hr=pEnv->SetCellSize(esriRasterEnvValue,&vCellsize);
  277.    if(FAILED(hr)) return;
  278.    IWorkspacePtr pWo;
  279.    IWorkspaceFactoryPtr pWSF(CLSID_RasterWorkspaceFactory);
  280.    hr=pWSF->OpenFromFile((CComBSTR)m_OutputPath,NULL,&pWo);
  281.    if(FAILED(hr))
  282.    {
  283.    AfxMessageBox("失败!");      
  284.    return;
  285.    }
  286.    //Slope开始
  287.    VARIANT vZfactor;
  288.    vZfactor.vt= VT_R8;
  289.    vZfactor.dblVal=1;
  290.    enum esriGeoAnalysisSlopeEnum slopeType;
  291. UpdateData(TRUE);
  292.    if(m_degree==0) slopeType=esriGeoAnalysisSlopeDegrees;
  293.    if(m_degree==1) slopeType=esriGeoAnalysisSlopePercentrise;
  294.    if(m_degree==-1)
  295.    {
  296.     AfxMessageBox("请选择输出单位!");
  297.         return;
  298.    }
  299.    hr=pSurfaceOp->Slope(pInputDataset,slopeType,&vZfactor,&pOutputRaster);
  300.    if(FAILED(hr))
  301.    {
  302.    AfxMessageBox ("坡度图绘制失败!");
  303.    return;
  304.    }
  305.    IRasterBandCollectionPtr pBands(pOutputRaster);
  306.    IDatasetPtr pRD;
  307.    hr= pBands->SaveAs(CComBSTR(m_OutputRasterName_Slop),
  308.                                    pWo,
  309.    CComBSTR("GRID"),
  310.                                    &pRD);
  311. }