欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ArcEngine二次开发-栅格波段统计

程序员文章站 2022-06-12 16:47:05
...

基于接口IRasterStatistics,可统计栅格波段的最值、均值、中位数、标准差、忽略值等。
接口IRasterStatistics文档:http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/IRasterStatistics_Interface/001q0000032q000000/

        /// <summary>
        /// 获取栅格统计结果
        /// </summary>
        /// <param name="inFile">输入栅格文件</param>
        /// <param name="bandIndex">波段索引</param>
        /// <returns></returns>
        public static IRasterStatistics GetRasterStatistics(string inFile, int bandIndex = 0)
        {
            var rasterBand = GetRasterBandAt(inFile,bandIndex);
            bool hasStatistics;
            rasterBand.HasStatistics(out hasStatistics);
            if (!hasStatistics)
            {
                //计算栅格统计值
                rasterBand.ComputeStatsAndHist();
            }

            IRasterStatistics rasterStatistics = rasterBand.Statistics;
            return rasterStatistics;
        }

        /// <summary>
        /// 基于给定的波段索引,获取栅格波段
        /// </summary>
        /// <param name="inFile">输入栅格文件</param>
        /// <param name="bandIndex">波段索引</param>
        /// <returns></returns>
        public static IRasterBand GetRasterBandAt(string inFile, int bandIndex = 0)
        {
            IRaster raster = GetRaster(inFile);
            IRasterBandCollection rasterBandCollection = raster as IRasterBandCollection;
            return rasterBandCollection == null ? null : rasterBandCollection.Item(bandIndex);
        }

        /// <summary>
        /// 获取栅格
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static IRaster GetRaster(string filePath)
        {
            IRasterLayer rasterLayer = GetRasterLayer(filePath);
            return rasterLayer.Raster;
        }

        /// <summary>
        /// 获取栅格图层
        /// </summary>
        /// <param name="filePath">图层文件路径</param>
        /// <returns></returns>
        public static IRasterLayer GetRasterLayer(string filePath)
        {
            IRasterDataset rasDataset = GetRasterDataset(filePath);
            IRasterLayer rasterLayer = new RasterLayerClass();
            rasterLayer.CreateFromDataset(rasDataset);
            return rasterLayer;
        }

        /// <summary>
        /// 获取栅格数据集
        /// </summary>
        /// <param name="filePath">栅格文件路径</param>
        /// <returns></returns>
        public static IRasterDataset GetRasterDataset(string filePath)
        {
            string fileName = Path.GetFileName(filePath);
            IRasterWorkspace pRasterWorkspace = (IRasterWorkspace)GetRasterWorkspace(filePath);
            IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(fileName);
            return pRasterDataset;
        }

        /// <summary>
        /// 获取栅格工作空间
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static IWorkspace GetRasterWorkspace(string filePath)
        {
            string directory = Path.GetDirectoryName(filePath);
            IWorkspaceFactory workFactory = new RasterWorkspaceFactoryClass();
            return workFactory.OpenFromFile(directory, 0);
        }