基于C#的ArcEngine二次开发教程(17):获取栅格属性的接口及代码实现

目录

1 问题描述

2 问题解析

3 要点分析

3.1 ArcGIS的接口与方法

3.3 DataGridView的学习总结

3.3.1 属性设置

3.3.2 添加新行

3.3.3 绘制行号

3.3.4 点击首行或首列选中

3.3.5 选中单一的格子

4 源码实现

5 结果展示

6 参考优质博文:



1 问题描述

从栅格属性中提取分辨率信息、焦点坐标信息、参考坐标系统信息、中央子午线等

2 问题解析

3 要点分析

3.1 ArcGIS的接口与方法

主要用到的接口:

IRasterProps
IRasterBandCollection
IRasterDataset
IRasterPyramid3
IRasterBand
栅格属性列表:
列数和行数(Columns and Rows):通过IRasterProps的Width和Height属性获取
波段数量(Number of Bands):通过IRasterBandCollection接口获取(可以由IRaster对象跳转接口到此接口)
像元大小(CellSize(X,Y)):通过IRasterProps的MeanCellSize
格式(Format):通过IRasterDataset的Format属性获取
源类型(Source Type):可以通过GP工具SetRasterProperties来设置。
像素类型(Pixel Type):通过IRasterProps接口的PixelType 属性获取
像素位深(Pixel Depth):根据像素类型来判断像素位深
无数据值(NoData Value):通过IRasterProps 的NoDataValue属性获取
颜色表/色带(Colormap):通过IRaster2接口的Colormap属性获取色带
金字塔(Pyramids):通过IRasterPyramid3接口来创建、获取、删除金字塔(PS:用IRasterDataset接口跳转到IRasterPyramid3接口)
压缩(Compression):通过IRasterDataset的CompressionType属性获取压缩类型
范围(Extent):将IRasterDataset接口对象转换成IGeoDataset对象来范围信息
空间参考(Spatial Reference):1)将IRasterDataset接口对象转换成IGeoDataset对象来获取空间参考信息 2)通过IRasterProps 的属性SpatialReference获取 3)其它方法
统计(Statistics):通过IRasterBand接口的Statistics属性获取波段的统计信息
波段集合:通过IRasterBandCollection 接口来添加、删除、获取波段。

3.3 DataGridView的学习总结

3.3.1 属性设置

  • DataGridView的最后一行不显示:dataGridView1.AllowUserToAddRows = false;
  • 自动填充列宽:AutoSizeColumns: Fill
  • 不进行列排序

3.3.2 添加新行

方法一:使用add方法

            //采用Add()方法添加新行
            int index1 = dataGridView1.Rows.Add();
            dataGridView1.Rows[index1].Cells[0].Value = "行数";
            dataGridView1.Rows[index1].Cells[1].Value = myRasterProp.Height.ToString();

方法二:添加add(row)

        private void addPropToDgv(string propDiscription, string propValue)
        {
            //采用DataGridViewRow添加新行
            DataGridViewRow row = new DataGridViewRow();
            DataGridViewTextBoxCell textboxcell0 = new DataGridViewTextBoxCell();
            textboxcell0.Value = propDiscription;
            row.Cells.Add(textboxcell0);
            DataGridViewTextBoxCell textboxcell1 = new DataGridViewTextBoxCell();
            textboxcell1.Value = propValue;
            row.Cells.Add(textboxcell1);
            //DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
            //row.Cells.Add(comboxcell);
            dataGridView1.Rows.Add(row);
        }

3.3.3 绘制行号

方式一

//显示行号
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var grid = sender as DataGridView;
            var rowIdx = (e.RowIndex + 1).ToString();
            var centerFormat = new StringFormat() 
            { 
                // right alignment might actually make more sense for numbers
                Alignment = StringAlignment.Center, 
                LineAlignment = StringAlignment.Center
            };
            var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
        }

方式二

//显示行号
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.dataGridview1.RowHeaderDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).Tostring(System.Golbalization.CultureInfo.CurrentCulture), this.dataGridview1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.X + 4);
        }

3.3.4 点击首行或首列选中

        //点击列头选中整列
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
            dataGridView1.Columns[e.ColumnIndex].Selected = true;
        }
        //点击行头选中整行
        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.Rows[e.RowIndex].Selected = true;
        }

3.3.5 选中单一的格子

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1)
            {
                this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dataGridView1.Rows[e.ColumnIndex].Selected = true;
            }           
        }

4 源码实现

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
//https://blog.csdn.net/yh0503/article/details/52644017
//https://blog.csdn.net/mengxiangzhengfaya/article/details/52985746
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            InitializeComponent();
        }

        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            textBox1.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        }

        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }
        private void addPropToDgv(string propDiscription, string propValue)
        {
            //采用DataGridViewRow添加新行
            DataGridViewRow row = new DataGridViewRow();
            DataGridViewTextBoxCell textboxcell0 = new DataGridViewTextBoxCell();
            textboxcell0.Value = propDiscription;
            row.Cells.Add(textboxcell0);
            DataGridViewTextBoxCell textboxcell1 = new DataGridViewTextBoxCell();
            textboxcell1.Value = propValue;
            row.Cells.Add(textboxcell1);
            //DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();
            //row.Cells.Add(comboxcell);
            dataGridView1.Rows.Add(row);
        }
        private void btn_GetRasterInfo_Click(object sender, EventArgs e)
        {
            
            IWorkspaceFactory myWorkFact = new RasterWorkspaceFactoryClass();
            string rasterData = textBox1.Text;
            IRasterWorkspace myRasterWorkspce = myWorkFact.OpenFromFile(System.IO.Path.GetDirectoryName(rasterData), 0) as IRasterWorkspace;
            IRasterDataset myRasterDataset = myRasterWorkspce.OpenRasterDataset(System.IO.Path.GetFileName(rasterData)) as IRasterDataset;
            IRasterLayer myRasterLayer = new RasterLayerClass();
            myRasterLayer.CreateFromDataset(myRasterDataset);
            IRasterProps myRasterProp = myRasterLayer.Raster as IRasterProps;

            //采用Add()方法添加新行
            int index1 = dataGridView1.Rows.Add();
            dataGridView1.Rows[index1].Cells[0].Value = "行数";
            dataGridView1.Rows[index1].Cells[1].Value = myRasterProp.Height.ToString();

            addPropToDgv("列数", myRasterProp.Width.ToString());
            addPropToDgv("像素类型", myRasterProp.PixelType.ToString());
            addPropToDgv("波段数",(myRasterLayer.Raster as IRasterBandCollection).Count.ToString());
            addPropToDgv("压缩类型", myRasterDataset.CompressionType.ToString());
            //四个角点的最大最小坐标
            addPropToDgv("最大X坐标", myRasterProp.Extent.XMax.ToString());
            addPropToDgv("最小X坐标", myRasterProp.Extent.XMin.ToString());
            addPropToDgv("最大Y坐标", myRasterProp.Extent.YMax.ToString());
            addPropToDgv("最小Y坐标", myRasterProp.Extent.YMin.ToString());
            //四个角点的坐标
            addPropToDgv("左下X坐标", myRasterProp.Extent.LowerLeft.X.ToString());
            addPropToDgv("左下Y坐标", myRasterProp.Extent.LowerLeft.Y.ToString());
            addPropToDgv("左上X坐标", myRasterProp.Extent.UpperLeft.X.ToString());
            addPropToDgv("左上Y坐标", myRasterProp.Extent.UpperLeft.Y.ToString());
            addPropToDgv("右下X坐标", myRasterProp.Extent.LowerRight.X.ToString());
            addPropToDgv("右下Y坐标", myRasterProp.Extent.LowerRight.Y.ToString());
            addPropToDgv("右上X坐标", myRasterProp.Extent.UpperRight.X.ToString());
            addPropToDgv("右上Y坐标", myRasterProp.Extent.UpperRight.Y.ToString());
            //X和Y坐标的格网分辨率
            addPropToDgv("X尺寸", myRasterProp.MeanCellSize().X.ToString());
            addPropToDgv("Y尺寸", myRasterProp.MeanCellSize().Y.ToString());
            
            //参考坐标信息
            ISpatialReference pSpatialReference = myRasterProp.SpatialReference;
            addPropToDgv("空间参考", pSpatialReference.Name.ToString());
            
            //投影坐标系
            IProjectedCoordinateSystem pcs = pSpatialReference as IProjectedCoordinateSystem;
            addPropToDgv("地理坐标系", pcs.GeographicCoordinateSystem.Name);
            addPropToDgv("基准面", pcs.GeographicCoordinateSystem.Datum.Name);
            addPropToDgv("参考椭球", pcs.GeographicCoordinateSystem.Datum.Spheroid.Name);
            addPropToDgv("投影坐标系", pcs.Projection.Name);
            addPropToDgv("中央子午线", pcs.get_CentralMeridian(true).ToString());
            addPropToDgv("线性单位", pcs.CoordinateUnit.Name.ToString());
            addPropToDgv("尺度因子", pcs.ScaleFactor.ToString());
            addPropToDgv("假东距", pcs.FalseEasting.ToString());
            addPropToDgv("假北距", pcs.FalseNorthing.ToString());
                    
        }
           
        //显示行号
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var grid = sender as DataGridView;
            var rowIdx = (e.RowIndex + 1).ToString();
            var centerFormat = new StringFormat() 
            { 
                // right alignment might actually make more sense for numbers
                Alignment = StringAlignment.Center, 
                LineAlignment = StringAlignment.Center
            };
            var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
            e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
        }
        //点击列头选中整列
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
            dataGridView1.Columns[e.ColumnIndex].Selected = true;
        }
        //点击行头选中整行
        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.Rows[e.RowIndex].Selected = true;
        }
        //选中格子
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1)
            {
                this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dataGridView1.Rows[e.ColumnIndex].Selected = true;
            }           
        }
    }
}

 

5 结果展示

6 参考优质博文:

ArcEngine 栅格数据 总结

C# ArcEngine获取坐标系、投影类型、计量单位、带号、几度分带、精度


一起进步,欢迎关注个人微信公众账号【学而立行】

 

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛引路

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值