1、距离测量

距离测量时,片段长度通过两点之间距离计算得到,全部长度通过片段长度的和计算得到。主要用到INewLineFeedback和IScreenDisplay两个接口。

1)INewLineFeedback接口

该接口的主要方法如表:

方法 描述
Start 从指定点开始绘制追踪线的绘制
MoveTo 鼠标移动到的点,并实时绘制与上一节点的连接线
AddPoint 添加一个点
Stop 停止追踪线的绘制,并返回用户绘制的几何体

2)IScreenDisplay接口

MapControl中的每一个视图都有一个ScreenDisplay对象,用于控制视图中的图形绘制。ScreenDisplay对象除了管理窗体屏幕的显示属性外,也管理缓存和视图屏幕的变化等。可以通过IActiveView接口的ScreenDisplay属性获取ScreenDisplay对象。通ScreenDisplay对象的DisplayTransformation属性进行设备单位和地图单位的转换。

3)实现的思路

    • 鼠标点击时,首先判断INewLineFeedback接口的实例化对象pNewLineFeedback是否为空,如果为空就实例化,并设当前的鼠标点为pNewLineFeedback的起始点。反之,则把当前鼠标点添加到pNewLineFeedback中。
    • 鼠标移动时。实时计算鼠标移动点与上一鼠标点击点的距离,以及所画线的长度。
    • 鼠标双击时,停止绘制,并清空pNewLineFeedback对象
    • btnMeasureLine的单击事件打结果窗体,并设置bMeasurelength为true,以便从数据视图的鼠标事件中判断量算功能是否开启。

2、面积量测

面积量测通过INewPolygonFeedback接口绘制多边形来实现。使用NewPolygonFeedback和使用NewLineFeedback的方法类似,但其显示和返回的几何特征是一个封闭多边形,这意味着画多边形停止时,起点将成为终点,从而结束形状的绘制,该方法至少需要三个点被添加到几何对象中。

实现的思路如下

1)、点击鼠标时首先判断INewPolygonFeedback接口的实例化对象pNewPolygonFeedback是否为空,则实例化,并设当前鼠标点为pNewPolygonFeedback的起始点;反之则把当前的鼠标点添加到pNewPolygonFeedback中。

2)、鼠标移动时,判断绘制多边形时点的个数pPointCol是否超过3个点,如果超过三个,则由点击构建IPolygon接口,IArea接口,进而计算出面的总长度和面积。

3)、鼠标双击时,停止绘制,并清空pNewPloygonFeedback对象。


3、面积测量和距离测量的具体代码分布:

1.前提:新建一个用来显示测量结果的公共窗体
2.二者的button_click事件:
  #region 距离的测量按钮单击事件
private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
{
mainMapControl.CurrentTool = null;
pMouseOperate = "MeasureLength";
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
if (frmMeasureResult == null || frmMeasureResult.IsDisposed)
{
frmMeasureResult = new FormMeasureResult();
frmMeasureResult.frmClosed += new FormMeasureResult.FormClosedEventHandler(frmMeasureResult_frmColsed);
frmMeasureResult.labToltal.Text = "";
frmMeasureResult.Text = "距离量测";
frmMeasureResult.Show();
}
else
{
frmMeasureResult.Activate();
}
}
#endregion
  #region 面积测量按钮单击事件
private void btnPolyMeasure_ItemClick(object sender, ItemClickEventArgs e)
{
mainMapControl.CurrentTool = null;
pMouseOperate = "MeasureArea";
mainMapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;//光标样式为十字丝
if (frmMeasureResult == null || frmMeasureResult.IsDisposed)
{
frmMeasureResult = new FormMeasureResult();
frmMeasureResult.frmClosed += new FormMeasureResult.FormClosedEventHandler(frmMeasureResult_frmColsed); ;
frmMeasureResult.labToltal.Text = "";
frmMeasureResult.Text = "面积测量";
frmMeasureResult.Show();
}
else
{
frmMeasureResult.Activate();
}
}
#endregion
3.在mainForm中定义全局变量
  #region 变量的定义
//变量的定义
//距离和面积量测的变量
private FormMeasureResult frmMeasureResult = null;//量算的结果窗体
private INewLineFeedback pNewLineFeedback;//追踪线对象
private INewPolygonFeedback pNewPolyFeedback;//追踪面对象
private IPoint pPoint = null;//鼠标点击点
private IPoint pMovePt = null;//鼠标移动时的当前点
private double dToltalLength = ;//量测总长度
private double dSegmentLength = ;//片段距离
private IPointCollection pArearPointCol = new MultipointClass();//对面积量算时画的点进行存储
//定义状态栏坐标显示工具
private string sMapUnits = "未知单位";//地图单位变量
private object missing = Type.Missing;
#endregion
4.在mianMapControl控件的OnMouse_Down事件中进行switch语句注册
 #region   距离量测
