ArcEngine|为点要素类的属性表添加XY坐标值

前言

在最近的项目中,需要对面要素转成的点要素进行坐标的分析,但转换后的数据属性表中并没有X坐标和Y坐标,因此需要使用Arcengine获得所有点的坐标值并将其添加至要素的属性表中。

在Arcmap中实现的操作如下:

打开ArcToolBox中的数据管理工具——>要素——>添加XY坐标,即可为输入的要素添加XY坐标。

在Arcengine中的实现如下:

大致思路是遍历点要素类,对其中的每一个点要素,获取其经度和纬度的属性,然后将其写入属性表,因此在使用前,需要先为原要素的属性表添加X坐标字段与Y坐标字段,具体怎样做可以参考我的这篇博客ArcEinge|为要素类的属性表添加字段 -welt-  博客园 (cnblogs.com)。

代码实现

private IFeatureClass getXY(IFeatureClass fc)
        private IFeatureClass getXY(IFeatureClass fc)
        {
            ITable table = fc as ITable;
            int x_idx = -1, y_idx = -1;
            for (int j = 0; j < table.Fields.FieldCount; j++)
            {
                if (table.Fields.get_Field(j).Name.ToString() == "POINT_X") 
                {
                    x_idx = j;
                }
                else if (table.Fields.get_Field(j).Name.ToString() == "POINT_Y")
                {
                    y_idx = j;
                }
            }
            IFeatureCursor cursor = fc.Update(null, false);
            IFeature feature = cursor.NextFeature();
            ITable gtable = fc as ITable;
            ICursor pCursor = gtable.Search(null, false);
            IRow pRow = pCursor.NextRow();

            do
            {
                IPoint point = feature.Shape as IPoint;
                double x, y;
                point.QueryCoords(out x, out y); // 在这里得到坐标

                string typex = pRow.get_Value(x_idx).ToString();  // 在这里得到要操作字段的值
                string typey = pRow.get_Value(y_idx).ToString();  // 在这里得到要操作字段的值

                feature = cursor.NextFeature();
                pRow = pCursor.NextRow();
            } while (feature != null);
            fc.Update(null, true);

            return fc;
        }

 

posted @ 2022-09-04 10:36  Weltㅤ  阅读(308)  评论(0编辑  收藏  举报