Ejemplo n.º 1
0
        ///<summary>
        /// 区域像元值统计ZonalStatisticsAsTable
        /// 统计某范围内像元值
        /// 返回datatable
        /// </summary>
        /// <param name="inFile">输入shp文件名</param>
        /// <param name="inValueFile">要统计的栅格文件名</param>
        /// <param name="zoneField">区域字段,区分要统计的区域</param>
        /// <param name="statistisType">统计类型(保留字段)</param>
        public DataTable ZonalStatisticsAsTable(string inFile, string inValueFile, string zoneField, string statistisType = null)
        {
            IFeatureClass  pFeatureClass = null;
            IRasterDataset pRasterDst    = null;
            IZonalOp       pZonalOp      = null;
            ITable         pResTable     = null;

            try
            {
                pFeatureClass = EngineAPI.OpenFeatureClass(inFile);
                pRasterDst    = EngineAPI.OpenRasterFile(inValueFile);
                pZonalOp      = new RasterZonalOpClass();
                pResTable     = pZonalOp.ZonalStatisticsAsTable((IGeoDataset)pFeatureClass, (IGeoDataset)pRasterDst, true);
                ITable          pZoneTable = (ITable)pFeatureClass;
                TableConversion conver     = new TableConversion();
                DataTable       result     = conver.AETableToDataTable(pResTable);
                DataTable       zone       = conver.AETableToDataTable(pZoneTable);
                result.Columns.Remove("Rowid");
                result.Columns.Remove("VALUE");
                //result.Columns.Remove("AREA");
                //result.Columns.RemoveAt(0);
                //result.Columns.RemoveAt(0);
                //result.Columns.RemoveAt(1);
                result.Columns.Add(zoneField);
                for (int i = 0; i < zone.Rows.Count; i++)
                {
                    if (i < result.Rows.Count)
                    {
                        result.Rows[i][zoneField] = zone.Rows[i][zoneField];
                    }
                }
                result.Columns[zoneField].SetOrdinal(0);
                return(result);
            }
            catch (Exception ex)
            {
                Log.WriteLog(typeof(GPExecutor), ex);
                return(null);
            }
            finally
            {
                if (pFeatureClass != null)
                {
                    Marshal.ReleaseComObject(pFeatureClass);
                }
                if (pRasterDst != null)
                {
                    Marshal.ReleaseComObject(pRasterDst);
                }
                if (pZonalOp != null)
                {
                    Marshal.ReleaseComObject(pZonalOp);
                }
                if (pResTable != null)
                {
                    Marshal.ReleaseComObject(pResTable);
                }
            }
        }
        /// <summary>
        /// 获取高程信息ITable
        /// </summary>
        /// <param name="strFolderPath">DEM文件夹</param>
        /// <param name="strFileName">DEM文件名称</param>
        /// <param name="myIFeatureClass">过滤面状数据集</param>
        /// <returns>高程信息ITable</returns>
        public ITable CalculateValueFromFile(string strFolderPath, string strFileName, IFeatureClass myIFeatureClass)
        {
            IWorkspaceFactory myIWorkspaceFactory       = new RasterWorkspaceFactory();
            IWorkspace        myIWorkspace              = myIWorkspaceFactory.OpenFromFile(strFolderPath, 0);
            IRasterWorkspace  myIRasterWorkspace        = myIWorkspace as IRasterWorkspace;
            IRasterDataset    myIRasterDataset          = myIRasterWorkspace.OpenRasterDataset(strFileName);//创建影像数据集
            IGeoDataset       rasterIGeoDataset         = myIRasterDataset as IGeoDataset;
            IGeoDataset       myIFeatureClassGeoDataset = myIFeatureClass as IGeoDataset;
            IZonalOp          myIZonalOp = new RasterZonalOpClass();
            ITable            myITable   = myIZonalOp.ZonalStatisticsAsTable(myIFeatureClassGeoDataset, rasterIGeoDataset, true);

            return(myITable);
        }