基于C#的ArcEngine二次开发23:复合要素的识别与导出

目录

1 复合要素的定义

2. 复合要素的识别与导出

2.1 判断复合要素

2.2 输出为shp文件

2.3 No support for this geometry type异常及处理

3 带环面的处理

3.1 手工造环

3.2 程序判断


1 复合要素的定义

所谓复合要素就是一个图形有多个部分构成,

例如一条线由分离的两段构成,如下图:

一个面有两个分离的面构成:

2. 复合要素的识别与导出

2.1 判断复合要素

对复合要素的识别原理与之前的博文基于C#的ArcEngine二次开发21:线(面)要素折角监测、折点提取与shape文件导出类似,

之前提取折点使用的接口是

IPointCollection pPc = pFeature.Shape as IPointCollection;

现在我们要找的是要素的几何图形:

使用的接口是:

IGeometry pGeometry = pFeature.ShapeCopy;

IGeometryCollection pGeocoll = pGeometry as IGeometryCollection;

int geomcount = pGeocoll.GeometryCount;

如果geomcount大于1,说明这个要素是复合素,我们将该要素提取并输出即可

2.2 输出为shp文件

与之前的博文基于C#的ArcEngine二次开发21:线(面)要素折角监测、折点提取与shape文件导出类似,进行函数改编:

       /// <summary>
        /// 导出要素为shape文件
        /// </summary>
        /// <param name="tProjectedCoordinateSystem"></param>
        /// <param name="ouFeatures"></param>
        /// <param name="ouPath"></param>
        public void ExportPtsToShapefile(ISpatialReference reference,
            List<IFeature> ouFeatures, string ouPath)
        {
            //1. 打开工作空间
            string strShapeFolder = System.IO.Path.GetDirectoryName(ouPath);
            const string strShapeFieldName = "shape";
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(strShapeFolder, 0);
            //2. 设置字段集
            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

            //3. 设置字段
            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;
            //4. 创建类型为几何类型的字段
            pFieldEdit.Name_2 = strShapeFieldName;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            //为esriFieldTypeGeometry类型的字段创建几何定义,包括类型和空间参照
            IGeometryDef pGeoDef = new GeometryDefClass(); //The geometry definition for the field if IsGeometry is TRUE.
            IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;

            pGeoDefEdit.SpatialReference_2 = reference;

            //设置要素的类型
            IGeometry geoType = ouFeatures[0].Shape as IGeometry;
            pGeoDefEdit.GeometryType_2 = geoType.GeometryType;
            //导出与查询结果一致的几何类型要素
            //此处必须根据要导出的数据类型进行修改,否则会出现不支持几何图形的错误


            pFieldEdit.GeometryDef_2 = pGeoDef;
            pFieldsEdit.AddField(pField);

            //创建shapefile
            string strShapeName = System.IO.Path.GetFileName(ouPath);
            IFeatureClass shpFeatureClass = pWS.CreateFeatureClass(strShapeName, pFields, null, null, esriFeatureType.esriFTSimple, strShapeFieldName, "");
            
            //向shape中添加要素
            foreach (IFeature of in ouFeatures)
            {
                IFeature pFeature = shpFeatureClass.CreateFeature();
                pFeature.Shape = of.Shape;
                pFeature.Store();
            }
        }

2.3 No support for this geometry type异常及处理

发生该异常的原因是:

  • shape设置的几何图形与实际的几何图形类型不一致

处理方法:

  • 使用IGeometry接口获得当前复合要素的类型esriGeometryType;然后把原始要素的几何类型设置为输出要素的几何类型

3 带环面的处理

对线要素,简单的判断几何图形的个数即可判断其是否为复合要素;但是对于面要素,还需要考虑带有环面的情形;此时单纯依靠判断复合要素的个数的方法已经失效,需要重新设计方法进行处理。

      

说明:

A polygon is a collection of one or many exterior and interiorrings.  The rings do not need to be connected to or contained by other rings in the polygon.  However, all rings are considered to be part of a single polygon regardless of their location.  Rings can be embedded in the interior of other rings.  Embedded rings define interior boundaries or holes within the polygon.  Exterior rings are oriented in a clockwise direction while interior rings are oriented counterclockwise.

多边形是一个包含一个或多个内环或外环的集合,环不一定与这个多边形相连,但环一定是多边形的一部分;外圈顺时针,内圈逆时针

此时我们只要判断外环的个数,如果外环个数大于1,则必定为复合要素。

3.1 手工造环

步骤:

  • 打开编辑器,在当前多边形内部【创建要素】

  • 点击编辑器下拉符号,选择【裁剪】,在弹出的界面上【丢弃相交区域】

3.2 程序判断

思路:

  • 判断是否为点要素,点要素不做检查
  • 线要素只要几何图形数目大于1即为复合要素
  • 面要素外环数目大于1为复合要素
                esriGeometryType geoType = pGeometry.GeometryType;
                //点层不存在复合要素,仅对非点层进行判断
                if (geoType != esriGeometryType.esriGeometryPoint)
                {
                    //判断有几个几何要素构成
                    IGeometryCollection pGeocoll = pGeometry as IGeometryCollection;
                    //获取几何要素的类型             
                    int geomcount = pGeocoll.GeometryCount;
                    if (geomcount >= 2)
                    {
                        switch (geoType)
                        {
                            case esriGeometryType.esriGeometryPolygon:
                                //面层判断外环个数,如果大于1,则为复合要素
                                IPolygon poly = pGeometry as IPolygon;
                                if (poly.ExteriorRingCount > 1)
                                {
                                    comEleLists.Add(pFeature);
                                }
                                break;
                            case esriGeometryType.esriGeometryPolyline:
                                comEleLists.Add(pFeature);
                                break;
                            default:
                                break;
                        }
                        //comEleLists.Add(pFeature);
                    }     
                }

4 拓展

4.1 获取面心点坐标

IPoint centerPoint =new PointClass();//获得要素的中心点
IArea pArea = pFeature.Shape as IArea;
pArea.QueryCentroid(centerPoint);

更多精彩,欢迎关注

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小薛引路

喜欢的读者,可以打赏鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值