由坐标数据生成点SHP文件,并由多组点生成线SHP文件(下)


由坐标数据生成点SHP文件,并由多组点生成线SHP文件(下)

上一章说完了如何生成点shp文件,这里继续讲如何生成线shp文件


5.获取DataTable中的坐标数据,根据要求获取点对
这里用到上一章步骤3里存储点和名称的键值对,取出组成线的点对,来生成线shp文件。

string name1 = String.Empty;
string name2 = String.Empty;
for (int j = 1; j < mDataTable.Rows.Count; j++)
{
    name1=String.Empty;
    name2 = mDataTable.Rows[j][2].ToString();
    if (mDataTable.Rows[j][1].ToString() != "")
    {
        name1 = mDataTable.Rows[j][1].ToString();
        temp = name1;
        IPoint pPointExsist = pdic[name1];
        pCol.Add(pPointExsist.ID);
    }
    else if (mDataTable.Rows[j][1].ToString() == "")
    {
        name1 = temp;
    }
    if (name2 != "")
    {
        IPoint pPoint2 = pdic[name2];
        if (Tool.LineIsExist(pPoint2, pCol))
        {
            continue;
        }
        IPoint pPoint1 = pdic[name1];
        double ppLength = Tool.GetLength(pPoint1, pPoint2);
        string ppName = name1 + name2;
        Tool.AddLine(pLyrLine, pPoint1, pPoint2, ppName, ppLength, axMapControl1);
    }
}

这里Tool.LineIsExist()方法是用于判断线是否已经存在,例如当J1J2线出现过,J2J1就是重复多余的线了。Tool.LineIsExist()代码如下:

 public static bool LineIsExist(IPoint pPoint, List<int> pCollection)
 {
     int id = pPoint.ID;
     for (int i = 0; i < pCollection.Count; i++)
     {
         if (id == pCollection[i])
             return true;

     }
     return false;
 }

6.创建空的线shp文件,设置属性信息,加载空的线layer
这一步与上一章步骤3类似,此处省略。

7.编辑线layer的属性信息,添加线要素
添加线要素跟添加点要素略有不同,此处线要素是要根据点要素生成的,所以由两点生成线的代码为:

ILine pLine=new LineClass();
pLine.PutCoords(pPt1,pPt2);//pPt1,pPt2分别为线的两端点
ISegment pSeg=pLine as  ISegmet;
ISegmentCollection pPath=new PathClass();
pPath.AddSegment(pSeg,Type.Missing,Type.Missing);
IGeometryCollection polyLine=new PolyLineClass();
polyLine.AddGeometry(pPath as IGeometry,Type.Missing,Type.Missing);

接下来,给属性赋值,并刷新要素。

IFeatureLayer pFeatureLyr=pLayer as IFeatureLayer;//pLayer是创建空的线shp文件生成的Ilayer
IFeatureClass pFeatureCls=pFeatureLyr.FeatureClass;
IFeatureClassWrite pFeatureClsWrite=pFeatureCls as IFeatureClassEdit;
IWorkspaceEdit  pWorkspaceEdit=(pFeatureClsEdit as IDataset).Workspace as IWorkspaceEdit;
pWorkspaceEdit.StartEditing(true);
pWorkspaceEdit.StartEditOperation();

IFeature pFeature=pFeatureCls.CreateFeature();
IFields pFields=pFeature.Fields;
pFeature.set_Value(pFields.FindField("管线号"),name);
pFeature.set_Value(pFields.FindField("长度"),length);
pFeature.Shape=polyLine as IPolyLine;
pFeatureClsWrite.WriteFeature(pFeature);
pWorkspaceEdit.StopEditOperation();
pWorkspaceEdit.StopEditing(true);

axMapControl.ActiveView.Refresh();

转载自:https://blog.csdn.net/u014380270/article/details/54985468

You may also like...