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

相关文章

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

Linux使用scp进行远程目录文件复制的详细步骤和示例

《Linux使用scp进行远程目录文件复制的详细步骤和示例》在Linux系统中,scp(安全复制协议)是一个使用SSH(安全外壳协议)进行文件和目录安全传输的命令,它允许在远程主机之间复制文件和目录,... 目录1. 什么是scp?2. 语法3. 示例示例 1: 复制本地目录到远程主机示例 2: 复制远程主

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.