一、GDAL类库下载:
下载地址:GISInternals Support Site
比如我下载的是release-1930-x64-gdal-3-4-2-mapserver-7-6-4
点击release-1930-x64-gdal-3-4-2-mapserver-7-6-4的链接,下载这两个文件
二、解压文件和安装gdb插件
1.下载好的两个文件,解压其中zip文件,
解压后如图
2.将filegdb.msi文件安装,安装后找到对应文件夹,如图
3.将gdadlplugins文件夹和FileGDBAPI.dll文件拷贝到release-1930-x64-gdal-3-4-2-mapserver-7-6-4文件夹下的bin文件夹下;
4.将D:\release-1930-x64-gdal-3-4-2-mapserver-7-6-4\bin\gdal\csharp下的8个dll拷贝到release-1930-x64-gdal-3-4-2-mapserver-7-6-4文件夹下的bin文件夹下;
5.用vs2019创建windows桌面程序(netframework为4.7.2),一定要注意以x64程序运行;
6.将release-1930-x64-gdal-3-4-2-mapserver-7-6-4文件夹下的bin文件夹内容(除exe)拷贝到debug下;
7.添加引用 包含四个dll gdal_csharp.dll gdalconst_csharp.dll ogr_csharp osr_csharp ,路径为release-1930-x64-gdal-3-4-2-mapserver-7-6-4\bin
8.初始化代码以及读写gdb文件方法
-
Gdal.AllRegister();
-
Ogr.RegisterAll();
-
Gdal.SetConfigOption(
"GDAL_FILENAME_IS_UTF8",
"YES");
-
// 为了使属性表字段支持中文,请添加下面这句
-
Gdal.SetConfigOption(
"SHAPE_ENCODING",
"");
//避免乱码
-
Gdal.SetConfigOption(
"FILEGDB_ENCODING",
"UTF-8");
-
Utils
utils
=
new
Utils();
-
string
gdbFile
=
"D:\\test.gdb";
-
-
-
DataSource
sourceDs
= utils.ReadVectorFile(gdbFile);
-
//创建目标数据
-
String
outFileDir
=
"D:\\OUT";
-
DataSource
targetDs
= utils.WriteVectorFile(sourceDs, outFileDir +
"\\outData.gdb");
-
public DataSource
WriteVectorFile
(DataSource dataSource, string strVectorFile)
-
{
-
-
string
strDriverName
=
"FileGDB";
-
// 创建一个文件,根据strDriverName扩展名自动判断驱动类型
-
OSGeo.OGR.
Driver
oDriver
= Ogr.GetDriverByName(strDriverName);
-
if (oDriver ==
null)
-
{
-
Console.WriteLine(strVectorFile +
" 驱动不可用!\n");
-
return
null;
-
}
-
DataSource
oDS
= oDriver.CreateDataSource(strVectorFile,
null);
-
if (oDS ==
null)
-
{
-
Console.WriteLine(
"创建文件【" + strVectorFile +
"】失败!\n");
-
return
null;
-
}
-
-
return oDS;
-
}
-
-
-
public DataSource
ReadVectorFile
(string strVectorFile)
-
{
-
-
string
strDriverName
=
"OpenFileGDB";
-
// 创建一个文件,根据strDriverName扩展名自动判断驱动类型
-
OSGeo.OGR.
Driver
oDriver
= Ogr.GetDriverByName(strDriverName);
-
if (oDriver ==
null)
-
{
-
Console.WriteLine(strVectorFile +
" 驱动不可用!\n");
-
return
null;
-
}
-
DataSource
oDS
= oDriver.Open(strVectorFile,
0);
-
if (oDS ==
null)
-
{
-
Console.WriteLine(
"读取gdb文件【" + strVectorFile +
"】失败!\n");
-
return
null;
-
}
-
return oDS;
-
}
9.环境变量设置:
GDAL_HOME D:\release-1930-x64-gdal-3-4-2-mapserver-7-6-4
GDAL_DRIVER_PATH D:\release-1930-x64-gdal-3-4-2-mapserver-7-6-4\bin\gdalplugins
GDAL_DATA D:\release-1930-x64-gdal-3-4-2-mapserver-7-6-4\bin\gdal-data
path: %GDAL_HOME%\bin %GDAL_HOME%\bin\gdal\java %PROJ_LIB% %GDAL_DATA%
转载:https://blog.csdn.net/xlp789/article/details/128363235