Ejemplo n.º 1
0
        public static void RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo)
        {
            IRaster       pRaster                      = pRasterLayer.Raster;
            IRasterProps  pProps                       = pRaster as IRasterProps;
            object        cellSizeProvider             = pProps.MeanCellSize().X;
            IGeoDataset   pInputDataset                = pRaster as IGeoDataset;
            IExtractionOp 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.esriRasterEnvMinOf, ref extentProvider, ref snapRasterData);
            //IWorkspaceFactory pWrokspaceFactory = new RasterWorkspaceFactoryClass();
            //if (flag == 0)
            //{
            //    flag++;
            //}
            //if (flag == 1)
            //{
            //    pRasterAnaEnvir.OutWorkspace = pWrokspaceFactory.OpenFromFile("D://temp", 0);
            //}
            IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo, true); //裁切操作
            IRaster     clipRaster     = pOutputDataset as IRaster;                           //裁切后得到的IRaster

            //保存裁切后得到的clipRaster
            //如果直接保存为img影像文件
            IWorkspaceFactory pWKSF    = new RasterWorkspaceFactory();
            SaveFileDialog    savefile = new SaveFileDialog();

            savefile.Title  = "选择输出影像";
            savefile.Filter = "栅格文件(*.tif)|*.tif|栅格文件(*.img)|*.img";
            if (savefile.ShowDialog() == DialogResult.OK)
            {
                IWorkspace pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(savefile.FileName), 0);
                ISaveAs    pSaveAs    = clipRaster as ISaveAs;
                pSaveAs.SaveAs(System.IO.Path.GetFileName(savefile.FileName), pWorkspace, "TIFF");//以img格式保存;
                MessageBox.Show("裁切成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Ejemplo n.º 2
0
        private IRaster RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo)
        {
            if (clipGeo == null)
            {
                return(null);
            }

            IRaster clipRaster = null;

            IRaster       pRaster                      = pRasterLayer.Raster;
            IRasterProps  pProps                       = pRaster as IRasterProps;
            object        cellSizeProvider             = pProps.MeanCellSize().X;
            IGeoDataset   pInputDataset                = pRaster as IGeoDataset;
            IExtractionOp 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);

            if (pOutputDataset is IRasterLayer)
            {
                IRasterLayer rasterLayer = 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;
            }

            return(clipRaster);
        }
Ejemplo n.º 3
0
        public void RasterMeaAtt(IRaster pRaster, IGeometry pGeoMetry, ref double pixelmax, ref double pixelmin)
        {
            IPolygon      clipGeo                      = pGeoMetry as IPolygon;
            IRasterProps  pProps                       = pRaster as IRasterProps;
            object        cellSizeProvider             = pProps.MeanCellSize().X;
            IGeoDataset   pInputDataset                = pRaster as IGeoDataset;
            IExtractionOp 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     = null;                                                            //裁切后得到的IRaster

            if (pOutputDataset is IRasterLayer)
            {
                IRasterLayer rasterLayer = 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;
            }
            else
            {
                //return false;
            }

            CalRasterAtt(clipRaster as IRaster2, ref pixelmax, ref pixelmin);
        }
Ejemplo n.º 4
0
 private IRasterLayer IntersectRaster(IRasterLayer rsterLayer, IPolygon polygon2)
 {
     //  获得与组合成的面相叠加的栅格
     if (radioBtnKJ.Checked && polygon2 != null)
     {
         IExtractionOp op = new RasterExtractionOpClass();
         IGeoDataset dataset = op.Polygon((IGeoDataset)rsterLayer.Raster, polygon2, true);
         IRasterLayer layer = new RasterLayerClass();
         layer.CreateFromRaster((IRaster)dataset);
         rsterLayer = layer;
     }
     return rsterLayer;
 }
