Excel坐标点转线

  1. IWorkspaceFactory pShpWksFact = new ShapefileWorkspaceFactory();
  2. IFeatureWorkspace pFeatWks;
  3. pFeatWks = (IFeatureWorkspace)pShpWksFact.OpenFromFile(filePath, 0);
  4. const string strShapeFieldName = "Shape";
  5. //定义属性字段
  6. IFields pFields = new Fields();
  7. IFieldsEdit pFieldsEdit;
  8. pFieldsEdit = pFields as IFieldsEdit;
  9. IField pField = new Field();
  10. IFieldEdit pFieldEdit = new Field() as IFieldEdit;
  11. pFieldEdit.Name_2 = strShapeFieldName;
  12. pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
  13. pField = pFieldEdit as IField;
  14. //定义几何属性
  15. IGeometryDef pGeomDef = new GeometryDef();
  16. IGeometryDefEdit pGeomDefEdit = new GeometryDef() as IGeometryDefEdit;
  17. pGeomDefEdit = pGeomDef as IGeometryDefEdit;
  18. switch (shpTypeComboBox.Text)
  19. {
  20.     case "Point":
  21.         pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
  22.         break;
  23.     case "Polyline":
  24.         pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
  25.         break;
  26.     case "Polygon":
  27.         pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
  28.         break;
  29. }
  30.  
  31. pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
  32. pGeomDefEdit.SpatialReference_2 = new UnknownCoordinateSystem() as ISpatialReference;
  33. pFieldEdit.GeometryDef_2 = pGeomDef;
  34. pFieldsEdit.AddField(pField);
  35. pFields = pFieldsEdit as IFields;
  36. IFeatureClass pFeatureClass;
  37. pFeatureClass = pFeatWks.CreateFeatureClass(fileName, pFields, null, null,
  38.     esriFeatureType.esriFTSimple, strShapeFieldName, "");
  39. //添加属性字段
  40. for (int i = 0; i < addFieldListBox.Items.Count; i++)
  41. {
  42.     IField pfield = new Field();
  43.     IFieldEdit pfieldEdit = new Field() as IFieldEdit;
  44.     pfieldEdit.Name_2 = addFieldListBox.Items[i].ToString();
  45.     pfieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
  46.     pfield = pfieldEdit as IField;
  47.     pFeatureClass.AddField(pfield);
  48. }
  49. //绘制线
  50. IFeatureClassWrite fr = pFeatureClass as IFeatureClassWrite;
  51. IWorkspaceEdit w = (pFeatureClass as IDataset).Workspace as IWorkspaceEdit;
  52. IFeature f;
  53. //可选参数的设置
  54. object Missing = Type.Missing;
  55. IPoint p = new PointClass();
  56. w.StartEditing(true);
  57. w.StartEditOperation();
  58. //定义一个多义线对象
  59. IPolyline PlyLine = new PolylineClass();
  60. //定义一个点的集合
  61. IPointCollection ptclo = PlyLine as IPointCollection;
  62. ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
  63. ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
  64. for (int i = 0; i < excelDataGridViewX.Rows.Count - 1; i++)
  65. {
  66.     f = pFeatureClass.CreateFeature();
  67.  
  68.     DataGridViewRow dataRow = excelDataGridViewX.Rows[i];
  69.     double pointX, pointY;
  70.     pointX = double.Parse(dataRow.Cells[xComboBoxEx.Text].Value.ToString());
  71.     pointY = double.Parse(dataRow.Cells[yComboBoxEx.Text].Value.ToString());
  72.     p.PutCoords(pointX,pointY);
  73.     ptclo.AddPoint(p, ref Missing, ref Missing);
  74.     for (int j = 0; j < addFieldListBox.Items.Count; j++)
  75.     {
  76.         string fieldName = addFieldListBox.Items[j].ToString();
  77.         f.set_Value(f.Fields.FindField(fieldName),
  78.             dataRow.Cells[fieldName].Value.ToString());
  79.     }
  80.     IPolyline ppolyline = new PolylineClass();
  81.     ppolyline = (IPolyline)ptclo;
  82.     ppolyline.SpatialReference = spatialReference;
  83.     f.Shape = ppolyline;
  84.     fr.WriteFeature(f);
  85. }
  86.  
  87. //f.Store();
  88.  
  89. w.StopEditOperation();
  90. w.StopEditing(true);
  91. //添加新建的数据至Map中
  92. axMapControl.AddShapeFile(filePath, fileName);
  93. //this.Hide();
原文地址:https://www.cnblogs.com/wangyawei/p/9006067.html