ArcEngine:如何进行缩放图层、属性信息显示、状态栏显示?

2023-11-21 00:20

本文主要是介绍ArcEngine:如何进行缩放图层、属性信息显示、状态栏显示?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

01 前言

如果以后不是工作相关,我或许以后再难了解ArcEngine相关的GIS桌面软件二次开发的内容?

02 要求

  1. 创建窗体应用程序;(10分)
  2. 修改窗口标题为本人的“学号 姓名”;(5分)
  3. 添加主地图控件、图层树控件和数据表显示控件,并合理布局;(10分)
  4. 添加菜单和状态栏控件;(5分)
  5. 增加“打开地图文档”菜单功能,弹出对话框选择地图文档(*.mxd),在主地图控件中显示该地图;(10分)
  6. 鼠标在主地图控件中移动时,将鼠标对应的地图坐标显示在状态栏上;(10分)
  7. 地图显示范围发生改变时,将地图比例显示在状态栏上;(10分)
  8. 点击图层控件的图层项,将地图视图显示范围缩放到该图层范围;(10分)
  9. 在数据表显示控件中显示所选图层的属性;(20分)
  10. 撰写期末考查报告。(10分)

笔记本;WIndow11;VS2012;ArcEngine10.2

如果安装高版本的VS可能无法完美复现本博客,因为高版本VS如何创建基础版的应用,而是创建的完全0的空应用。

03 实现步骤和代码说明

3.1 创建窗体应用程序(创建项目)

在这里插入图片描述

初始界面如下:

在这里插入图片描述

在这里插入图片描述

如果不存在上述面板如下打开:

在这里插入图片描述

如此可以看到如下初始界面:

在这里插入图片描述

在这里插入图片描述

3.2 修改窗口标题为本人的“学号 姓名”

在这里插入图片描述

3.3 添加主地图控件、图层树控件和数据表显示控件,并合理布局

主地图控件和图层树控件已经在创建项目之后已经存在了,只是数据表(用于后续选中图层的属性信息显示)并没有,而且要与当前控件布局协调。

数据表使用DataGridView工具。

3.3.1 添加数据表DataGridView

在这里插入图片描述

3.3.2 调整布局

但是目前布局不合理,我们需要调整DataGridViewaxMapControl1(主地图控件)的空间位置。

修改DataGridViewDock属性为Bottom,这里就是用其他工具进行调整了,简单一些好了。

在这里插入图片描述

3.4 添加菜单和状态栏控件;

由于创建项目时已经帮助我们添加这些控件,因此我们无需重复操作,这里直接运行看看界面吧。

在这里插入图片描述

3.5 增加“打开地图文档”菜单功能,弹出对话框选择地图文档(*.mxd),在主地图控件中显示该地图;

这个功能实际上创建项目时也已经实现了,因此演示如下:

在这里插入图片描述

3.6 鼠标在主地图控件中移动时,将鼠标对应的地图坐标显示在状态栏上;

实际上也已经实现,这里不再说明

3.7 地图显示范围发生改变时,将地图比例显示在状态栏上

状态栏加一个用于显示比例尺:

在这里插入图片描述

监听范围更新事件:

在这里插入图片描述

事件代码如下:

