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]; } /// <summary> /// 加载淹没区DEM,并创建淹没范围影像 /// </summary> /// <param name="m_DEMFilePath">DEM文件路径</param> /// <returns></returns> 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(); } /// <summary> /// 从像素空间转换到地理空间 /// </summary> /// <param name="adfGeoTransform">影像坐标变换参数</param> /// <param name="pixel">像素所在行</param> /// <param name="line">像素所在列</param> /// <param name="x">X</param> /// <param name="y">Y</param> 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]; } /// <summary> /// 从地理空间转换到像素空间 /// </summary> /// <param name="adfGeoTransform">影像坐标变化参数</param> /// <param name="x">X</param> /// <param name="y">Y</param> /// <param name="pixel">像素所在行</param> /// <param name="line">像素所在列</param> 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类,后面再一步步完善这个类。