[AE] ArcGIS Engine与栅格

【Tools文件】AE栅格文件的操作

  • 删除栅格文件
  • 打开栅格文件
  • 保存栅格文件
  • 栅格计算器
  • 获取栅格数据中的二维矩阵,得到System.Array类型(二维数组)
  • 根据System.Array二维矩阵(二维数组)修改栅格文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.SpatialAnalyst;

namespace PM25Application
{
    class Tools
    {

        #region 栅格文件基本操作
        /// <summary>
        ///     删除栅格数据fp
        /// </summary>
        /// <param name="fp">路径</param>
        public static bool DeleteRasterFile(string fp)
        {
            try
            {
                string fn = Path.GetFileNameWithoutExtension(fp);
                var files = Directory.GetFiles(Path.GetDirectoryName(fp), fn + ".*");
                if (files.Length < 0)
                    return true;
                for (int i = 0; i < files.Length; i++)
                {
                    File.Delete(files[i]); //删除文件
                }
            }
            catch (Exception e) {
                return false; //异常
            }
            return true;
        }


        /// <summary>
        ///     打开栅格_tif格式
        /// </summary>
        /// <param name="fp">栅格路径</param>
        /// <returns>IGeoDataset</returns>
        public static IGeoDataset OpenRaster(string fp)
        {
            IWorkspaceFactory wf = new RasterWorkspaceFactoryClass();
            IWorkspace ws = wf.OpenFromFile(Path.GetDirectoryName(fp), 0);
            IRasterWorkspace2 rw = ws as IRasterWorkspace2;
            IRasterDataset rd = rw.OpenRasterDataset(Path.GetFileName(fp));
            IGeoDataset pGeo = rd as IGeoDataset;
            MainForm.ShowRaster(pGeo, Path.GetFileName(fp));
            return pGeo;
        }

        /// <summary>
        ///     保存栅格_tif格式
        /// </summary>
        /// <param name="pGeo"></param>
        /// <param name="out_fp"></param>
        /// <returns></returns>
        public static bool SaveRaster(IGeoDataset pGeo, string out_fp)
        {
            // out_fp是否存在,存在的话删除其文件
            if (File.Exists(out_fp) == true)
            {
                bool ok = DeleteRasterFile(out_fp);
                if (ok == false) { //删除文件失败
                    return false;
                }
            }
            // 将矩阵GeoDataset转成栅格集
            IRasterBandCollection irbc = (IRasterBandCollection)pGeo;
            IRasterDataset pRD = irbc.Item(0).RasterDataset;
            // 保存
            IWorkspaceFactory workspaceFactoryOut = new RasterWorkspaceFactoryClass();
            IWorkspace workspace1 = workspaceFactoryOut.OpenFromFile(Path.GetDirectoryName(out_fp), 0);
            irbc.SaveAs(Path.GetFileName(out_fp), workspace1, "TIFF");

            MainForm.ShowRaster(pGeo, Path.GetFileName(out_fp));
            return true;
        }

        public static bool SaveRaster(IRasterBandCollection pRBC, string out_fp)
        {
            // 保存
            IWorkspaceFactory workspaceFactoryOut = new RasterWorkspaceFactoryClass();
            IWorkspace workspace1 = workspaceFactoryOut.OpenFromFile(Path.GetDirectoryName(out_fp), 0);
            pRBC.SaveAs(Path.GetFileName(out_fp), workspace1, "TIFF");
            return true;
        }

        /// <summary>
        ///     两个IGeoDataset相减,并取绝对值
        /// </summary>
        /// <param name="pGeo1"></param>
        /// <param name="pGeo2"></param>
        /// <returns></returns>
        public static IGeoDataset SubAndAbs(IGeoDataset pGeo1, IGeoDataset pGeo2)
        {
            IMapAlgebraOp pMapAlgebraOp = new RasterMapAlgebraOpClass();
            pMapAlgebraOp.BindRaster(pGeo1, "pGeo1");
            pMapAlgebraOp.BindRaster(pGeo2, "pGeo2");
            string formula = string.Concat("Abs( [pGeo1] - [pGeo2] )");
            formula = string.Concat("Con( [pGeo1] == 0 | [pGeo2] == 0, 0, ", formula, " )");
            IGeoDataset result = pMapAlgebraOp.Execute(formula);
            return result;
        }

        /// <summary>
        ///     栅格文件转数组
        /// </summary>
        /// <param name="raster"></param>
        /// <param name="novalue"></param>
        /// <returns></returns>
        public static System.Array RasterToArray(IGeoDataset pGD, ref object novalue)
        {
            IRaster raster = pGD as IRaster;
            IRasterProps props = (IRasterProps)raster;
            novalue = props.NoDataValue;
            IPnt pBlockSize = new PntClass();
            pBlockSize.SetCoords((double)props.Width, (double)props.Height);
            IRaster2 raster2 = (IRaster2)raster;
            IPixelBlock pixelBlock = raster2.CreateCursorEx(pBlockSize).PixelBlock;
            pBlockSize.SetCoords(0.0, 0.0);
            raster.Read(pBlockSize, pixelBlock);
            IPixelBlock3 block3 = (IPixelBlock3)pixelBlock;
            return (System.Array)block3.get_PixelData(0);
        }

        public static IGeoDataset AlterRasterArray(IGeoDataset pGeo, Array arr)
        {
            IRaster raster = pGeo as IRaster;
            IRasterProps props = (IRasterProps)raster;
            IPnt pBlockSize = new PntClass();
            pBlockSize.SetCoords((double)props.Width, (double)props.Height);
            IRaster2 raster2 = (IRaster2)raster;
            IPixelBlock pixelBlock = raster2.CreateCursorEx(pBlockSize).PixelBlock;
            pBlockSize.SetCoords(0.0, 0.0);
            raster.Read(pBlockSize, pixelBlock);
            pixelBlock.set_SafeArray(0, arr);

            //编辑raster,将更新的值写入raster中  
            IRasterEdit rasterEdit = raster as IRasterEdit;
            //左上点坐标  
            IPnt tlp = new Pnt();
            tlp.SetCoords(0, 0);
            rasterEdit.Write(tlp, pixelBlock);
            rasterEdit.Refresh();
            return pGeo;
        }
        #endregion
    }
}

上一篇:基于C#的AE二次开发之影像数据的裁切(掩膜)


下一篇:如何在java中进行2 x 3阶光栅扫描?