ArcGIS Engine 10 开发手册(9-7)自定义的等高线赋值类

本文目录
  • 正文

本文分享一个用于自定义的等高线赋值类。

代码如下

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace EngineApplication

{

  public class CalculatContourTask : ESRI.ArcGIS.Controls.IEngineEditTask

  {

    #region Private Members IEngineEditor pEngineEditor; IEngineEditSketch pEditSketch; IEngineEditLayers pEditLayer;

    #endregion

    IFeatureLayer pFeatureLayer;

    #region IEngineEditTask Implementations

    public void Activate (ESRI.ArcGIS.Controls.IEngineEditor pEditor,

      ESRI.ArcGIS.Controls.IEngineEditTask pOldTask)

    {

      if (pEditor == null) return;

      pEngineEditor = pEditor;

      pEditSketch = pEngineEditor as IEngineEditSketch;
      pEditSketch.GeometryType = esriGeometryType.esriGeometryPolyline;
      pEditLayer = pEditSketch as IEngineEditLayers;

      //Listen to engine editor events 
      ((IEngineEditEvents_Event) pEditSketch).OnTargetLayerChanged += new IEngineEditEvents_OnTargetLayerChangedEventHandler (OnTargetLayerChanged);

      ((IEngineEditEvents_Event) pEditSketch).OnCurrentTaskChanged += new IEngineEditEvents_OnCurrentTaskChangedEventHandler (OnCurrentTaskChanged);

    }

    public void Deactivate ()

    {

      pEditSketch.RefreshSketch ();

      //Stop listening to engine editor events. 
      ((IEngineEditEvents_Event) pEditSketch).OnTargetLayerChanged -= OnTargetLayerChanged;

      ((IEngineEditEvents_Event) pEditSketch).OnCurrentTaskChanged -= OnCurrentTaskChanged;

      //Release object references. 
      pEngineEditor = null;
      pEditSketch = null;
      pEditLayer = null;

    }

    public string GroupName

    {

      get

      {

        //This property allows groups to be created/used in the        EngineEditTaskToolControl treeview.

        //If an empty string is supplied the task will be appear in an "Other        Tasks " group.

        //In this example the Reshape Polyline_CSharp task will appear in the existing Modify Tasks group.

        return "Modify Tasks ";

      }

    }

    public string Name

    {

      get

      {
        return "CalculateContourTask "; //unique edit task name
      }

    }

    public void OnDeleteSketch ()

    {

    }

    public void OnFinishSketch ()

    {

      //get reference to featurelayer being edited 
      pFeatureLayer = pEditLayer.TargetLayer as IFeatureLayer;

      //get reference to the sketch geometry 
      IGeometry pPolyline = pEditSketch.Geometry;

      if (pPolyline.IsEmpty == false)

      {

        ParaSetting pFormSetting = new ParaSetting (pFeatureLayer.FeatureClass);

        pFormSetting.ShowDialog ();

        if (pFormSetting.DialogResult == DialogResult.OK)

        {

          pHeightName = pFormSetting.pFieldNames.Text;

          pHeight = Convert.ToDouble (pFormSetting.dHeight.Text);
          pInterval = Convert.ToDouble (pFormSetting.dInterval.Text);
          pFormSetting.Dispose ();

          pFormSetting = null;

          IFeatureCursor pFeatureCursor = GetFeatureCursor (pPolyline, pFeatureLayer.FeatureClass);

          CalculateIntersect (pFeatureCursor, pPolyline);

          MessageBox.Show ("计算完成 ");

        }

      }

      //refresh the display

      IActiveView pActiveView = pEngineEditor.Map as IActiveView;
      pActiveView.PartialRefresh (esriViewDrawPhase.esriViewGeography, (object) pFeatureLayer, pActiveView.Extent);

    }

    public string UniqueName

    {

      get

      {
        return "CalculateContourTask";
      }

    }

    #endregion

    #region Event Listeners

    public void OnTargetLayerChanged ()

    {

      PerformSketchToolEnabledChecks ();

    }

    void OnCurrentTaskChanged ()

    {

      if (pEngineEditor.CurrentTask.Name == "CalculateContourTask")

      {

        PerformSketchToolEnabledChecks ();

      }

    }

    #endregion

    private IFeatureCursor GetFeatureCursor (IGeometry pGeometry, IFeatureClass pFeatureClass)

    {

      //空间过虑器的创建

      ISpatialFilter pSpatialFilter = new SpatialFilter ();
      pSpatialFilter.Geometry = pGeometry;

      //空间过虑器几何体实体

      //空间过虑器参照系

      //空间过虑器空间数据字段名

      pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;

      //空间过虑器空间关系类型

      pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;

      //相交

      IFeatureCursor pFeatureCursor = pFeatureClass.Search (pSpatialFilter, false);

      return pFeatureCursor;

    }

    //起始等高线值

    private double pHeight;

    //等高线间距

    private double pInterval;

    //高程字段名

    private string pHeightName;

    private void CalculateIntersect (IFeatureCursor pFeatureCursor, IGeometry pGeometry)

    {

      if (pFeatureCursor == null)

      {

        return;

      }

      //要素游标

      IMultipoint pIntersectionPoints = null;

      //多点

      IPointCollection pPointColl = null;

      List<IFeature> pFeatureList = new List<IFeature> ();

      //和直线相交的要素集合

      ITopologicalOperator pTopoOperator = pGeometry as ITopologicalOperator;
      IPointCollection pSketchPointColl = pGeometry as IPointCollection;

      //所画直线的起点

      IPoint pPoint0 = pSketchPointColl.get_Point (0);

      IFeature pFeature = pFeatureCursor.NextFeature ();
      pFeatureList.Clear ();

      while ((pFeature != null))

      {

        //和直线相交的要素集合 
        pFeatureList.Add (pFeature);

        pFeature = pFeatureCursor.NextFeature ();

      }

      IPolyline pPolyline = pGeometry as IPolyline;
      IPoint pPointF = pPolyline.FromPoint;

      Dictionary<double, IFeature> pDic = new Dictionary<double, IFeature> ();

      // IProximityOperator

      //此时pFeatureL中的等值线并不是按顺序(空间)排列,需要排序

      //求出各交点到直线起点距离

      int pCount = pFeatureList.Count;
      double[] sortArray = new double[pCount];
      for (int i = 0; i <= pCount - 1; i++)

      {

        try

        {

          pFeature = pFeatureList[i];

          //求交点:

          pIntersectionPoints = pTopoOperator.Intersect (pFeature.Shape, esriGeometryDimension.esriGeometry0Dimension) as IMultipoint;

          pPointColl = pIntersectionPoints as IPointCollection;
          sortArray[i] = GetDistace (pPointF, pPointColl.get_Point (0));

          pDic.Add (GetDistace (pPointF, pPointColl.get_Point (0)), pFeatureList[i]);

          //距离

          //下个要素

          pFeature = pFeatureCursor.NextFeature ();

        } catch (Exception e)

        {

          MessageBox.Show (e.ToString ());

        }

      }

      //冒泡法

      for (int H = sortArray.Length - 1; H >= 0; H--)

      {

        for (int j = 0; j < H; j++)

        {

          if (sortArray[j] > sortArray[j + 1])

          {

            double temp = sortArray[j];
            sortArray[j] = sortArray[j + 1];
            sortArray[j + 1] = temp;

          }

        }

      }

      int pFieldIndex = pFeatureLayer.FeatureClass.Fields.FindField (pHeightName);

      Dictionary<IFeature, double> pDicdis = new Dictionary<IFeature, double> ();

      for (int m = 0; m < sortArray.Length; m++)

      {

        foreach (KeyValuePair<double, IFeature> pKey in pDic)

        {

          if (sortArray[m] == pKey.Key)

          {

            IFeature pFeatureH = pKey.Value;

            pFeatureH.set_Value (pFieldIndex, pHeight + pInterval * m);
            pFeatureH.Store ();

          }

        }

      }

    }

    /// <summary>

    /// 获取我们画的线和等高线之间的距离

    /// </summary>

    /// <param name="pPoint1 "></param>

    /// <param name="pPoint2 "></param>

    /// <returns></returns>

    private double GetDistace (IPoint pPoint1, IPoint pPoint2)

    {

      //pPoint1.X = GetPlan(pPoint1.X);

      //pPoint1.Y = GetPlan(pPoint1.Y);

      //pPoint2.X = GetPlan(pPoint2.X);

      //pPoint2.Y = GetPlan(pPoint2.Y);

      //return (pPoint1.X - pPoint2.X) * (pPoint1.X - pPoint2.X) + (pPoint1.Y - pPoint2.Y) * (pPoint1.Y - pPoint2.Y);

      IProximityOperator pProximity = pPoint1 as IProximityOperator;

      return pProximity.ReturnDistance (pPoint2);

    }

    #region private methods

    private void PerformSketchToolEnabledChecks ()

    {

      if (pEditLayer == null) return;

      //Only enable the sketch tool if there is a polyline target layer.

      if (pEditLayer.TargetLayer.FeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)

      {

        pEditSketch.GeometryType = esriGeometryType.esriGeometryNull;
        return;

      }

      pEditSketch.GeometryType = esriGeometryType.esriGeometryPolyline;

    }

    #endregion

  }

}

ArcGIS Engine 10 开发手册全集

ArcGIS Engine 10 开发手册全集: ArcGIS Engine 10 开发手册

如果本文对你有所帮助,欢迎对我们团队进行打赏捐助,让我们在传播3S的路上可以走得更远。
赞赏支持
微信捐助麻辣GIS 支付宝捐助麻辣GIS

麻辣GIS-Sailor
作者:
GIS爱好者,学GIS,更爱玩GIS。

如您有疑问,可在文末留言,或到QQ群提问。

本站QQ群(一):291616564 麻辣GIS

本站QQ群(二):166408035 麻辣GIS(二)

微信公众号:malagis,扫描右边二维码直接关注。

发表评论