/// <summary> /// 降维 /// </summary> public bool BandReduce(out string msg) { IRasterDataset pRasterDataset = null; IRaster pRaster = null; StreamReader sr = null; IWorkspaceFactory pWKSF = null; IWorkspace pWorkspace = null; try { //调用IDL代码降维,存在32位内存不足问题。 _Hyper_BandReduce = System.IO.Path.Combine(BLL.ConstDef.PATH_TEMP, DateTime.Now.ToFileTime().ToString() + ".tif"); //string cmd = BLL.ConstDef.IDL_FUN_BANDREDUCE + ",'" + _Hyper + "','" + ConstDef.FILE_BANDS + "','" + _Hyper_BandReduce + "'"; //BLL.EnviVars.instance.IdlModel.Execute(cmd); //BLL.EnviVars.instance.IdlModel.Distroy(); sr = new StreamReader(ConstDef.FILE_BANDS, Encoding.Default); string strBands = sr.ReadLine(); string[] bandsArr = strBands.Split(','); pRasterDataset = EngineAPI.OpenRasterFile(_Hyper); pRaster = (pRasterDataset as IRasterDataset2).CreateFullRaster(); IRasterBandCollection pRasterBandCollection = pRaster as IRasterBandCollection; foreach (string str in bandsArr) { int bandIndex = int.Parse(str) - 1; if (-1 < bandIndex && bandIndex < pRasterBandCollection.Count) { pRasterBandCollection.Remove(bandIndex); } } IRasterBand pBand = pRasterBandCollection.Item(0); IRasterDataset pOutRasterDataset = pBand.RasterDataset; ISaveAs pSaveAs = pOutRasterDataset as ISaveAs; FileInfo fInfo = new FileInfo(_Hyper_BandReduce); pWKSF = new RasterWorkspaceFactoryClass(); pWorkspace = pWKSF.OpenFromFile(fInfo.DirectoryName, 0); if (fInfo.Extension == ".img") { IDataset pDataset = pSaveAs.SaveAs(fInfo.Name, pWorkspace, "IMAGINE Image"); Marshal.ReleaseComObject(pDataset); } if (fInfo.Extension == ".tif") { IDataset pDataset = pSaveAs.SaveAs(fInfo.Name, pWorkspace, "TIFF"); Marshal.ReleaseComObject(pDataset); } Marshal.ReleaseComObject(pSaveAs); msg = ""; return(true); } catch (Exception ex) { msg = ex.Message; return(false); } finally { if (pWorkspace != null) { Marshal.ReleaseComObject(pWorkspace); } if (pWKSF != null) { Marshal.ReleaseComObject(pWKSF); } if (pRaster != null) { Marshal.ReleaseComObject(pRaster); } sr.Close(); sr.Dispose(); } }
//private rstPixelType GetRasterPixelType(string strType) //{ // if (strType == rstPixelType.PT_CHAR.ToString()) // { // return rstPixelType.PT_CHAR; // } // else if (strType == rstPixelType.PT_UCHAR.ToString()) // { // return rstPixelType.PT_UCHAR; // } // else if (strType == rstPixelType.PT_SHORT.ToString()) // { // return rstPixelType.PT_SHORT; // } // else if (strType == rstPixelType.PT_USHORT.ToString()) // { // return rstPixelType.PT_USHORT; // } // else if (strType == rstPixelType.PT_LONG.ToString()) // { // return rstPixelType.PT_LONG; // } // else if (strType == rstPixelType.PT_ULONG.ToString()) // { // return rstPixelType.PT_ULONG; // } // else if (strType == rstPixelType.PT_CSHORT.ToString()) // { // return rstPixelType.PT_CSHORT; // } // else if (strType == rstPixelType.PT_CLONG.ToString()) // { // return rstPixelType.PT_CLONG; // } // else if (strType == rstPixelType.PT_FLOAT.ToString()) // { // return rstPixelType.PT_FLOAT; // } // else if (strType == rstPixelType.PT_DOUBLE.ToString()) // { // return rstPixelType.PT_DOUBLE; // } // return rstPixelType.PT_CHAR; //} //private rstResamplingTypes GetRasterResample(string strType) //{ // if (strType == "NearestNeighbor") // { // return rstResamplingTypes.RSP_NearestNeighbor; // } // else if (strType == "BilinearInterpolation") // { // return rstResamplingTypes.RSP_BilinearInterpolation; // } // else if (strType == "CubicConvolution") // { // return rstResamplingTypes.RSP_CubicConvolution; // } // else if (strType == "Majority") // { // return rstResamplingTypes.RSP_Majority; // } // return rstResamplingTypes.RSP_NearestNeighbor; //} #endregion private bool ExportToTif() { if (!txtOutData.Text.EndsWith("tif")) { MessageBox.Show("输出文件名不是tif文件!"); return(false); } String strFullName = txtOutData.Text; string strPath = System.IO.Path.GetDirectoryName(strFullName); //导出文件路径 string strName = System.IO.Path.GetFileName(strFullName); //导出文件名 try { IRasterBandCollection bandsOut = m_pRaster as IRasterBandCollection; IRasterBandCollection rasterBands = m_pRaster.RasterDataset as IRasterBandCollection; double[] dNodata; int nBandOut = 1; //IRaster pRasterOut = null; IRasterBand pBand = null; if (comboBoxExBands.SelectedIndex == 0)//所有波段 { //添加其他波段 for (int i = 3; i < rasterBands.Count; i++) { pBand = rasterBands.Item(i); bandsOut.AppendBand(pBand); } nBandOut = rasterBands.Count; } else { #region 原代码 //pRasterOut = new RasterClass(); //IRasterBandCollection pRB2 = pRasterOut as IRasterBandCollection; //pRB2.AppendBand(rasterBands.Item(comboBoxExBands.SelectedIndex - 1)); //m_pRasterProps = pRasterOut as IRasterProps; #endregion //导出单波段时,不能用Clear(),会清除图层的几何校正属性 int nOut = bandsOut.Count; for (int i = 0; i < nOut; i++) { bandsOut.Remove(i); } pBand = rasterBands.Item(comboBoxExBands.SelectedIndex - 1); bandsOut.AppendBand(pBand); } //重新设置NoData dNodata = new double[nBandOut]; if (!string.IsNullOrEmpty(textNoData.Text)) { for (int i = 0; i < nBandOut; i++) { dNodata[i] = Convert.ToDouble(textNoData.Text); } m_pRasterProps.NoDataValue = dNodata; } //else //{ // for (int i = 0; i < nBandOut; i++) // { // dNodata[i] = ClsGDBDataCommon.getNoDataValue((rasterBands.Item(i) as IRasterProps).NoDataValue);// Convert.ToDouble((rasterBands.Item(i) as IRasterProps).NoDataValue); // } // m_pRasterProps.NoDataValue = dNodata; //} IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass(); IWorkspace pWS = pWSF.OpenFromFile(System.IO.Path.GetDirectoryName(txtOutData.Text), 0); //导出时要保持分辨率和行列数 m_pRasterProps.Width = Convert.ToInt32(txtOutColumns.Text); m_pRasterProps.Height = Convert.ToInt32(txtOutRows.Text); double dcellSizeX = double.Parse(txtCellSizeX.Text); double dcellSizeY = double.Parse(txtCellSizeY.Text); IEnvelope pEnvelope = new EnvelopeClass(); pEnvelope.XMin = m_pRasterProps.Extent.UpperLeft.X; pEnvelope.YMax = m_pRasterProps.Extent.UpperLeft.Y; pEnvelope.XMax = m_pRasterProps.Extent.UpperLeft.X + m_pRasterProps.Width * dcellSizeX; pEnvelope.YMin = m_pRasterProps.Extent.UpperLeft.Y - m_pRasterProps.Height * dcellSizeY; m_pRasterProps.Extent = pEnvelope; //设置存储位数 m_pRasterProps.PixelType = (rstPixelType)cmbPixelType.SelectedIndex; ISaveAs2 pSaveAs = m_pRasterProps as ISaveAs2; IRasterStorageDef pRSDef = new RasterStorageDefClass(); IRasterStorageDef2 pRsDef2 = pRSDef as IRasterStorageDef2; //将存储栅格的分辨率设置为与原始图像相同,其实经过上面行列计算之后已经是相同的 IPnt pPntdec = new PntClass(); pPntdec.X = dcellSizeX; pPntdec.Y = dcellSizeY; pRsDef2.CellSize = pPntdec; pRsDef2.PyramidResampleType = (rstResamplingTypes)cmbResample.SelectedIndex; IRasterDataset pDataset = pSaveAs.SaveAsRasterDataset(System.IO.Path.GetFileName(txtOutData.Text), pWS, "TIFF", pRSDef); IRasterDatasetEdit3 rasterEdit3 = pDataset as IRasterDatasetEdit3; //rasterEdit3.DeleteStats();//This method is avaliable only on raster datasets in File and ArcSDE geodatabases. rasterEdit3.ComputeStatisticsHistogram(1, 1, null, true); //导出数据之后要恢复图像的原始属性 m_pRasterProps.SpatialReference = m_pSpatialRef; m_pRasterProps.Extent = m_Envelope; m_pRasterProps.Width = m_nCols; m_pRasterProps.Height = m_nRows; //加到当前地图中 IRasterLayer layerNew = new RasterLayerClass(); layerNew.CreateFromDataset(pDataset); m_pMap.AddLayer(layerNew); IActiveView activeView = m_pMap as IActiveView; activeView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); return(true); } catch (Exception exc) { MessageBox.Show(exc.Message, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); } }