首页 > 代码库 > [GDAL]写入shp

[GDAL]写入shp

 C#通过Wkt码构建shp,记录写不进去!

技术分享
 1  static void WriteVectorFile() 2         { 3             string strVectorFile = "E:\\"; 4             // 注册所有的驱动 5             Ogr.RegisterAll(); 6             //创建数据,这里以创建ESRI的shp文件为例 7             string strDriverName = "ESRI Shapefile"; 8             Driver oDriver = Ogr.GetDriverByName(strDriverName); 9             if (oDriver == null)10             {11                 Console.WriteLine("%s 驱动不可用!\n", strVectorFile);12                 return;13             }14             // 创建数据源15             DataSource oDS = oDriver.CreateDataSource(strVectorFile, null);16             if (oDS == null)17             {18                 Console.WriteLine("创建矢量文件【%s】失败!\n", strVectorFile);19                 return;20             }21 22             // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定23             Layer oLayer = oDS.CreateLayer("TestPolygon", null, wkbGeometryType.wkbPolygon, null);24             if (oLayer == null)25             {26                 Console.WriteLine("图层创建失败!\n");27                 return;28             }29 30             // 下面创建属性表31             // 先创建一个叫FieldID的整型属性32             FieldDefn oFieldID = new FieldDefn("FieldID", FieldType.OFTInteger);33             oLayer.CreateField(oFieldID, 1);34 35             // 再创建一个叫FeatureName的字符型属性,字符长度为5036             FieldDefn oFieldName = new FieldDefn("FieldName", FieldType.OFTString);37             oFieldName.SetWidth(100);38             oLayer.CreateField(oFieldName, 1);39             FeatureDefn oDefn = oLayer.GetLayerDefn();40 41             // 创建三角形要素42             Feature oFeatureTriangle = new Feature(oDefn);43             oFeatureTriangle.SetField(0, 0);44             oFeatureTriangle.SetField(1, "三角形");45             Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))");46             oFeatureTriangle.SetGeometry(geomTriangle);47             oLayer.CreateFeature(oFeatureTriangle);48 49             // 创建矩形要素50             Feature oFeatureRectangle = new Feature(oDefn);51             oFeatureRectangle.SetField(0, 1);52             oFeatureRectangle.SetField(1, "矩形");53             Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))");54             oFeatureRectangle.SetGeometry(geomRectangle);55             oLayer.CreateFeature(oFeatureRectangle);56 57             // 创建五角形要素58             Feature oFeaturePentagon = new Feature(oDefn);59             oFeaturePentagon.SetField(0, 2);60             oFeaturePentagon.SetField(1, "五角形");61             Geometry geomPentagon = Geometry.CreateFromWkt("POLYGON ((70 0,85 0,90 15,80 30,65 15,70 0))");62             oFeaturePentagon.SetGeometry(geomPentagon);63             oLayer.CreateFeature(oFeaturePentagon);64             Console.WriteLine("\n数据集创建完成!\n");65         }
View Code

通过对象构建

 

技术分享

 计划将这一套结构改成C#的。

[GDAL]写入shp