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

时间:2021-07-01 23:28:28

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

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

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

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

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

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

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

  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) / ( * ma_val * mb_val);
double angleAMB = Math.Acos(cosM) / System.Math.PI * ;
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(, , );
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 = ; j < count; j++)
{
IGeometry pGeometry = pGeometryCollection.get_Geometry(j);
Temp_Vertices = pGeometry as IPointCollection;
Temp_Vertices.AddPoint(Temp_Vertices.get_Point());
for (int i = ; i < Temp_Vertices.PointCount - ; i++)
{
Temp_Vertices.QueryPoint(i, First_Point);
Temp_Vertices.QueryPoint(i + , Center_Point);
Temp_Vertices.QueryPoint(i + , Last_Point);
double angle = GetAngle(First_Point, Center_Point, Last_Point);
if (angle <= acuteAngle || angle >= - 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, );
}
}
}
pFeature = pFeatureCursor.NextFeature();
}
}
}
catch (Exception)
{
throw new Exception("执行角度计算时发生错误,错误FeatureID为:"+pFeature.OID);
}
finally
{
Marshal.FinalReleaseComObject(pFeatureCursor);
}
listAngle = listOutAngle;
return listError;
}