C#下GDAL的使用这里就不多赘述了。參见上一篇博客。
代码中都加了凝视,这里就不再一一叙述了。代码例如以下:
class FloodSimulation
{
#region 类成员变量
public Dataset m_DEMDataSet; //DEM数据集
public Dataset m_FloodSimulatedDataSet; //洪涝淹没范围数据集
public int m_XSize; //数据X方向栅格个数
public int m_YSize; //数据Y方向栅格个数
public double m_AreaFlooded; //水面面积
public double m_WaterVolume; //淹没水体体积
public double[] m_FloodBuffer; //填充缓冲区(洪涝淹没范围)
public double[] m_DEMdataBuffer; //DEM数据缓冲
/* 这里的GeoTransform(影像坐标变换參数)的定义是:通过像素所在的行列值得到其左上角点空间坐标的运算參数
比如:某图像上(P,L)点左上角的实际空间坐标为:
Xp = GeoTransform[0] + P * GeoTransform[1] + L * GeoTransform[2];
Yp = GeoTransform[3] + P * GeoTransform[4] + L * GeoTransform[5]; */
public double[] m_adfGeoTransform;
#endregion
//构造函数
public FloodSimulation()
{
m_adfGeoTransform = new double[6];
}
///
/// 载入淹没区DEM,并创建淹没范围影像
///
/// DEM文件路径
///
public void loadDataSet(string m_DEMFilePath)
{
m_DEMDataSet = Gdal.Open(m_DEMFilePath, Access.GA_ReadOnly);
//获取X、Y方向栅格数
m_XSize = m_DEMDataSet.RasterXSize;
m_YSize = m_DEMDataSet.RasterYSize;
//读取DEM数据到内存中
Band m_DEMBand = m_DEMDataSet.GetRasterBand(1); //获取第一个波段
m_DEMdataBuffer = new double[m_XSize * m_YSize];
m_DEMBand.ReadRaster(0, 0, m_XSize, m_YSize, m_DEMdataBuffer, m_XSize, m_YSize, 0, 0);
//淹没范围填充缓冲区
m_FloodBuffer = new double[m_XSize * m_YSize];
//获取影像坐标转换參数
m_DEMDataSet.GetGeoTransform(m_adfGeoTransform);
//创建洪涝淹没范围影像
string m_FloodImagePath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\FloodSimulation\\FloodedRegion.tif";
if (System.IO.File.Exists(m_FloodImagePath))
{
System.IO.File.Delete(m_FloodImagePath);
}
//在GDAL中创建影像,先须要明白待创建影像的格式,并获取到该影像格式的驱动
OSGeo.GDAL.Driver driver = Gdal.GetDriverByName("GTiff");
//调用Creat函数创建影像
m_FloodSimulatedDataSet=driver.Create(m_FloodImagePath, m_XSize, m_YSize, 1, DataType.GDT_CFloat32, null);
//设置影像属性
m_FloodSimulatedDataSet.SetGeoTransform(m_adfGeoTransform); //影像转换參数
m_FloodSimulatedDataSet.SetProjection(m_DEMDataSet.GetProjection()); //投影
//将影像数据写入内存
m_FloodSimulatedDataSet.GetRasterBand(1).WriteRaster(0, 0, m_XSize, m_YSize, m_FloodBuffer, m_XSize, m_YSize, 0, 0);
m_FloodSimulatedDataSet.GetRasterBand(1).FlushCache();
m_FloodSimulatedDataSet.FlushCache();
}
///
/// 从像素空间转换到地理空间
///
/// 影像坐标变换參数
/// 像素所在行
/// 像素所在列
/// X
/// Y
public void imageToGeoSpace( double [] m_GeoTransform, int pixel,int line, out double X,out double Y )
{
X = m_GeoTransform[0] + pixel * m_GeoTransform[1] + line * m_GeoTransform[2];
Y = m_GeoTransform[3] + pixel * m_GeoTransform[4] + line * m_GeoTransform[5];
}
///
/// 从地理空间转换到像素空间
///
/// 影像坐标变化參数
/// X
/// Y
/// 像素所在行
/// 像素所在列
public void geoToImageSpace(double[] m_GeoTransform, double x, double y, out int pixel, out int line)
{
line = (int)((y * m_GeoTransform[1] - x * m_GeoTransform[4] + m_GeoTransform[0] * m_GeoTransform[4] - m_GeoTransform[3] * m_GeoTransform[1]) / (m_GeoTransform[5] * m_GeoTransform[1] - m_GeoTransform[2] * m_GeoTransform[4]));
pixel = (int)((x - m_GeoTransform[0] - line * m_GeoTransform[2]) / m_GeoTransform[1]);
}
}
因项目须要做洪涝模拟。所以採用GDAL使用C#编写了FloodSimulation类,后面再一步步完好这个类。