在C#中使用GDAL创建Shape文件

这几天在项目中考虑使用GDAL,由于10年没有用过VC了,就在网上搜了下怎么样在C# 中使用GDAL,看到了http://blog.csdn.net/liminlu0314/article/details/8828940这边文章。拿过来测试了一下,可以用,把自己的测试代码贴上来,以便日后参考。

         // 为了支持中文路径,请添加下面这句代码
            OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
            // 为了使属性表字段支持中文,请添加下面这句
            OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");

            string strVectorFile = "E:\\TestPolygon.shp";

            // 注册所有的驱动
            Ogr.RegisterAll();

            //创建数据,这里以创建ESRI的shp文件为例
            string strDriverName = "ESRI Shapefile";
            int count = Ogr.GetDriverCount();
            Driver oDriver = Ogr.GetDriverByName(strDriverName);
            if (oDriver == null)
            {
                Console.WriteLine("%s 驱动不可用!\n", strVectorFile);
                return;
            }

            // 创建数据源
            DataSource oDS = oDriver.CreateDataSource(strVectorFile, null);
            if (oDS == null)
            {
                Console.WriteLine("创建矢量文件【%s】失败!\n", strVectorFile);
                return;
            }

            // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定
            Layer oLayer = oDS.CreateLayer("TestPolygon", null, wkbGeometryType.wkbPolygon, null);
            if (oLayer == null)
            {
                Console.WriteLine("图层创建失败!\n");
                return;
            }

            // 下面创建属性表
            // 先创建一个叫FieldID的整型属性
            FieldDefn oFieldID = new FieldDefn("FieldID", FieldType.OFTInteger);
            oLayer.CreateField(oFieldID, 1);

            // 再创建一个叫FeatureName的字符型属性,字符长度为50
            FieldDefn oFieldName = new FieldDefn("FieldName", FieldType.OFTString);
            oFieldName.SetWidth(100);
            oLayer.CreateField(oFieldName, 1);

            FeatureDefn oDefn = oLayer.GetLayerDefn();

            // 创建三角形要素
            Feature oFeatureTriangle = new Feature(oDefn);
            oFeatureTriangle.SetField(0, 0);
            oFeatureTriangle.SetField(1, "三角形");
            Geometry geomTriangle = Geometry.CreateFromWkt("POLYGON ((0 0,20 0,10 15,0 0))");
            oFeatureTriangle.SetGeometry(geomTriangle);

            oLayer.CreateFeature(oFeatureTriangle);

            // 创建矩形要素
            Feature oFeatureRectangle = new Feature(oDefn);
            oFeatureRectangle.SetField(0, 1);
            oFeatureRectangle.SetField(1, "矩形");
            Geometry geomRectangle = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))");
            oFeatureRectangle.SetGeometry(geomRectangle);

            oLayer.CreateFeature(oFeatureRectangle);

            // 创建岛要素
            Feature oFeatureHole= new Feature(oDefn);
            oFeatureHole.SetField(0, 1);
            oFeatureHole.SetField(1, "环岛测试");
            //Geometry geomWYX = Geometry.CreateFromWkt("POLYGON ((30 0,60 0,60 30,30 30,30 0))"); 
            OSGeo.OGR.Geometry outGeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            outGeo.AddPoint(40, -30, 0);
            outGeo.AddPoint(60, -30, 0);
            outGeo.AddPoint(60, -10, 0);
            outGeo.AddPoint(40, -10, 0);

            OSGeo.OGR.Geometry inGeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            inGeo.AddPoint(45, -25, 0);
            inGeo.AddPoint(55, -25, 0);
            inGeo.AddPoint(55, -15, 0);
            inGeo.AddPoint(45, -15, 0);

            OSGeo.OGR.Geometry geo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
            geo.AddGeometryDirectly(outGeo);
            geo.AddGeometryDirectly(inGeo);
            oFeatureHole.SetGeometry(geo);
            oLayer.CreateFeature(oFeatureHole);

            // 创建Multi要素
            Feature oFeatureMulty = new Feature(oDefn);
            oFeatureMulty.SetField(0, 1);
            oFeatureMulty.SetField(1, "MultyPart测试");
            OSGeo.OGR.Geometry geo1 = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            geo1.AddPoint(25, -10, 0);
            geo1.AddPoint(5, -10, 0);
            geo1.AddPoint(5, -30, 0);
            geo1.AddPoint(25, -30, 0);
            OSGeo.OGR.Geometry poly1 = new Geometry(wkbGeometryType.wkbPolygon);
            poly1.AddGeometryDirectly(geo1);

            OSGeo.OGR.Geometry geo2 = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
            geo2.AddPoint(0, -15, 0);
            geo2.AddPoint(-5, -15, 0);
            geo2.AddPoint(-5, -20, 0);
            geo2.AddPoint(0, -20, 0);

            OSGeo.OGR.Geometry poly2 = new Geometry(wkbGeometryType.wkbPolygon);
            poly2.AddGeometryDirectly(geo2);

            OSGeo.OGR.Geometry geoMulty = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbMultiPolygon);
            geoMulty.AddGeometryDirectly(poly1);
            geoMulty.AddGeometryDirectly(poly2);
            oFeatureMulty.SetGeometry(geoMulty);

            oLayer.CreateFeature(oFeatureMulty);
            
          );

          
            Console.WriteLine("\n数据集创建完成!\n");
        }

  

  特别说明:

  在GDAL中,MultiPolygon由多个Polygon组成;而Polygon由LinearRing组成,像环要素就是有一个外部LinearRing和若干个内部LinearRing组成的。

转载于:https://www.cnblogs.com/ITGIS/p/3542601.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值