参考:http://bbs.esrichina-bj.cn/ESRI/viewthread.php?tid=46367
调用的文本文件数据:
程序的实现:
第一步:全局变量!
/// <summary> /// 要保存的ShapeFile全路径名称 /// </summary> string shapeFileFullName = string.Empty; /// <summary> /// 测量数据全路径名称 /// </summary> string surveyDataFullName = string.Empty; List<string> pColumns = new List<string>(); List<CPoint> pCPointList = new List<CPoint>(); struct CPoint { public double x; public double y; public string name; }
第二步:打开按钮的事件!
private void OpenBtn_Click(object sender, EventArgs e) { OpenFileDialog pOFD = new OpenFileDialog(); pOFD.Multiselect = false; pOFD.Title = "打开本地测量坐标文件"; pOFD.InitialDirectory = System.IO.Directory.GetCurrentDirectory(); pOFD.Filter = "测量坐标文件(*.TXT)|*.TXT"; if (pOFD.ShowDialog() == DialogResult.OK) { surveyDataFullName = pOFD.FileName; this.textBox1.Text = surveyDataFullName; } }
第三步:保存按钮事件!
private void SaveBtn_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Shape文件(*.shp) |*.shp"; DialogResult dialogResult = saveFileDialog.ShowDialog(); if (dialogResult == DialogResult.OK) { shapeFileFullName = saveFileDialog.FileName; } else { shapeFileFullName = null; return; } this.textBox2.Text = shapeFileFullName; }
第四步:实现的函数一:获取文本文件中的点!
/// <summary> /// 获取点数据 /// </summary> /// <param name="surveyDataFullName">测量数据全路径名称</param> /// <returns></returns> private List<CPoint> GetAllPoint(string surveyDataFullName) { try { List<CPoint> pList = new List<CPoint>(); if (surveyDataFullName == null || surveyDataFullName == string.Empty) { MessageBox.Show("选择野外测量数据"); return null; } if (!System.IO.File.Exists(surveyDataFullName)) { MessageBox.Show("野外测量数据不存在!"); return null; } string strLine; //获取一行的字符串 char[] charArray = new char[] { ',' }; //分隔符 string[] strArray; //获取被分割的字符串数组 System.IO.FileStream aFile = new System.IO.FileStream(surveyDataFullName, FileMo StreamReader sr = new StreamReader(aFile, Encoding.Default); strLine = sr.ReadLine(); //获取第一行的字符串 strArray = strLine.Split(charArray); //将字符串分割 if (strArray.Length > 0) { for (int x = 0; x < strArray.Length; x++) { pColumns.Add(strArray[x]); //将字符串数组中的内容复制给 列List } } else { return null; } strLine = sr.ReadLine(); //继续读第二行字符串 while (strLine != null) { strArray = strLine.Split(charArray); //赋值到字符串数组中 CPoint pCPoint = new CPoint(); //定义 点 pCPoint.x = Convert.ToDouble(strArray[0]); //获取点的横坐标 pCPoint.y = Convert.ToDouble(strArray[1]); //获取点的纵坐标 pCPoint.name = strArray[2].Trim(); //获取点的名称 pList.Add(pCPoint); //将点加到 点List 中 strLine = sr.ReadLine(); //继续读下一行 } sr.Close(); return pList; //将 点List 返回了 } catch (Exception Err) { MessageBox.Show(Err.Message); return null; } }
第五步:实现的函数二:创建 shp 文件!
/// <summary> /// 创建Shp文件 /// </summary> /// <param name="saveFile"></param> /// <returns></returns> private IFeatureLayer CreateShpFromPoints(string outfileNamePath) { int index = outfileNamePath.LastIndexOf('\\'); string folder = outfileNamePath.Substring(0, index); //获取shp文件夹 shapeFileFullName = outfileNamePath.Substring(index + 1); //获取shp文件名 IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFWS = pWSF.OpenFromFile(folder, 0) as IFeatureWorkspace; //如果shapefile存在替换它 if (File.Exists(outfileNamePath)) { IFeatureClass featureClass = pFWS.OpenFeatureClass(shapeFileFullName); IDataset pDataset = (IDataset)featureClass; pDataset.Delete(); //将里面数据删除 } IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit; pFieldsEdit = (IFieldsEdit)pFields; IField pField = new FieldClass(); IFieldEdit pFieldEdit = (IFieldEdit)pField; pFieldEdit.Name_2 = "Shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGDefEdit = (IGeometryDefEdit)pGeometryDef; pGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField); pField = new FieldClass(); pFieldEdit = (IFieldEdit)pField; pFieldEdit.Length_2 = 20; pFieldEdit.Name_2 = pColumns[2]; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; pFieldsEdit.AddField(pField); IFeatureClass pFeatureClass; pFeatureClass = pFWS.CreateFeatureClass(shapeFileFullName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", ""); List<string> pBuildingList = new List<string>(); for (int i = 0; i < pCPointList.Count; i++) { if (pBuildingList.Contains(pCPointList[i].name.Trim()) == false) { pBuildingList.Add(pCPointList[i].name.Trim()); //将不一样的名字加了进来 } } for (int i = 0; i < pBuildingList.Count; i++) //遍历不同的名字 { IPointCollection pPointColl = new PolygonClass(); object o = Type.Missing; for (int j = 0; j < pCPointList.Count; j++) { if (pCPointList[j].name.Trim() == pBuildingList[i].Trim()) { IPoint pPoint = new PointClass(); pPoint.X = pCPointList[j].x; pPoint.Y = pCPointList[j].y; pPointColl.AddPoint(pPoint, ref o, ref o); //相同的名字添加到同一个多边形中 } } if (pPointColl.PointCount > 0) { IClone pClone = pPointColl.get_Point(0) as IClone; IPoint pEndPoint = pClone.Clone() as IPoint; //将第一个点拷贝到最后一个点 pPointColl.AddPoint(pEndPoint, ref o, ref o); } IFeature pFeature = pFeatureClass.CreateFeature(); pFeature.Shape = pPointColl as IPolygon; pFeature.Store(); pFeature.set_Value(pFeature.Fields.FindField(pColumns[2]), pBuildingList[i].Trim()); pFeature.Store(); } IFeatureLayer pFeaturelayer = new FeatureLayerClass(); pFeaturelayer.FeatureClass = pFeatureClass; return pFeaturelayer; }
第六步:创建按钮事件!
private void CreateBtn_Click_1(object sender, EventArgs e) { pCPointList = this.GetAllPoint(surveyDataFullName); //调用函数 IFeatureLayer pFeatureLayer = CreateShpFromPoints(shapeFileFullName); //调用函数 pFeatureLayer.Name = "建筑物"; this.axMapControl1.Map.AddLayer(pFeatureLayer); }