/// <summary> /// Raster图层导入到数据库 /// </summary> /// <param name="dbWorkspace">数据库</param> /// <param name="strRasterFileDir">本地栅格图层路径</param> /// <param name="strRasterFileName">本地栅格图层名称</param> /// <param name="strOutName">数据库栅格图层名称</param> /// <returns></returns> public IRasterDataset RasterFileIntoGDB(IWorkspace dbWorkspace, string strRasterFileDir, string strRasterFileName, string strOutName) { IWorkspace pSdeWorkSpace = dbWorkspace; try { //判断是否有重名现象 IWorkspace2 pWS2 = pSdeWorkSpace as IWorkspace2; //如果名称已存在 if (pWS2.get_NameExists(esriDatasetType.esriDTRasterDataset, strOutName)) { DialogResult result; result = MessageBox.Show("栅格文件名 " + strOutName + " 在数据库中已存在!" + "\r是否覆盖?", "相同文件名", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); //覆盖原栅格要素 if (result == DialogResult.Yes) { IRasterWorkspaceEx pRWs = pSdeWorkSpace as IRasterWorkspaceEx; IDataset pDataset = pRWs.OpenRasterDataset(strOutName) as IDataset; pDataset.Delete(); pDataset = null; } else if (result == DialogResult.No) { //不覆盖,则退出for循环,忽略这个要素,转入下一个要素的导入 return(null); } } IWorkspaceFactory pRasterWsFac = new RasterWorkspaceFactoryClass(); IWorkspace pWs = pRasterWsFac.OpenFromFile(strRasterFileDir, 0); IRasterDataset pRasterDs = null; IRasterWorkspace pRasterWs; if (!(pWs is IRasterWorkspace)) { MessageBox.Show("错误信息:" + strRasterFileDir + "不是栅格工作空间。"); return(null); } pRasterWs = pWs as IRasterWorkspace; pRasterDs = pRasterWs.OpenRasterDataset(strRasterFileName); ISaveAs2 saveAs2 = (ISaveAs2)pRasterDs; IRasterStorageDef rasterStorageDef = new RasterStorageDefClass(); IRasterStorageDef2 rasterStorageDef2 = (IRasterStorageDef2)rasterStorageDef; rasterStorageDef2.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000; rasterStorageDef2.CompressionQuality = 50; rasterStorageDef2.Tiled = true; rasterStorageDef2.TileHeight = 128; rasterStorageDef2.TileWidth = 128; saveAs2.SaveAsRasterDataset(strOutName, pSdeWorkSpace, "JP2", rasterStorageDef2); return(pRasterDs); } catch (Exception ex) { MessageBox.Show("错误信息:" + ex.Message); return(null); } }
//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); } }