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

相关文章

springboot3.x使用@NacosValue无法获取配置信息的解决过程

《springboot3.x使用@NacosValue无法获取配置信息的解决过程》在SpringBoot3.x中升级Nacos依赖后,使用@NacosValue无法动态获取配置,通过引入SpringC... 目录一、python问题描述二、解决方案总结一、问题描述springboot从2android.x

HTML5的input标签的`type`属性值详解和代码示例

《HTML5的input标签的`type`属性值详解和代码示例》HTML5的`input`标签提供了多种`type`属性值,用于创建不同类型的输入控件,满足用户输入的多样化需求,从文本输入、密码输入、... 目录一、引言二、文本类输入类型2.1 text2.2 password2.3 textarea(严格

input的accept属性让文件上传安全高效

《input的accept属性让文件上传安全高效》文章介绍了HTML的input文件上传`accept`属性在文件上传校验中的重要性和优势,通过使用`accept`属性,可以减少前端JavaScrip... 目录前言那个悄悄毁掉你上传体验的“常见写法”改变一切的 html 小特性:accept真正的魔法:让

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp

idea粘贴空格时显示NBSP的问题及解决方案

《idea粘贴空格时显示NBSP的问题及解决方案》在IDEA中粘贴代码时出现大量空格占位符NBSP,可以通过取消勾选AdvancedSettings中的相应选项来解决... 目录1、背景介绍2、解决办法3、处理完成总结1、背景介绍python在idehttp://www.chinasem.cna粘贴代码,出

JAVA SpringBoot集成Jasypt进行加密、解密的详细过程

《JAVASpringBoot集成Jasypt进行加密、解密的详细过程》文章详细介绍了如何在SpringBoot项目中集成Jasypt进行加密和解密,包括Jasypt简介、如何添加依赖、配置加密密钥... 目录Java (SpringBoot) 集成 Jasypt 进行加密、解密 - 详细教程一、Jasyp

linux实现对.jar文件的配置文件进行修改

《linux实现对.jar文件的配置文件进行修改》文章讲述了如何使用Linux系统修改.jar文件的配置文件,包括进入文件夹、编辑文件、保存并退出编辑器,以及重新启动项目... 目录linux对.jar文件的配置文件进行修改第一步第二步 第三步第四步总结linux对.jar文件的配置文件进行修改第一步进

Go语言中如何进行数据库查询操作

《Go语言中如何进行数据库查询操作》在Go语言中,与数据库交互通常通过使用数据库驱动来实现,Go语言支持多种数据库,如MySQL、PostgreSQL、SQLite等,每种数据库都有其对应的官方或第三... 查询函数QueryRow和Query详细对比特性QueryRowQuery返回值数量1个:*sql

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

C#实现SHP文件读取与地图显示的完整教程

《C#实现SHP文件读取与地图显示的完整教程》在地理信息系统(GIS)开发中,SHP文件是一种常见的矢量数据格式,本文将详细介绍如何使用C#读取SHP文件并实现地图显示功能,包括坐标转换、图形渲染、平... 目录概述功能特点核心代码解析1. 文件读取与初始化2. 坐标转换3. 图形绘制4. 地图交互功能缩放