GDAL 生成shp文件
附件:http://pan.baidu.com/s/1i3GPwrV(C#版GDAL接口.dll)
示例程序: http://pan.baidu.com/s/1jpIKQ (程序是在vs2008 x86下编写的,其中WKT数据是从数据库中获取的,程序中的界面没有任何的作用,直接点击执行即可)
1、环境配置
添加引用和头文件就可以使用了。
2、代码示例,c#调用gdal生产shp文件。
static void WriteVectorFileShp(String strVectorFile, String wkt, double area) //创建算法生产的边界矢量图 { // 为了支持中文路径,请添加下面这句代码 OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO"); // 为了使属性表字段支持中文,请添加下面这句 OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 注册所有的驱动 Ogr.RegisterAll(); //创建数据,创建ESRI的shp文件 string strDriverName = "ESRI Shapefile"; Driver oDriver = Ogr.GetDriverByName(strDriverName); if (oDriver == null) { Debug.WriteLine("%s 驱动不可用!\n", strVectorFile); return; } // 步骤1、创建数据源 DataSource oDS = oDriver.CreateDataSource(strVectorFile, null); if (oDS == null) { Debug.WriteLine("创建矢量文件【%s】失败!", strVectorFile); return; } //步骤2、创建空间坐标系 OSGeo.OSR.SpatialReference oSRS = new OSGeo.OSR.SpatialReference(""); oSRS.SetWellKnownGeogCS("WGS84"); //步骤3、创建图层,并添加坐标系,创建一个多边形图层(wkbGeometryType.wkbUnknown,存放任意几何特征) Layer oLayer = oDS.CreateLayer("TestPolygon", oSRS, wkbGeometryType.wkbUnknown, null); if (oLayer == null) { Debug.WriteLine("图层创建失败!"); return; } // 步骤4、下面创建属性表 FieldDefn oFieldPlotArea = new FieldDefn("PlotArea", FieldType.OFTString); // 先创建一个叫PlotArea的属性 oFieldPlotArea.SetWidth(100); // 步骤5、将创建的属性表添加到图层中 oLayer.CreateField(oFieldPlotArea, 1); //步骤6、定义一个特征要素oFeature(特征要素包含两个方面1.属性特征2.几何特征) FeatureDefn oDefn = oLayer.GetLayerDefn(); Feature oFeature = new Feature(oDefn); //建立了一个特征要素并将指向图层oLayer的属性表 //步骤7、设置属性特征的值 oFeature.SetField(0, area.ToString()); OSGeo.OGR.Geometry geomTriangle = OSGeo.OGR.Geometry.CreateFromWkt(wkt);//创建一个几何特征 //步骤8、设置几何特征 oFeature.SetGeometry(geomTriangle); //步骤9、将特征要素添加到图层中 oLayer.CreateFeature(oFeature); //Debug.WriteLine("数据集创建完成!"); }
WKT
POLYGON ((116.40773261182795 36.555299480545493, 116.40773261146948 36.555299489139742, 116.40773027146895 36.555377819139721, 116.40773027158551 36.55537784423705, 116.40773044158547 36.555382184237054, 116.40773044243686 36.555382198655138, 116.40773727111547 36.555468681927707, 116.40773694133803 36.555493495167283, 116.40773694180282 36.555493519006163, 116.40774177111638 36.555585835896657, 116.40774077137661 36.555636162777148, 116.40774077130489 36.55563617, 116.40774077130449 36.555688669999995, 116.40774077155113 36.555688683391054, 116.40774411155046 36.555779343391045, 116.40774412501702 36.555779429252262, 116.40774415857561 36.55577951145429, 116.40774421098901 36.555779586966615, 116.40774428032486 36.555779653005267, 116.40774436402701 36.555779707135606, 116.40774445900952 36.555779747361996, 116.4077445617707 36.55577977220139, 116.40774466852197 36.555779780738021, 116.40774477532773 36.555779772657189, 116.40774487825036 36.555779748256789, 116.40774497349534 36.555779708436411, 116.40774505755128 36.555779654664107, 116.4077451273193 36.555779588922341, 116.40774518022721 36.555779513634818, 116.40774521432441 36.555779431577179, 116.40774522835389 36.55577934577466, 116.40774705835356 36.555737185774653, 116.40774705826921 36.555737152388872, 116.40774272871791 36.555647821660123, 116.4077428886787 36.555585836690454, 116.40774438848156 36.555542012440306, 116.40774438826199 36.555541982269375, 116.40773972876654 36.555446492622629, 116.40773988866363 36.555427005140359, 116.40774088862004 36.555377177296748, 116.40774088785585 36.555377145330816, 116.40773456007933 36.55528400806763, 116.40773738640789 36.555258870719484, 116.40773738868464 36.555258827631221, 116.4077370586843 36.555208167631221, 116.40773705863404 36.555208163506975, 116.40773655863383 36.555180163506982, 116.40773654654753 36.555180076531009, 116.40773651385493 36.555179993079058, 116.40773646178864 36.555179916297483, 116.4077363923117 36.555179849081163, 116.4077363080436 36.555179793964335, 116.40773621216144 36.555179753025037, 116.40773610828025 36.5551797278068, 116.40773600031658 36.5551797192604, 116.40773589234097 36.555179727708072, 116.40773578842439 36.555179752831322, 116.40773569248478 36.555179793682917, 116.4077356081393 36.555179848722673, 116.40773553856799 36.555179915875406, 116.4077354863939 36.555179992609325, 116.40773545358411 36.555180076031334, 116.40773544137561 36.555180162996209, 116.40773344149484 36.555283986768309, 116.40773261182795 36.555299480545493))