Exemple #1
0
        public bool RasterSubsetByPolygon(IRasterDataset pRasterDataset, IPolygon clipGeo, string FileName)
        {
            try
            {
                IRasterBandCollection rasterBands = pRasterDataset as IRasterBandCollection;
                int nBandCount = rasterBands.Count;
                //设置输出栅格参数
                IRasterLayer rasterLayer = new RasterLayerClass();
                rasterLayer.CreateFromDataset(pRasterDataset);
                IRaster pRaster = rasterLayer.Raster;//此处只得到前3个波段
                IRasterBandCollection bandsNew = pRaster as IRasterBandCollection;
                IRasterBand           pBand    = null;

                for (int i = 3; i < nBandCount; i++)
                {
                    pBand = rasterBands.Item(i);
                    bandsNew.AppendBand(pBand);
                }

                IRasterProps pProps           = pRaster as IRasterProps;
                object       cellSizeProvider = pProps.MeanCellSize().X;
                IGeoDataset  pInputDataset    = pRaster as IGeoDataset;

                //IGeoDataset pInputDataset = pRasterDataset as IGeoDataset;//此种方式也只是得到前3个波段

                //设置格格处理环境
                IExtractionOp2             pExtractionOp   = new RasterExtractionOpClass();
                IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;
                pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);

                object extentProvider = clipGeo.Envelope;
                object snapRasterData = Type.Missing;
                pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);

                IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true); //裁切操作

                IRaster clipRaster;                                                                           //裁切后得到的IRaster
                if (pOutputDataset is IRasterLayer)
                {
                    IRasterLayer rasterLayer2 = pOutputDataset as IRasterLayer;
                    clipRaster = rasterLayer.Raster;
                }
                else if (pOutputDataset is IRasterDataset)
                {
                    IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
                    clipRaster = rasterDataset.CreateDefaultRaster();
                }
                else if (pOutputDataset is IRaster)
                {
                    clipRaster = pOutputDataset as IRaster;
                    IRasterProps temRastPro = clipRaster as IRasterProps;
                    temRastPro.NoDataValue = pProps.NoDataValue;
                    temRastPro.PixelType   = rstPixelType.PT_UCHAR;
                }
                else
                {
                    return(false);
                }
                //保存裁切后得到的clipRaster
                //如果直接保存为img影像文件
                //判断保存类型
                string strFileType = System.IO.Path.GetExtension(FileName);
                switch (strFileType.ToUpper())
                {
                case "TIF":
                    strFileType = "TIFF";
                    break;

                case "IMG":
                    strFileType = "IMAGINE Image";
                    break;

                default:
                    strFileType = "TIFF";
                    break;
                }
                IWorkspaceFactory pWKSF      = new RasterWorkspaceFactoryClass();
                IWorkspace        pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(FileName), 0);
                ISaveAs           pSaveAs    = clipRaster as ISaveAs;
                IDataset          pDataset   = pSaveAs.SaveAs(FileName, pWorkspace, strFileType);//以TIF格式保存
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pDataset);
                MessageBox.Show("\"" + FileName + "\"成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(true);
                //MessageBox.Show("成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return(false);
            }
        }
        public bool SaveAs(bool bSetRepresentationType = false, rstRepresentationType representationType = rstRepresentationType.DT_ATHEMATIC, bool bUseMessagebox = true)
        {
            object editValue = this.m_checkedCmbBand.EditValue;
            bool   result;

            if (editValue == null)
            {
                XtraMessageBox.Show("请设置输出波段!", "提示信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
                result = false;
            }
            else if (string.IsNullOrEmpty(this.m_sOutputPath))
            {
                XtraMessageBox.Show("请选择文件输出路径!", "提示信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
                result = false;
            }
            else
            {
                FileInfo fileInfo = new FileInfo(this.m_sOutputPath);
                if (fileInfo.Exists)
                {
                    if (XtraMessageBox.Show("此文件已存在,是否覆盖?", "提示信息", System.Windows.Forms.MessageBoxButtons.OKCancel, System.Windows.Forms.MessageBoxIcon.Asterisk) != System.Windows.Forms.DialogResult.OK)
                    {
                        result = false;
                        return(result);
                    }
                    fileInfo.Delete();
                }
                IRaster  raster = this.m_raster;
                string[] array  = editValue.ToString().Split(new char[]
                {
                    ','
                });
                if (array.Length != this.m_bandList.Count)
                {
                    IRaster      raster2     = ((this.m_raster as IRaster2).RasterDataset as IRasterDataset3).CreateRaster();
                    IRasterProps rasterProps = raster2 as IRasterProps;
                    rasterProps.Width  = (this.m_raster as IRasterProps).Width;
                    rasterProps.Height = (this.m_raster as IRasterProps).Height;
                    IRasterBandCollection rasterBandCollection  = raster2 as IRasterBandCollection;
                    IRasterBandCollection rasterBandCollection2 = (this.m_raster as IRaster2).RasterDataset as IRasterBandCollection;
                    for (int i = 0; i < rasterBandCollection2.Count; i++)
                    {
                        IRasterBand rasterBand = rasterBandCollection2.Item(i);
                        string[]    array2     = array;
                        for (int j = 0; j < array2.Length; j++)
                        {
                            string text = array2[j];
                            if (text.Trim() == rasterBand.Bandname)
                            {
                                rasterBandCollection.AppendBand(rasterBand);
                                break;
                            }
                        }
                    }
                    raster = raster2;
                }
                this.SetResamplingMethod(raster, this.m_cmbResamplingType);
                this.SetPixelType(raster, this.m_cmbPixelType);
                if (bSetRepresentationType)
                {
                    this.SetRepresentationType(raster, representationType);
                }
                frmWaitDialog frmWait   = new frmWaitDialog("正在输出......", "提示信息");
                IWorkspace    workspace = null;
                string        text2     = "输出成功!";
                //Logger logger = new Logger();
                try
                {
                    frmWait.Owner   = this.m_frm;
                    frmWait.TopMost = false;
                    string   text3  = this.m_cmbOutputType.Text;
                    ISaveAs2 saveAs = raster as ISaveAs2;
                    if (!saveAs.CanSaveAs(text3))
                    {
                        XtraMessageBox.Show("不支持指定像素类型或文件格式的输出!", "提示信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
                        result = false;
                    }
                    else
                    {
                        IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
                        workspace = workspaceFactory.OpenFromFile(Path.GetDirectoryName(this.m_sOutputPath), 0);
                        IDataset o = saveAs.SaveAs(Path.GetFileName(this.m_sOutputPath), workspace, text3);
                        if (o != null)
                        {
                            Marshal.ReleaseComObject(o);
                        }
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    text2  = "输出失败!";
                    result = false;
                }
                finally
                {
                    if (workspace != null)
                    {
                        Marshal.ReleaseComObject(workspace);
                    }
                    frmWait.Close();
                    if (bUseMessagebox)
                    {
                        XtraMessageBox.Show(text2, "提示信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
                    }
                }
            }
            return(result);
        }
Exemple #3
0
        //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);
            }
        }