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

相关文章

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

Java中有什么工具可以进行代码反编译详解

《Java中有什么工具可以进行代码反编译详解》:本文主要介绍Java中有什么工具可以进行代码反编译的相关资,料,包括JD-GUI、CFR、Procyon、Fernflower、Javap、Byte... 目录1.JD-GUI2.CFR3.Procyon Decompiler4.Fernflower5.Jav

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

Python进行PDF文件拆分的示例详解

《Python进行PDF文件拆分的示例详解》在日常生活中,我们常常会遇到大型的PDF文件,难以发送,将PDF拆分成多个小文件是一个实用的解决方案,下面我们就来看看如何使用Python实现PDF文件拆分... 目录使用工具将PDF按页数拆分将PDF的每一页拆分为单独的文件将PDF按指定页数拆分根据页码范围拆分

HTML5中下拉框<select>标签的属性和样式详解

《HTML5中下拉框<select>标签的属性和样式详解》在HTML5中,下拉框(select标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中选择值的方式,本文将深入探讨select标签的... 在html5中,下拉框(<select>标签)作为表单的重要组成部分,为用户提供了一个从预定义选项中

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景