case "MeasureLength":
//判断追踪线对象是否为空,若是则实例化并设置当前鼠标点为起始点
if (pNewLineFeedback == null)
{
//实例化追踪线对象
pNewLineFeedback = new NewLineFeedbackClass();
pNewLineFeedback.Display = (mainMapControl.Map as IActiveView).ScreenDisplay;
//设置起点,开始动态线绘制
pNewLineFeedback.Start(pPoint);
dToltalLength = ;
}
else //如果追踪线对象不为空,则添加当前鼠标点
{
pNewLineFeedback.AddPoint(pPoint);
}
//pGeometry = m_PointPt;
if (dSegmentLength != )
{
dToltalLength = dToltalLength + dSegmentLength;
}
break;
#endregion
#region 面积测量
case "MeasureArea":
if (pNewPolyFeedback == null)
{
//实例化追踪面对象
pNewPolyFeedback = new NewPolygonFeedback();
pNewPolyFeedback.Display = (mainMapControl.Map as IActiveView).ScreenDisplay;
pArearPointCol.RemovePoints(, pArearPointCol.PointCount);
//开始绘制多边形
pNewPolyFeedback.Start(pPoint);
pArearPointCol.AddPoint(pPoint, ref missing, ref missing);
}
else
{
pNewPolyFeedback.AddPoint(pPoint);
pArearPointCol.AddPoint(pPoint,ref missing,ref missing ); }
break;
#endregion
5.在mapControl的OnMouseMove事件中进行设置鼠标移动的实时测量结果
  #region 长度量算 实时显示测量结果
if (pMouseOperate == "MeasureLength")
{
if (pNewLineFeedback != null)
{
pNewLineFeedback.MoveTo(pMovePt);
}
double deltaX = ; //两点之间X差值
double deltaY = ; //两点之间Y差值 if ((pPoint != null) && (pNewLineFeedback != null))
{
deltaX = pMovePt.X - pPoint.X;
deltaY = pMovePt.Y - pPoint.Y;
dSegmentLength = Math.Round(Math.Sqrt((deltaX * deltaX) + (deltaY * deltaY)), );
dToltalLength = dToltalLength + dSegmentLength;
if (frmMeasureResult != null)
{
frmMeasureResult.labToltal .Text = String.Format("当前线段长度{0}{1}",dSegmentLength,sMapUnits );
frmMeasureResult.labToltalLength.Text = String.Format("总长度:{0}{1}",dToltalLength,sMapUnits );
//// frmMeasureResult.labToltal.Text = String.Format(
// "当前线段长度:{0:.###}{1};\r\n总长度为: {2:.###}{1}",
// dSegmentLength, sMapUnits, dToltalLength);
dToltalLength = dToltalLength - dSegmentLength; //鼠标移动到新点重新开始计算
}
frmMeasureResult.frmClosed += new FormMeasureResult.FormClosedEventHandler(frmMeasureResult_frmColsed);
}
}
#endregion
#region 面积量算
if(pMouseOperate=="MeasureArea")
{
if(pNewPolyFeedback!=null)
{
pNewPolyFeedback .MoveTo(pMovePt);
}
IPointCollection pPointCol=new Polygon();
IPolygon pPolygon=new PolygonClass();
IGeometry pGeo=null;
ITopologicalOperator pTopo=null;
for(int i=;i<=pArearPointCol.PointCount-;i++)
{
pPointCol.AddPoint(pArearPointCol.get_Point(i),ref missing,ref missing);
}
pPointCol.AddPoint(pMovePt,ref missing,ref missing);
if(pPointCol.PointCount<) return;
pPolygon=pPointCol as IPolygon;
if((pPolygon!=null))
{
pPolygon.Close();
pGeo=pPolygon as IGeometry;
pTopo=pGeo as ITopologicalOperator ;
//使几何图形的拓扑正确
pTopo.Simplify();
pGeo.Project(mainMapControl.Map.SpatialReference);
IArea pArea=pGeo as IArea ;//面积量算所画的面
frmMeasureResult.labToltal.Text=String.Format("总面积为:{0}平方{1}",pArea.Area,sMapUnits);
frmMeasureResult.labToltalLength.Text=String.Format("总长度为:{0}{1}",pPolygon.Length,sMapUnits);
pPolygon=null;
}
}
#endregion
6.mainMapControl的OnDoubleclick事件中
  #region 距离量测的结束设置
if (pMouseOperate == "MeasureLength")
{
if (frmMeasureResult != null)
{
frmMeasureResult .labToltalLength .Text = "线段总长度为:" + dToltalLength + sMapUnits;
}
if (pNewLineFeedback != null)
{
pNewLineFeedback.Stop();
pNewLineFeedback = null;
//清空所画的线对象
(mainMapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null);
}
dToltalLength = ;
dSegmentLength = ;
}
#endregion
#region 面积测量的结束设置
if (pMouseOperate == "MeasureArea")
{
if (pNewPolyFeedback != null)
{
pNewPolyFeedback.Stop();
pNewPolyFeedback = null;
//清空所画的线对象
(mainMapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewForeground, null, null);
}
pArearPointCol.RemovePoints(, pArearPointCol.PointCount);//清空点集中的所有点
}
#endregion

综上述:一个完美的距离和面积测量的工具就完成了!欢迎同行们相互交流和学习。

