C#使用GDAL读取与创建影像

时间:2022-04-16 06:59:51

代码中都加了凝视,这里就不再一一叙述了。代码例如以下:

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>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>影像坐标调动參数</param> /// <param>像素地址行</param> /// <param>像素地址列</param> /// <param>X</param> /// <param>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>影像坐标变革參数</param> /// <param>X</param> /// <param>Y</param> /// <param>像素地址行</param> /// <param>像素地址列</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类,,后面再一步步完好这个类。