private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e){//当前比例尺string strScale = "   1:" + ((long)axMapControl1.MapScale).ToString() + "  ";// 在状态栏中进行格式化显示statusBarScale.Text = string.Format("{0}", strScale);}

实现效果如下:

在这里插入图片描述

3.8 点击图层控件的图层项,将地图视图显示范围缩放到该图层范围

显然,需要监听TOC图层树的鼠标点击事件:

在这里插入图片描述

事件代码如下:

// 当鼠标点击图层时, 实现图层缩放和属性获取显示两个功能private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e){// 做鼠标点击位置检测-用于获取当前点击位置是否为图层以及获取点击的图层对象esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone;  // 赋初值NoneIBasicMap basicMap = null;ILayer pCurLayer = null;object unk = null;object data = null;axTOCControl1.HitTest(e.x, e.y, ref itemType, ref basicMap, ref pCurLayer, ref unk, ref data);  // 检测点击位置, 返回给ref等// 如果点击的不是图层对象, 那么不进行任何操作if (itemType != esriTOCControlItem.esriTOCControlItemLayer){return;}// 如果pCurLayer为空则不进行操作if (pCurLayer == null){return;}// 获取当前点击图层的感兴趣区域范围IEnvelope env = pCurLayer.AreaOfInterest;  // 获取当前点击图层的范围// 当前选中图层的框为空, 那么不进行任何操作if ((env.IsEmpty) || (env == null)){return;}// 将主视图的范围显示为当前右键选中图层的范围, 实现缩放到当前图层的功能axMapControl1.Extent = env;// 显示当前图层属性(自定义一个query_feature_class函数进行操作, 传入当前点击图层变量)query_feature_class(pCurLayer);}

上述代码最后部分加入了一个自定义函数:

// 显示当前图层属性(自定义一个query_feature_class函数进行操作, 传入当前点击图层变量)
query_feature_class(pCurLayer);

注意:这是后续功能(显示选中图层的属性信息)的实现。

3.9 在数据表显示控件中显示所选图层的属性

我们主要实现前面的query_feature_class函数,代码如下:

// 实现选择所选图层属性功能
privatevoid query_feature_class(ILayer layer)
{// 获取空间过滤对象ISpatialFilter pSpatialFilter = new SpatialFilter();pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;pSpatialFilter.GeometryField = "Shape";// 将当前传入该函数的 layer 进行过滤IFeatureClass pFeatureClass = (layer as IFeatureLayer).FeatureClass;IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, true);if (pFeatureCursor == null){dataGridView.DataSource = null;}else{// 将对象传入dataGridView.DataSource = cursor2table(pFeatureCursor);}
}// 图层属性信息与DataGridView数据格式的转换
private DataTable cursor2table(IFeatureCursor pFeatureCursor)
{// 为DataGridView创建表头并赋值DataTable tbl = new DataTable();for (int ix = 0; ix < pFeatureCursor.Fields.FieldCount; ix++){tbl.Columns.Add(pFeatureCursor.Fields.get_Field(ix).Name);}// 将所有要素一行一行的迭代传入DataGridView中IFeature pFeature = pFeatureCursor.NextFeature();while (pFeature != null){DataRow row = tbl.NewRow();for (int ix = 0; ix < pFeatureCursor.Fields.FieldCount; ix++){row[ix] = pFeature.get_Value(ix).ToString();}tbl.Rows.Add(row);pFeature = pFeatureCursor.NextFeature();}return tbl;  // 返回已经存有图层属性信息的DataGridView对象
}

04 完整代码和应用界面展示

如此所有功能实现,完整代码:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;using ESRI.ArcGIS.Geometry;namespace DropCurtain
{public sealed partial class MainForm : Form{#region class private membersprivate IMapControl3 m_mapControl = null;private string m_mapDocumentName = string.Empty;#endregion#region class constructorpublic MainForm(){InitializeComponent();}#endregionprivate void MainForm_Load(object sender, EventArgs e){//get the MapControlm_mapControl = (IMapControl3)axMapControl1.Object;//disable the Save menu (since there is no document yet)menuSaveDoc.Enabled = false;}#region Main Menu event handlersprivate void menuNewDoc_Click(object sender, EventArgs e){//execute New Document commandICommand command = new CreateNewDocument();command.OnCreate(m_mapControl.Object);command.OnClick();}private void menuOpenDoc_Click(object sender, EventArgs e){//execute Open Document commandICommand command = new ControlsOpenDocCommandClass();command.OnCreate(m_mapControl.Object);command.OnClick();}private void menuSaveDoc_Click(object sender, EventArgs e){//execute Save Document commandif (m_mapControl.CheckMxFile(m_mapDocumentName)){//create a new instance of a MapDocumentIMapDocument mapDoc = new MapDocumentClass();mapDoc.Open(m_mapDocumentName, string.Empty);//Make sure that the MapDocument is not readonlyif (mapDoc.get_IsReadOnly(m_mapDocumentName)){MessageBox.Show("Map document is read only!");mapDoc.Close();return;}//Replace its contents with the current mapmapDoc.ReplaceContents((IMxdContents)m_mapControl.Map);//save the MapDocument in order to persist itmapDoc.Save(mapDoc.UsesRelativePaths, false);//close the MapDocumentmapDoc.Close();}}private void menuSaveAs_Click(object sender, EventArgs e){//execute SaveAs Document commandICommand command = new ControlsSaveAsDocCommandClass();command.OnCreate(m_mapControl.Object);command.OnClick();}private void menuExitApp_Click(object sender, EventArgs e){//exit the applicationApplication.Exit();}#endregion//listen to MapReplaced evant in order to update the statusbar and the Save menuprivate void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e){//get the current document name from the MapControlm_mapDocumentName = m_mapControl.DocumentFilename;//if there is no MapDocument, diable the Save menu and clear the statusbarif (m_mapDocumentName == string.Empty){menuSaveDoc.Enabled = false;statusBarXY.Text = string.Empty;}else{//enable the Save manu and write the doc name to the statusbarmenuSaveDoc.Enabled = true;statusBarXY.Text = System.IO.Path.GetFileName(m_mapDocumentName);}}private void axMapControl1_OnMouseMove(object sender, IMapControlEvents2_OnMouseMoveEvent e){statusBarXY.Text = string.Format("{0}, {1}  {2}", e.mapX.ToString("#######.##"), e.mapY.ToString("#######.##"), axMapControl1.MapUnits.ToString().Substring(4));}private void axMapControl1_OnExtentUpdated(object sender, IMapControlEvents2_OnExtentUpdatedEvent e){//当前比例尺string strScale = "   1:" + ((long)axMapControl1.MapScale).ToString() + "  ";// 在状态栏中进行格式化显示statusBarScale.Text = string.Format("{0}", strScale);}private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e){// 做鼠标点击位置检测-用于获取当前点击位置是否为图层以及获取点击的图层对象esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone;  // 赋初值NoneIBasicMap basicMap = null;ILayer pCurLayer = null;object unk = null;object data = null;axTOCControl1.HitTest(e.x, e.y, ref itemType, ref basicMap, ref pCurLayer, ref unk, ref data);  // 检测点击位置, 返回给ref等// 如果点击的不是图层对象, 那么不进行任何操作if (itemType != esriTOCControlItem.esriTOCControlItemLayer){return;}// 如果pCurLayer为空则不进行操作if (pCurLayer == null){return;}// 获取当前点击图层的感兴趣区域范围IEnvelope env = pCurLayer.AreaOfInterest;  // 获取当前点击图层的范围// 当前选中图层的框为空, 那么不进行任何操作if ((env.IsEmpty) || (env == null)){return;}// 将主视图的范围显示为当前右键选中图层的范围, 实现缩放到当前图层的功能axMapControl1.Extent = env;// 显示当前图层属性(自定义一个query_feature_class函数进行操作, 传入当前点击图层变量)query_feature_class(pCurLayer);}// 实现选择所选图层属性功能private void query_feature_class(ILayer layer){// 获取空间过滤对象ISpatialFilter pSpatialFilter = new SpatialFilter();pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;pSpatialFilter.GeometryField = "Shape";// 将当前传入该函数的 layer 进行过滤IFeatureClass pFeatureClass = (layer as IFeatureLayer).FeatureClass;IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, true);if (pFeatureCursor == null){dataGridView.DataSource = null;}else{// 将对象传入dataGridView.DataSource = cursor2table(pFeatureCursor);}}// 图层属性信息与DataGridView数据格式的转换private DataTable cursor2table(IFeatureCursor pFeatureCursor){// 为DataGridView创建表头并赋值DataTable tbl = new DataTable();for (int ix = 0; ix < pFeatureCursor.Fields.FieldCount; ix++){tbl.Columns.Add(pFeatureCursor.Fields.get_Field(ix).Name);}// 将所有要素一行一行的迭代传入DataGridView中IFeature pFeature = pFeatureCursor.NextFeature();while (pFeature != null){DataRow row = tbl.NewRow();for (int ix = 0; ix < pFeatureCursor.Fields.FieldCount; ix++){row[ix] = pFeature.get_Value(ix).ToString();}tbl.Rows.Add(row);pFeature = pFeatureCursor.NextFeature();}return tbl;  // 返回已经存有图层属性信息的DataGridView对象}}
}

实现界面:

在这里插入图片描述

打开地图文档功能:

在这里插入图片描述

图层缩放和选中图层的属性信息显示:

在这里插入图片描述

在这里插入图片描述

这篇关于ArcEngine:如何进行缩放图层、属性信息显示、状态栏显示?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/398495

相关文章

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基

springboot实现配置文件关键信息加解密

《springboot实现配置文件关键信息加解密》在项目配置文件中常常会配置如数据库连接信息,redis连接信息等,连接密码明文配置在配置文件中会很不安全,所以本文就来聊聊如何使用springboot... 目录前言方案实践1、第一种方案2、第二种方案前言在项目配置文件中常常会配置如数据库连接信息、Red

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服