GDAL:Ogr读写FileGDB文件(C#)

这篇博客介绍了如何在C#中利用GDAL的OGR库进行FileGDB文件的读写操作,特别是在需要进行坐标转换场景下的应用。文章提到了FileGDBAPI.dll对于写操作的重要性,并且分享了一个代码样例,演示了如何将一个文件转换坐标后生成新的FileGDB文件。此外,还提及了Shapefile的读写处理方式,以及在处理MDB文件时遇到的挑战。
摘要由CSDN通过智能技术生成

首先需要FileGDBAPI.dll支持(ogr_FileGDB.dll只支持读,不支持写),存放处理可参见https://blog.csdn.net/xzhh19921019/article/details/53419065。上代码样例(用于将一个文件进行坐标转换生成新的文件。shapefile读写类同于fileGDB,差别在于只有一个layer,无需循环,这里略去具体代码。mdb还没有找到好的方法——不想用AE):

private void ConversionToGeodeticWithFourParametersOfLgo() {
            string errMsg = "";
            foreach (string strFileName in lstFileName) {
                //try {
                DataSource originDataSource = Ogr.Open(strFileName, 0);//0 is for reading,1 is for update
                if (originDataSource == null) {
                    errMsg += "不能打开:" + strFileName + "\r\n";
                    continue;
                }
                Layer layer0 = originDataSource.GetLayerByIndex(0);//if it is a shapefile ,then it only has 1 layer.
                if (layer0 == null) {
                    errMsg += "获取文件:" + strFileName + "的第0个图层失败!" + "\r\n";
                    continue;
                }
                SpatialReference originSpatialReference = layer0.GetSpatialRef();
                string strTemp = originSpatialReference.GetAttrValue("SPHEROID", 0);
                if (strTemp == null) {
                    errMsg += strFileName + ":请注意,该文件无空间参考系,请选用参考系设置”工具将界面设定的“源坐标系”参数写入文件!" + "\r\n";
                    continue;
                }
                SpatialReference targetSpatialReference = originSpatialReference.CloneGeogCS();
                string suffix = Path.GetExtension(strFileName);
                string targetFileName;
                //originDataSource.Dispose();
                if (originSpatialReference.IsProjected() == 1) {//projected to geodetic
                    originSpatialReference.Dispose();
                    double e1Square = 2 * 1 / targetEllipsoid.fReciprocal - 1 / targetEllipsoid.fReciprocal * 1 / targetEllipsoid.fReciprocal;// first eccentricity square
                    double b = targetEllipsoid.a * (1 - 1 / targetEllipsoid.fReciprocal);
                    double e2Square = Math.Pow(targetEllipsoid.a / b, 2) - 1;// second eccentricity square
                    double[] gaussCoefficient = LidfSurveyFunctions.GaussCoefficient(1 / targetEllipsoid.fReciprocal);
                    if (suffix.Equals(".shp")) {                        
                       //略去
                    } else if (suffix.Equals(".gdb")) {
                        targetFileName = Path.Combine(targetDirectory, Path.GetFileName(strFileName));
                        if (Directory.Exists(targetFileName)) {
                            FileHelper.DeleteFolder(targetFileName);
                        }
                        if (!Directory.Exists(targetDirectory)) {
                            Directory.CreateDirectory(targetDirectory);
                        }
                        OSGeo.OGR.Driver driver = Ogr.GetDriverByName("FileGDB");
                        if (driver == null) {
                            errMsg += "不能获取:" + strFileName + "的驱动器,请检查文件!" + "\r\n";
                            continue;
                        }
                        DataSource targetDataSource = driver.CreateDataSource(targetFileName, null);
                        int la
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值