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

GIS编程

开发平台:

Visual C++

  1. // DlgCreateRasterIDW.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "3D0214.h"
  5. #include "DlgCreateRasterIDW.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CDlgCreateRasterIDW dialog
  13. CDlgCreateRasterIDW::CDlgCreateRasterIDW(CWnd* pParent /*=NULL*/)
  14. : CDialog(CDlgCreateRasterIDW::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CDlgCreateRasterIDW)
  17. m_edit_IDWInputCellsize = 0.0;
  18. m_edit_IDWOutputRaster = _T("");
  19. m_edit_IDWPointInput = _T("");
  20. m_edit_IDWPower = 0.0;
  21. //}}AFX_DATA_INIT
  22. }
  23. void CDlgCreateRasterIDW::DoDataExchange(CDataExchange* pDX)
  24. {
  25. CDialog::DoDataExchange(pDX);
  26. //{{AFX_DATA_MAP(CDlgCreateRasterIDW)
  27. DDX_Control(pDX, IDC_EDIT_IDW_OUTPUT_RASTER, m_Control_IDWOutputRaster);
  28. DDX_Control(pDX, IDC_STATIC_Set2, m_static_settings2);
  29. DDX_Control(pDX, IDC_STATIC_Set1, m_static_settings1);
  30. DDX_Control(pDX, IDC_EDIT_IDW_RADIUSSETTINGS_2, m_edit_settings2);
  31. DDX_Control(pDX, IDC_EDIT_IDW_RADIUSSETTINGS_1, m_edit_settings1);
  32. DDX_Control(pDX, IDC_COMBO_IDW_SEARCH_RADIUS_TYPE, m_combo_IDWsearchRadiusType);
  33. DDX_Control(pDX, IDC_COMBO_IDW_ZVALUE_FIELD, m_combo_IDWZvalueField);
  34. DDX_Text(pDX, IDC_EDIT_IDW_INPUT_CELL_SIZE, m_edit_IDWInputCellsize);
  35. DDX_Text(pDX, IDC_EDIT_IDW_OUTPUT_RASTER, m_edit_IDWOutputRaster);
  36. DDX_Text(pDX, IDC_EDIT_IDW_POINT_INPUT, m_edit_IDWPointInput);
  37. DDX_Text(pDX, IDC_EDIT_IDW_WIGHT, m_edit_IDWPower);
  38. //}}AFX_DATA_MAP
  39. }
  40. BEGIN_MESSAGE_MAP(CDlgCreateRasterIDW, CDialog)
  41. //{{AFX_MSG_MAP(CDlgCreateRasterIDW)
  42. ON_CBN_SELCHANGE(IDC_COMBO_IDW_SEARCH_RADIUS_TYPE, OnSelchangeComboIdwSearchRadiusType)
  43. ON_BN_CLICKED(IDC_BUTTON_IDW_ADDPOINTLAYER, OnButtonIdwAddpointlayer)
  44. ON_BN_CLICKED(IDC_BUTTON_SELSAVERASTERPATH, OnButtonSelsaverasterpath)
  45. //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CDlgCreateRasterIDW message handlers
  49. BOOL CDlgCreateRasterIDW::OnInitDialog() 
  50. {
  51. CDialog::OnInitDialog();
  52. // TODO: Add extra initialization here
  53. //将搜索范围默认选项设置为"可变的
  54. m_combo_IDWsearchRadiusType.SetCurSel(0);
  55. m_edit_settings1.SetWindowText("12");
  56. m_selIndex=0;
  57. // m_combo_IDWZvalueField. AddString("可变的");
  58. return TRUE;  // return TRUE unless you set the focus to a control
  59.               // EXCEPTION: OCX Property Pages should return FALSE
  60. }
  61. void CDlgCreateRasterIDW::OnSelchangeComboIdwSearchRadiusType() 
  62. {
  63. // TODO: Add your control notification handler code here
  64. if(m_combo_IDWsearchRadiusType.GetCurSel()!=CB_ERR)
  65. {
  66. m_selIndex=m_combo_IDWsearchRadiusType.GetCurSel();
  67. if ( m_selIndex == 0 ) //**当选中"可变的"
  68. {
  69. m_static_settings1.SetWindowText("点的数目:");
  70. m_static_settings2.SetWindowText("最大距离:");
  71. m_edit_settings1.SetWindowText("12");
  72. m_edit_settings2.SetWindowText("");
  73. }
  74. if( m_selIndex ==1) //*当选中"固定的"
  75. {
  76. m_static_settings1.SetWindowText("距离:");
  77. m_static_settings2.SetWindowText("最少点数:");
  78. m_edit_settings1.SetWindowText("");
  79. m_edit_settings2.SetWindowText("0");
  80. }
  81. }
  82. }
  83. void CDlgCreateRasterIDW::OnButtonIdwAddpointlayer() 
  84. {
  85. // TODO: Add your control notification handler code here
  86. ::OleInitialize(NULL);//组件操作初始化
  87. //打开对话框
  88. IGxDialogPtr ipGxDialog(CLSID_GxDialog);
  89.  IGxObjectFilterPtr ipGxObjectFilter(CLSID_GxFilterPointFeatureClasses);   
  90. //注意使用了GxFilterPointFeatureClasses
  91. //可以过滤点特征集,满足功能只用点内插的需要
  92.     HRESULT hr=ipGxDialog->putref_ObjectFilter(ipGxObjectFilter);
  93. //HRESULT hr;
  94. if(FAILED(hr)) return;
  95. hr=ipGxDialog->put_Title(::SysAllocString(L"选择数据"));
  96. if(FAILED(hr)) return;
  97. IEnumGxObjectPtr ipEnumGxObject;
  98. VARIANT_BOOL bResult;
  99. hr=ipGxDialog->DoModalOpen(0,&ipEnumGxObject,&bResult);
  100. if(FAILED(hr)) return;
  101. if(bResult==VARIANT_FALSE) return;
  102. hr=ipEnumGxObject->Reset();///
  103. if(FAILED(hr)) return;
  104. IGxObjectPtr ipGxObject;
  105. while(!ipEnumGxObject->Next( &ipGxObject))
  106. {
  107. IGxDatasetPtr ipGxDataset( ipGxObject);
  108. if(ipGxDataset!=0)
  109. {
  110. IDatasetPtr ipDataset;
  111. hr=ipGxDataset->get_Dataset( &ipDataset);
  112. if(FAILED(hr)) return;
  113. IFeatureClassPtr ipFeatureClass(ipDataset);
  114. m_ipfeat=ipFeatureClass;
  115. CComBSTR bstrName ;//获得输人点层的全名
  116. ipGxObject->get_FullName(&bstrName);
  117. CString str(bstrName);
  118. m_edit_IDWPointInput=str;
  119. UpdateData(FALSE);
  120. //将点层ipFeatureClass的所有字段添加到。ombo box控件列表中
  121. m_combo_IDWZvalueField.ResetContent();//先清空
  122. HRESULT hr;
  123. long lFieldCount;
  124. IFieldsPtr pFClassPointFields;
  125. m_ipfeat->get_Fields(&pFClassPointFields);
  126. pFClassPointFields->get_FieldCount(&lFieldCount);
  127. for(int i=0;i<lFieldCount;i++)
  128. {    
  129. CComBSTR pname;    
  130. IFieldPtr pField;   
  131. hr=pFClassPointFields->get_Field(i,&pField);    
  132. if(FAILED(hr )) return;   
  133. hr=pField->get_Name(&pname);    
  134. if( FAILED(hr)) return;   
  135. CString ppname(pname);    
  136. m_combo_IDWZvalueField. AddString(ppname);
  137. }
  138. m_combo_IDWZvalueField.SetCurSel(0);
  139. //初始化显示。omboBox(3)
  140. }
  141. }
  142. }
  143. void CDlgCreateRasterIDW::OnOK() 
  144. {
  145. // TODO: Add extra validation here
  146. UpdateData(TRUE);
  147. if(m_ipfeat!=NULL)
  148. {
  149. IDWOp(m_ipfeat);
  150. }
  151. else
  152. {
  153. AfxMessageBox("没有输人点层!"); return;
  154. }
  155. CDialog::OnOK();
  156. }
  157. void CDlgCreateRasterIDW::IDWOp(IFeatureClass * ipfeatureclass) 
  158. {
  159. IFeatureClassDescriptorPtr pFDescr(CLSID_FeatureClassDescriptor);
  160. //获得选中字段的字符串
  161. int index=m_combo_IDWZvalueField.GetCurSel();
  162. CString sFieldName;
  163. m_combo_IDWZvalueField.GetLBText(index,sFieldName);
  164. CComBSTR s=(CComBSTR)sFieldName;
  165. HRESULT hr;
  166. hr=pFDescr->Create(ipfeatureclass,NULL,s);
  167. //创建RasterlnterpolationOp对象
  168. IInterpolationOpPtr pIntOp(CLSID_RasterInterpolationOp);
  169. //设置输出RASTER网格大小,默认的RASTER输出空间范围和输人数据相同
  170. IRasterAnalysisEnvironmentPtr pEnv(pIntOp);
  171. VARIANT cs;
  172. cs.vt=VT_R8;
  173. cs .dblVal=m_edit_IDWInputCellsize; //对话框输入cell 尺寸
  174. hr = pEnv->SetCellSize(esriRasterEnvValue,&cs);
  175. //搜索范围设置
  176. IRasterRadiusPtr pRadius(CLSID_RasterRadius);
  177.   if(m_selIndex==0)
  178. {
  179. CString strl;
  180. m_edit_settings1.GetWindowText(strl);
  181. long count=atoi(strl); CString str2;
  182. m_edit_settings2.GetWindowText(str2);
  183. double distance=atof ( str2);
  184. VARIANT dis;
  185. dis.vt=VT_R8;
  186. dis. dblVal=distance;    
  187. hr=pRadius->SetVariable(count,&dis);
  188. }
  189.   if(m_selIndex==1)
  190. {     
  191. CString str1,str2;    
  192. m_edit_settings1.GetWindowText(str1);    
  193. double distance=atof( str1);    
  194. m_edit_settings2.GetWindowText(str2);
  195.      long count=atoi(str2);    
  196. VARIANT cou;
  197. cou. vt=VT_I4;
  198. cou.lVal=count;
  199. hr=pRadius->SetFixed(distance,&cou); 
  200. }
  201. //使用FeatureClassDescriptor作为IInterpolationOp的输人对象进行内插操作
  202. IGeoDatasetPtr pFDescr2Geo(pFDescr);
  203. IGeoDatasetPtr pOutRaster2Geo;
  204. hr=pIntOp->IDW(pFDescr2Geo,m_edit_IDWPower,pRadius,NULL,&pOutRaster2Geo);
  205. if(FAILED("hr"))
  206. {
  207. AfxMessageBox ("内插操作失败!");
  208. return;
  209. }    
  210. IRasterPtr pOutRaster(pOutRaster2Geo);    
  211. IRasterBandCollectionPtr pRasBandC(pOutRaster);    
  212. //输出RASTER路径、名称及其格式设置    
  213. IWorkspaceFactoryPtr pWSF(CLSID_RasterWorkspaceFactory);    
  214. IWorkspacePtr pWS; 
  215. hr= pWSF->OpenFromFile((CComBSTR)m_edit_IDWOutputRaster,NULL,&pWS); 
  216.      if(FAILED(hr)) AfxMessageBox("failed");
  217. //RASTER格式可以为"GRID" , " TIFF"或者"IMAGINE Image"    
  218. IDatasetPtr prasterdataset;    
  219. hr=pRasBandC->SaveAs(CComBSTR("Raster"),pWS,CComBSTR("GRID"),&prasterdataset);    
  220. if(FAILED( hr))
  221.      {        
  222. AfxMessageBox ( " DEM保存失败!");    
  223. }
  224. }
  225. void CDlgCreateRasterIDW::OnButtonSelsaverasterpath() 
  226. {
  227. // TODO: Add your control notification handler code here
  228.     CString strFilter="File Folder(*.File Folder)|*.File Folder||";//设置打开文件的过滤条件
  229. CString strTitle="选择保存路径";
  230.         DWORD MAXFILE = 2412; // allocate enough memory space
  231.      CString strPath,strInit,strTemp;
  232.         CFileDialog pDlg(FALSE,0,0,OFN_ALLOWMULTISELECT| OFN_EXPLORER | OFN_LONGNAMES|
  233.                         OFN_FILEMUSTEXIST |OFN_HIDEREADONLY,strFilter, NULL);
  234.         //设置对话框               
  235.         pDlg.m_ofn.nMaxFile = MAXFILE; // set the buffer size        
  236.         pDlg.m_ofn.lpstrTitle=strTitle;
  237.         pDlg.m_ofn.lpstrInitialDir=strInit;
  238. char* buf = new char[MAXFILE];
  239. pDlg.m_ofn.lpstrFile = buf;
  240. pDlg.m_ofn.lpstrFile[0] = NULL;
  241. if(pDlg.DoModal()== IDOK)
  242. {
  243. //POSITION pos = pDlg.GetStartPosition();
  244. strPath=pDlg.GetPathName();
  245. //strPath=pDlg.GetFolderPath();
  246. m_Control_IDWOutputRaster.SetWindowText(strPath);
  247. AfxMessageBox(strPath,MB_YESNO);
  248. //AfxMessageBox("Do you really want to exit?",MB_YESNO);
  249. }
  250. }