随笔 - 54  文章 - 0  评论 - 5  阅读 - 13万

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();
posted on   gisery  阅读(460)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示