ArcGis 拓扑检查——狭长角代码C#

源:https://www.cnblogs.com/yzhyingcool/p/10009534.html

中学的时候醉心于研究怎么“逃课”,大学的时候豁然开悟——最牛逼的逃课是准时准地儿去上每一课,却不知到老师讲的啥,“大隐隐于市”大概就是这境界吧。

用到才听说有“余弦定理”这么一个东西,遂感叹“白上了大学”。

又百度了一下,高中数学……

检查角度先要根据已知的3点计算出其所组成的夹角的角度,这就需要“余弦定理”了。

其代码实现应该是下面的样子:

private static double GetAngle(IPoint first, IPoint cen, IPoint last)
        {
            double ma_x = first.X - cen.X;
            double ma_y = first.Y - cen.Y;
            double mb_x = last.X - cen.X;
            double mb_y = last.Y - cen.Y;
            double ab_x = first.X - last.X;
            double ab_y = first.Y - last.Y;
            double ab_val2 = ab_x * ab_x + ab_y * ab_y;
            double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y);
            double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y);
            double cosM = (ma_val * ma_val + mb_val * mb_val - ab_val2) / (2 * ma_val * mb_val);
            double angleAMB = Math.Acos(cosM) / System.Math.PI * 180;
            return angleAMB;
        }

 

在检查方法CheckAcuteAngle中调用GetAngle方法获取角度值,CheckAcuteAngle方法传入IFeatureClass类型的要素类(应该是一个面层的)与给定的double型角度上限值。返回值是有错的要素List与其对应的角度值List。

public static List<int> CheckAcuteAngle(IFeatureClass pFeatureClass, double acuteAngle, out List<double> listAngle)
        {
            IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)m_hookHelper.FocusMap.ActiveGraphicsLayer;
            pGraphicsContainer.DeleteAllElements();
            IColor color = DisplayUtils.RGBColor(255, 0, 0);
            List<int> listError = new List<int>();
            List<double> listOutAngle = new List<double>();
            IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
            IFeature pFeature = pFeatureCursor.NextFeature();
            try
            {
                if (pFeature != null)
                {
                    IPointCollection Temp_Vertices;
                    IPoint First_Point = new Point();
                    IPoint Center_Point = new Point();
                    IPoint Last_Point = new Point();
                    while (pFeature != null)
                    {   
                        IGeometryCollection pGeometryCollection=pFeature.Shape as IGeometryCollection;
                        int count = pGeometryCollection.GeometryCount;
                        for (int j = 0; j < count; j++)
                        {
                            IGeometry pGeometry = pGeometryCollection.get_Geometry(j);
                            Temp_Vertices = pGeometry as IPointCollection;
                            Temp_Vertices.AddPoint(Temp_Vertices.get_Point(1));
                            for (int i = 0; i < Temp_Vertices.PointCount – 2; i++)
                            {
                                Temp_Vertices.QueryPoint(i, First_Point);
                                Temp_Vertices.QueryPoint(i + 1, Center_Point);
                                Temp_Vertices.QueryPoint(i + 2, Last_Point);
                                double angle = GetAngle(First_Point, Center_Point, Last_Point);
                                if (angle <= acuteAngle || angle >= 360 – acuteAngle)
                                {
                                    listError.Add(pFeature.OID);
                                    listOutAngle.Add(angle);

                                    IPointArray pointArray = new PointArrayClass();
                                    pointArray.Add(First_Point);
                                    pointArray.Add(Center_Point);
                                    pointArray.Add(Last_Point);

                                    IElement pElement = MarkElementUtils.PointArray2LineMarkElement(pointArray, 1.0, color);
                                    pGraphicsContainer.AddElement(pElement, 0);
                                }
                            }
                        }
                        pFeature = pFeatureCursor.NextFeature();
                    }
                }
            }
            catch (Exception)
            {
                throw new Exception(“执行角度计算时发生错误,错误FeatureID为:”+pFeature.OID);
            }
            finally
            {
                Marshal.FinalReleaseComObject(pFeatureCursor);
            }
            listAngle = listOutAngle;
            return listError;
        }

转载自:https://blog.csdn.net/qq_29011299/article/details/84796936

You may also like...

退出移动版