Ejemplo n.º 5
0
        public void RasterClip(IRasterLayer pRasterLayer, IPolygon clipGeo, string FileName)
        {
            try
            {
                //IRaster pRaster = pRasterLayer.Raster;

                //IRasterProps pProps = pRaster as IRasterProps;

                //object cellSizeProvider = pProps.MeanCellSize().X;

                //IGeoDataset pInputDataset = pRaster as IGeoDataset;

                //IExtractionOp2 pExtractionOp = new ESRI.ArcGIS.SpatialAnalyst.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);


                IRasterProps pProps           = pRasterLayer.Raster  as IRasterProps;
                object       cellSizeProvider = pProps.MeanCellSize().X;
                IGeoDataset  pInputDataset    = pRasterLayer.Raster 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);//裁切操作
                //  IGeoDataset pOutputDataset = pExtractionOp.Rectangle(pInputDataset, clipGeo.Envelope, true);

                IRaster clipRaster; //裁切后得到的IRasterzzz

                if (pOutputDataset is IRasterLayer)
                {
                    IRasterLayer rasterLayer = 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 = pProps.PixelType;
                }

                else
                {
                    return;
                }

                //保存裁切后得到的clipRaster

                //如果直接保存为img影像文件

                IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass();

                IWorkspace pWorkspace = pWKSF.OpenFromFile(System.IO.Path.GetDirectoryName(FileName), 0);

                ISaveAs pSaveAs = clipRaster as ISaveAs;

                //bool cansave = pSaveAs.CanSaveAs("TIFF");
                bool cansave = pSaveAs.CanSaveAs("BMP");
                bool zzz     = cansave;

                IRasterDataset pRD = pSaveAs.SaveAs(System.IO.Path.GetFileName(FileName), pWorkspace, "TIFF") as IRasterDataset;//以img格式保存

                //Note, SaveAs will return a RasterDataset, to prevent from ISaveAs holding the output,
                //.NET ReleaseCOMObject needs to be called to release the referene to the output raster dataset:
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(outRasterDS);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pRD);


                MessageBox.Show("\"" + FileName + "\"成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception exp)
            {
                MessageBox.Show("截取样本失败!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 根据范围提取数据
        /// </summary>
        /// <param name="pFileName">目标数据名称(不包含路径)</param>
        /// <param name="clipExtent">几何范围</param>
        /// <param name="pDesWS">目标数据工作空间</param>
        /// <param name="format">目标数据类型</param>
        /// <param name="pResRatser">源栅格数据</param>
        /// <param name="bIn">true:表明提取几何范围内部的数据;false:提取几何范围外部的数据</param>
        /// <param name="eError"></param>
        private void StractData(string pFileName, IGeometry clipExtent, IWorkspace pDesWS, string format, IRaster pResRatser, bool bIn, out Exception eError)
        {
            eError = null;
            IExtractionOp extraction = new RasterExtractionOpClass();

            #region 进行裁切
            IGeoDataset      pOrgGeoDt  = pResRatser as IGeoDataset;
            IGeoDataset      pDesGeoDt  = null;
            IPolygon         pPoly      = clipExtent as IPolygon;
            ICircularArc     pArc       = clipExtent as ICircularArc;
            IPointCollection pPointColl = clipExtent as IPointCollection;
            IEnvelope        pRec       = clipExtent as IEnvelope;
            if (pPoly != null)
            {
                //几何范围是多边形
                pDesGeoDt = extraction.Polygon(pOrgGeoDt, pPoly, checkBoxClip.Checked);
            }
            else if (pArc != null)
            {
                //几何范围是圆形

                pDesGeoDt = extraction.Circle(pOrgGeoDt, pArc, checkBoxClip.Checked);
            }
            else if (pRec != null)
            {
                //几何范围是矩形

                pDesGeoDt = extraction.Rectangle(pOrgGeoDt, pRec, checkBoxClip.Checked);
            }
            else if (pPointColl != null)
            {
                //几何范围是点集

                pDesGeoDt = extraction.Points(pOrgGeoDt, pPointColl, checkBoxClip.Checked);
            }
            #endregion

            #region 进行保存
            IRaster pDesRaster = pDesGeoDt as IRaster;
            ISaveAs pSaveAs    = pDesRaster as ISaveAs;
            if (format == "TIF")
            {
                if (!pSaveAs.CanSaveAs("TIFF"))
                {
                    eError = new Exception("该栅格数据不能保存为tiff格式!");
                    return;
                }
                pSaveAs.SaveAs(pFileName, pDesWS, "TIFF");
            }
            else if (format == "IMG")
            {
                if (!pSaveAs.CanSaveAs("IMAGINE Image"))
                {
                    eError = new Exception("该栅格数据不能保存为IMG格式!");
                    return;
                }
                pSaveAs.SaveAs(pFileName, pDesWS, "IMAGINE Image");
            }
            else
            {
                //geodatabase结构数据需要用另外的方式来保存数据:PDB、GDB
                //geodatabase根据已有的rasterDataset生成目标rasterDataset(clipExtent几何限制条件没用上)
                IRaster2 pOrgRaster2 = pResRatser as IRaster2;
                if (pOrgRaster2 == null)
                {
                    return;
                }
                IRasterDataset mOrgRDataset = pOrgRaster2.RasterDataset; //源栅格数据集
                if (mOrgRDataset.CanCopy())
                {
                    mOrgRDataset.Copy(m_DesRasterCollName, pDesWS);
                }
            }
            #endregion
        }