ArcGIS Engine开发之量测功能的更多相关文章

  1. ArcGIS Engine开发鹰眼图的功能(代码优化篇)

    在上一篇,ArcGIS Engine开发鹰眼图的功能(基础篇) 上一篇的实现效果图如下, 如果仔细观察,会发现一个问题,即在“鹰眼”的区域,只要在红色线框内的注记会被覆盖. 如果红色框包括整张图的话, ...

  2. ArcGIS Engine开发鹰眼图的功能(基础篇)

    鹰眼是用于调节全视域范围内主地图显示范围情况的副地图.它体现了地图整体与详细局部的关系. 用户可以通过鼠标单击或者画框等动作实现鹰眼与主地图的交互情况. 鹰眼功能的原理是通过主地图窗口的地图控件和鹰眼 ...

  3. ArcGIS Engine开发的ArcGIS 版本管理的功能

    原文:ArcGIS Engine开发的ArcGIS 版本管理的功能 转自:http://blog.csdn.net/linghe301/article/details/7965901 这是以前的Arc ...

  4. ArcGIS Engine开发之地图基本操作(4)

    ArcGIS Engine开发中数据库的加载 1.加载个人地理数据库数据 个人地理数据库(Personal Geodatabase)使用Miscrosoft Access文件(*.mdb)进行空间数据 ...

  5. ArcGIS Engine开发前基础知识(3)

    对象模型图 一.对象模型图中的类与接口 ArcGIS Engine 提供大量的对象,这些对象之间存在各种各样的关系,如继承.组合.关联等.对象模型图(Object model diagram,ODM) ...

  6. ArcGIS Engine开发前基础知识(2)

    ArcGIS基本控件简介 ArcGIS Engine控件是一组可视化的开发组件,每个ArcGIS Engine控件都是一个COM组件.这些组件包括MapControl,PageLayoutContro ...

  7. C#,ArcGIS Engine开发入门教程

    C#,ArcGIS Engine开发入门教程 转自:http://blog.csdn.net/yanleigis/article/details/2233674  目录(?)[+] 五实现 一 加载A ...

  8. ArcGIS Engine开发基础总结(一)

    标准Engine功能 地图浏览    地图制作 数据查询 数据分析 及 所有的开发控件 —MapControl, PageLayout, Toolbar, TOC, ArcReader 对所有矢量和栅 ...

  9. VC、OpenGL、ArcGIS Engine开发的二维三维结合的GIS系统

    一.前言 众所周知,二维GIS技术发展了近四十年,伴随着计算机软硬件以及关系型数据库的飞速发展,二维GIS技术已日臻完善.在对地理信息的分析功能上有着无可比拟的优势.一些宏观的地理信息,一维的地理信息 ...

随机推荐

  1. 瞬间记住Javascript中apply与call的区别

    关于Javascript函数的apply与call方法的用法,网上的文章很多,我就不多话了.apply和call的作用很相似,但使用方式有区别 apply与call的第一个参数都是一个对象,这个对象就 ...

  2. 腾讯云 安装mono

    一.yum -y update 运行出现以下错误: http://centos.tencentyun.com/contrib/x86_64/repodata/filelists.xml.gz: [Er ...

  3. ucos实时操作系统学习笔记——任务间通信(消息)

    ucos另一种任务间通信的机制是消息(mbox),个人感觉是它是queue中只有一个信息的特殊情况,从代码中可以很清楚的看到,因为之前有关于queue的学习笔记,所以一并讲一下mbox.为什么有了qu ...

  4. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  5. Entity Framework 6 Recipes 2nd Edition(13-7)译 -> 返回只部分填充的实体

    问题 你有一个实体里的某个属性很少被读取或和更新,这个属性因为比较大,所以读取和更新都需要付很大的代价.你想有选择的放置这个属性 解决方案 假设你有一个如Figure 13-9 所示的模型 Figur ...

  6. babel presets stage-x

    在一些新框架的代码中,常基于es6/7标准来书写代码.鉴于这些标准被没有被浏览器广泛支持,我们一般使用babel来将使用e6/7标准书写的代码降级编译(或者说转译)为浏览器可解析的es4/5代码. 以 ...

  7. 如何隐藏DIV对象

    DIV对象在网页里面,相当于一个容器,在其内部,可以显示文字.图片.视频控件等等. 以下的教程,和大家一起来学习,如何隐藏DIV对象. 这必须使用CSS来控制,才能达到隐藏的目的,那么,就得使用CSS ...

  8. python的print函数的格式化输出

    使用print函数的时候,可以像C一样格式化输出,同时还支持参数化输出 print('%s' % ("CooMark")) print('整数|%d|' % (123)) prin ...

  9. ERP程序开发中遇到的六种错误

    经常回顾同事写的代码,发现一些问题,总结分析,用于员工培训,或系统优化方面的内容教学. 文中有问题的的代码我用黑体字标识. 1 界面与逻辑代码混淆 这是目前发现的比较严重的问题.框架花费了很大的力气, ...

  10. Linux epoll

    一. epoll函数集 epoll主要有三个函数: 1. int epoll_create(int size); 创建一个epoll的句柄,size用来告诉内核这个监听的数目一共有多大.这个参数不同于 ...