当先锋百科网

首页 1 2 3 4 5 6 7

前言:

熬过了一阵子忙碌,也一直没有写,不过相信经过了前几个作业后面的作业,很多人都可以自主摸索了,而且感觉作用并不大,关键还是在自己的摸索吧。关于阶段学习的总结在之后抽空写点,确实自己也走了很多弯路,不多言。在此以下仅给出个人的思路,仅供参考,代码尽量自己敲,尽量能够看懂每行代码!

作业:

EX05:编写程序,使用QueryFilter、SpatialFilter和FeatureCursor

  1. 在Country图层中,以"LANDLOCKED = \'Y\'"为条件进行查询,并对查询结果汇总Country的个数及面积之和。
  2. 在Country中先选中一个Country(例如Brazil),而后以"Population > 2000000"为条件查询该Country中的所有Cities,统计其个数。
  3. 将2中满足条件的Cities置于被选中状态。

(1)窗体设计和实现

(2)代码部分

using ESRI.ArcGIS;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
            InitializeComponent();
        }

以下代码实现第一个点击访问属性的功能,自行看注释

public int FindLayer(string FeatureName)       //自定义方法→根据要素名称获取索引
        {
            int i = 0;
            for (; i < axMapControl1.LayerCount; i++)
            {
                if (axMapControl1.get_Layer(i).Name == FeatureName)
                    break;
            }
            return i;
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            try
            {
                int LayerIndex = FindLayer("Country");
                IFeatureLayer pFL = axMapControl1.get_Layer(LayerIndex) as IFeatureLayer;
                IFeatureClass pFC = pFL.FeatureClass;                   //获取要素类

                IQueryFilter pQF = new QueryFilterClass();
                pQF.WhereClause = @"LANDLOCKED = 'Y' ";
                IFeatureCursor pFCur = pFC.Search(pQF, true);
                IFeature pFea = pFCur.NextFeature();            //建立要素访问游标

                int index = pFC.FindField("shape_Area");            //获取面积字段
                int Count1 = 0;
                double SumArea = 0;
                while (pFea != null)
                {
                    string str = pFea.get_Value(index).ToString();
                    SumArea += double.Parse(str);
                    pFea = pFCur.NextFeature();
                    Count1++;
                }
                SumArea *= 0.001 * 0.001;       //转换为km²
                MessageBox.Show("landlocked国家总面积为: " + SumArea.ToString() + "km²  \n" + "    国家数量为:" + Count1.ToString());
            }
            catch 
            {
                MessageBox.Show("请先加载mxd文件:QueryFilters.mxd");
            }//未加载文件时点击会报错


        }

以下代码实现点选提示信息以及空间查询SpatialFilter功能

        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
            //选择面图层并显示所选择的的国家名称
        {
            //
            IPoint pPoint = new PointClass();                        //实例化一个点并以该点作拓扑算子
            ITopologicalOperator pTO = pPoint as ITopologicalOperator;      //将点击的位置坐标赋予pPoint
            pPoint.PutCoords(e.mapX, e.mapY);                           //以缓冲半径为0进行缓冲  得到一个点
            IGeometry pGeometry = pTO.Buffer(0);       //以该点进行要素选择(只能选中面状要素,点和线无法选中)

            axMapControl1.Map.SelectByShape(pGeometry, null, false);            
            axMapControl1.Refresh();                            //刷新
            get_ContryName();
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            try 
            {
                int LayerIndex = FindLayer("Cities");
                IFeatureLayer pFL = axMapControl1.get_Layer(LayerIndex) as IFeatureLayer;
                IFeatureClass pFC = pFL.FeatureClass;                   //获取要素类

                IEnumFeature enumf = axMapControl1.Map.FeatureSelection as IEnumFeature;
                IFeature pFea = enumf.Next();

                ISpatialFilter pSF = new SpatialFilterClass();      //建立pSF
                pSF.Geometry = pFea.Shape;                           //与选择集建立联系
                pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;     //空间查询类型为包含
                pSF.WhereClause = "Population > 2000000";

                IFeatureCursor pFCur = pFC.Search(pSF, true);       //建立游标
                IFeature pFea2 = pFCur.NextFeature();
                int Count = 0;
                while (pFea2 != null)
                {
                    Count++;
                    pFea2 = pFCur.NextFeature();
                }

                MessageBox.Show("该国家中人口数大于2,000,000的城市有: " + Count.ToString() + "  个");
            }
            catch 
            {
                MessageBox.Show("请先选择区域!");
            }//未选择区域时点击会报错

        }
        public void get_ContryName()        //自定义方法→显示点选国家的名称
        {
            int LayerIndex = FindLayer("Country");
            IFeatureLayer pFL = axMapControl1.get_Layer(LayerIndex) as IFeatureLayer;
            IFeatureClass pFC = pFL.FeatureClass;
            int index = pFC.FindField("CNTRY_NAME");//获取字段

            ISelection pSelection = axMapControl1.Map.FeatureSelection;     //获取当前选择集
            IEnumFeatureSetup pEnumFeatureSetup = pSelection as IEnumFeatureSetup;// 打开属性标签
            pEnumFeatureSetup.AllFields = true;                          // 获取要素字段

            IEnumFeature pEnumFeature = pSelection as IEnumFeature;     //通过QI加载选择集中的要素
            IFeature pFea = pEnumFeature.Next();
            MessageBox.Show("你选中的国家为:  " + pFea.get_Value(index).ToString());          //显示所点选的国家名称
        }

以下代码实现高亮满足条件城市功能

        private void btn3_Click(object sender, EventArgs e)
        {
            try
            {
                int LayerIndex = FindLayer("Cities");
                IFeatureLayer pFL = axMapControl1.get_Layer(LayerIndex) as IFeatureLayer;
                IFeatureClass pFC = pFL.FeatureClass;

                IEnumFeature enumf = axMapControl1.Map.FeatureSelection as IEnumFeature;
                IFeature pFea = enumf.Next();

                ISpatialFilter pSF = new SpatialFilterClass();      //建立pSF
                pSF.Geometry = pFea.Shape;
                pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;     //空间查询类型为包含
                pSF.WhereClause = "Population > 2000000";

                IFeatureCursor pFCur = pFC.Search(pSF, false);
                IFeature pFea2 = pFCur.NextFeature();
                axMapControl1.Map.ClearSelection();
                while (pFea2 != null)
                {

                    axMapControl1.Map.SelectFeature (pFL, pFea2);//ISelectFeature接口用于高亮显示,将游标当前所指要素添加到高亮选择集
                    pFea2 = pFCur.NextFeature();
                }
                axMapControl1.Refresh();
            }
            catch
            {
                MessageBox.Show("当前面域中没有包含可查询要素,请勿多次点击,请重新选择!");
            }//多次点击无城市国家会报错!

        }

以上为个人本次作业的实现