VC++开发GIS系统(293)区量算之类的设计及实现

本文创建一个类用于实现区量算工作。功能演示如下:

GIS系统区量算类的定义

在 _malaAnalysis.h 中定义GIS自定义量算的类,代码如下:

/*
* 多边形量算
*/
class malaanalysisdll CmalaPolyMeasure :public CmalaMouseAction
{
public:
  CmalaPolyMeasure(CView* mView, malaScreen *pScreen, CString &fileFullPath);
  ~CmalaPolyMeasure();
  void LButtonDown(UINT nFlags, malaPoint point);
  void LButtonUp(UINT nFlags, malaPoint point);
  void MouseMove(UINT nFlags, malaPoint point);
private:
  malaPolyPro m_polypro;
  vector<malaPoint>m_poly;
  CmalaPolysSelect m_SelectPoly;
  bool m_Selected;
  bool m_bDraw;
  malaPoint m_ptOrigin;
  malaPoint m_perPoint;
  malaScreen *m_Screen;
  CString mPath;//文件路径
};
  • @ *m_Screen 屏幕坐标
  • @ m_PtOrigin 起点
  • @ m_PerPoint 临时的点
  • @ m_bDraw 标记是否按下左键
  • @ m_polypro 区的属性
  • @ m_poly 组成区的每一个点
  • @ m_SelectPoly 选择的区

类的实现

在 _malaAnalysis.cpp 中实现上述类的定义,代码如下:

/*
* 多边形量算
*/
CmalaPolyMeasure::CmalaPolyMeasure(CView* mView, malaScreen *pScreen, CString &fileFullPath)
{
  mBaseView = mView;

  mPath = fileFullPath;
  m_Screen = pScreen;

  CmalaPolysSelect obj(mBaseView, m_Screen, mPath);
  m_SelectPoly = obj;
  this->m_Selected = m_SelectPoly.m_Selected;
}

CmalaPolyMeasure::~CmalaPolyMeasure()
{

}

void CmalaPolyMeasure::LButtonDown(UINT nFlags, malaPoint point)
{
  if (!m_Selected)
    m_SelectPoly.LButtonDown(nFlags, point);
  else
  {
    m_bDraw = TRUE;
    m_ptOrigin = m_perPoint = point;
  }
}

void CmalaPolyMeasure::LButtonUp(UINT nFlags, malaPoint point)
{
  if (!m_Selected)
    m_SelectPoly.LButtonUp(nFlags, point);

  m_Selected = m_SelectPoly.m_Selected;
  if (m_Selected)
  {
    m_poly = m_SelectPoly.mSPoly;
    m_polypro = m_SelectPoly.mSPolyPro;
    malaLogic tMath;
    double area = tMath.ComputePolygonArea(m_poly);
    m_poly.push_back(m_poly[0]);
    double dis = tMath.distanceLine(m_poly);
    CString str;
    str.Format(L"面积:%f \n周长:%f", area, dis);
    MessageBox(mBaseView->m_hWnd, str, L"量算结果", MB_OK);
    m_Selected = FALSE;
    m_SelectPoly.m_Selected = FALSE;
  }
}

void CmalaPolyMeasure::MouseMove(UINT nFlags, malaPoint point)
{
  if (!m_Selected)
    m_SelectPoly.MouseMove(nFlags, point);
}

其中 distanceLine 的实现参考文章《VC++开发GIS系统(287)计算一条折线的长度》。ComputePolygonArea 用于计算多边形(区)的面积,可以参考文章《VC++开发GIS系统(292)计算任意多边形的面积

详细代码可以参考:GitHub

如您有疑问,可在文末留言,或到QQ群提问。
本站QQ群:291616564 麻辣GIS
微信公众号:malagis,扫描二维码直接关注。

打赏¥1

作者:,GIS爱好者。
分享本文,请您带上本文链接
分享到:

发表评论