bool Bool = false
public IWorkspace GetFGDBWorkspace()//获取地理数据库
{
string path;
IWorkspaceFactory pWsFac = new FileGDBWorkspaceFactoryClass();
IWorkspace pWs = pWsFac.OpenFromFile(path, 0);
return pWs;
}
private double Leng(IPoint p1, IPoint p2)//此函数用来计算两个点坐标的距离
{
double length = 0;
length=Math.Sqrt((p1.X-p2.X)*(p1.X-p2.X)+(p1.Y-p2.Y)*(p1.Y-p2.Y));
return length;
}
private int Layer_Count(string name)//获取图层编号
{
int Count = 0;
axMapControl1.Map.ClearSelection();//清除地图的选择
int pLayerCount = mf.axMapControl1.LayerCount;
string str = "";
for (int i = 0; i < pLayerCount; i++)
{
str = axMapControl1.Map.get_Layer(i).Name;
if (str == name)
{
Count = i;
}
}
return Count;
}
private void Create_Point(IPoint pPoint)//在点要素集中,创建点要素
{
ILayer pLayer = axMapControl1.Map.get_Layer(Layer_Count("Points"));
IFeatureLayer pFeatureLyr = pLayer as IFeatureLayer;
IFeatureClass pFeatCls = pFeatureLyr.FeatureClass;
IDataset pDataset = pFeatCls as IDataset;
IWorkspace pWS = pDataset.Workspace;
IWorkspaceEdit pWorkspaceEdit = pWS as IWorkspaceEdit;
pWorkspaceEdit.StartEditing(false);
pWorkspaceEdit.StartEditOperation();
IFeatureBuffer pfeatureBuffer;
IFeatureCursor pFeatureCuror;
IFeature _pFeature;
pfeatureBuffer = pFeatCls.CreateFeatureBuffer();
pFeatureCuror = pFeatCls.Insert(true);
_pFeature = pfeatureBuffer as IFeature;
IGeometry pPointGeo = pPoint as IGeometry;
_pFeature.Shape = pPointGeo;
pFeatureCuror.InsertFeature(pfeatureBuffer);
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);
axMapControl1.ActiveView.Refresh();
}
private void Point_C()//获取所有线要素的交点
{
int count = 0;
IWorkspace pWS;
pWS = GetFGDBWorkspace();
IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;
IFeatureClass inputFClass = pFWS.OpenFeatureClass("Lines");
IFeatureCursor pFeatureCursor1 = inputFClass.Search(null, true);
IFeature pFeature1;
pFeature1 = pFeatureCursor1.NextFeature();
while (pFeature1 != null)
{
ITopologicalOperator pTopological = (pFeature1.Shape) as ITopologicalOperator;
IFeatureCursor pFeatureCursor2 = inputFClass.Search(null, true);
IFeature pFeature2;
pFeature2 = pFeatureCursor2.NextFeature();
for (int i = 0; i <= count; i++)
{
pFeature2 = pFeatureCursor2.NextFeature();
}
while (pFeature2 != null)
{
IGeometry pGeoInterscet = pTopological.Intersect(pFeature2.Shape, esriGeometryDimension.esriGeometry0Dimension);
if (!pGeoInterscet.IsEmpty)
{
IPointCollection pColl = pGeoInterscet as IPointCollection;
IPoint pPoint = pColl.get_Point(0);
Create_Point(pPoint);
}
pFeature2 = pFeatureCursor2.NextFeature();
}
count++;
pFeature1 = pFeatureCursor1.NextFeature();
}
}
private void SplitLine(IPoint pPoint,IFeature pFeature)
{
try
{
bool result = false;
IGeometry pGeometry = pFeature.Shape;
if (pGeometry.GeometryType == esriGeometryType.esriGeometryPolyline)
{
IPointCollection pPointCollection = pFeature.Shape as IPointCollection;
int count = pPointCollection.PointCount;
IPoint First_P = new PointClass();
IPoint End_P = new PointClass();
First_P = pPointCollection.get_Point(0);
End_P = pPointCollection.get_Point(count-1);
IRelationalOperator pRelationalOperator = pGeometry as IRelationalOperator;
result=pRelationalOperator.Contains(pPoint);//判断打断点是否在待打断线上
if (result == true)
{
if (Leng(First_P,pPoint)>1&&Leng(End_P,pPoint)>1)//Leng函数检测打断点是否在在待打断线的端点,若是端点则无法打断,点坐标误差<1
{
IFeatureEdit pFeatureEdit = pFeature as IFeatureEdit;
pFeatureEdit.Split(pPoint);
Bool = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void Get_P_L()
{
IWorkspace pWS;
pWS=GetGDBWorkspace();
IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;
IFeatureClass inputFClass = pFWS.OpenFeatureClass("Points");//Points打断点 要素数据集
IFeatureCursor pFeatureCursor = inputFClass.Search(null, true);
IFeature pFeature;
pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)//点循环
{
IGeometry pGeoPoint = pFeature.Shape;
IPoint pPoint = (IPoint)pGeoPoint;
for (int i = 0; i < 5; i++)//每个点可以作为5条路的交点,循环5次,次阈值可以自定,一般最少4次循环
{
Bool = false;
IFeatureClass iFClass = pFWS.OpenFeatureClass("Lines");//待打断线 要素数据集
IFeatureCursor pFCursor = iFClass.Search(null, true);
IFeature _pFeature;
_pFeature = pFCursor.NextFeature();
while (_pFeature != null)//线循环
{
SplitLine(pPoint, _pFeature);
if (Bool == true)
{
break;
}
_pFeature = pFCursor.NextFeature();
}
}
pFeature = pFeatureCursor.NextFeature();
}
}
原始资源:待打断的线要素数据集,打断点的要素数据集(打断点即是所有线相互的交点)
流程:点循环 中间嵌套一个线循环,每打断一个点就要终止一次线循环,从新扫描线数据集,每个点进行5次打断循环,保证这个点所在的线段全部被打断
打断所有线最重要的一点是:当一条线被打断时,线要素会增加1, 所以开始的 IFeature 获取的游标就不能代表新的数据集了,就要中断从新搜索所有线要素
执行时从
Point_C();开始启动
然后执行Get_P_L();