说明:
GdalConfiguration.ConfigureGdal() 是 GDAL 的 C# 绑定中的一个方法,它用于配置 GDAL 的运行环境。
在使用 GDAL 之前,需要先对 GDAL 进行初始化。这可以通过调用 Gdal.AllRegister() 方法实现。但是,如果需要使用 GDAL 的一些高级功能,则需要调用 GdalConfiguration.ConfigureGdal() 方法进行进一步配置。
调用 GdalConfiguration.ConfigureGdal() 的示例代码:
GdalConfiguration.ConfigureGdal();
GdalConfiguration.ConfigureOgr();
GdalConfiguration.ConfigureOgr2();
注意:在使用 GDAL 之前,需要先将 GDAL 的 C# 包添加到项目中。这可以通过使用 NuGet 包管理器或手动安装 GDAL 的 C# 绑定来完成。
GDAL的代码示例:
using OSGeo.GDAL;
//using OSGeo.OSR;
using System;
namespace GDAL_test
{
class Program
{
static void Main(string[] args)
{
GdalConfiguration.ConfigureGdal();
//GdalConfiguration.ConfigureOgr();
// 注册所有的驱动
Gdal.AllRegister();
// 打开影像文件
string fileName = "H:/DEM/DEM12.5.tif";
Dataset dataset = Gdal.Open(fileName, Access.GA_ReadOnly);
// 获取影像信息
int width = dataset.RasterXSize;
int height = dataset.RasterYSize;
int bandCount = dataset.RasterCount;
// 获取投影参考系
//SpatialReference srs = new SpatialReference(dataset.GetProjectionRef());
//string wkt;
//string[] options = { "MULTILINE=YES", "PRETTY_PRINT=YES" };
//int projection = srs.ExportToWkt(out wkt, options);
string projection = dataset.GetProjectionRef();
// 获取投影坐标范围
double[] geoTransform = new double[6];
dataset.GetGeoTransform(geoTransform);
double minX = geoTransform[0];
double minY = geoTransform[3] + width * geoTransform[4] + height * geoTransform[5];
double maxX = geoTransform[0] + width * geoTransform[1] + height * geoTransform[2];
double maxY = geoTransform[3];
// 读取数据
Band band = dataset.GetRasterBand(1);
int[] buffer = new int[width * height];
band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0);
Console.WriteLine("Projection Information:\n" + projection);
Console.WriteLine("X-axis minimum:" + minX);
Console.WriteLine("Y-axis minimum:" + minY);
Console.WriteLine("X-axis maximum:" + maxX);
Console.WriteLine("Y-axis maximum:" + maxY);
Console.WriteLine("Columns:" + width);
Console.WriteLine("Rows:" + height);
Console.ReadLine();
// 使用完毕后关闭数据集
dataset.Dispose();
}
}
}
结果: