GDAL求取LAI叶面积指数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OSGeo.GDAL;
using System.IO;
using System.Diagnostics;

namespace Yaan_AppSysWinForm
{
    public partial class LaiForm : Form
    {
        public LaiForm()
        {
            InitializeComponent();
        }

        string fileNameOnly = "";

        private void btnInputImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbInputImage.Text = ofd.FileName;
            }
            int index = tbInputImage.Text.LastIndexOf("\\");
            fileNameOnly = tbInputImage.Text.Substring(index + 1);
            
        }

        private void btnInputClassified_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbInputClassified.Text = ofd.FileName;
            }
        }

        private void btnInputTxt_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                tbInputTxt.Text = ofd.FileName;
            }
        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            string saveFolder = "";
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                saveFolder = fbd.SelectedPath;
            }

            int index = fileNameOnly.LastIndexOf(".");
            tbOutput.Text = saveFolder + "\\" + "lai_result.tif";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("取消");
            this.Close();
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            Gdal.AllRegister();
            Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");

            if (tbInputImage.Text == "")
            {
                MessageBox.Show("影像路径不能为空");
                return;
            }

            Dataset dsSrc = Gdal.Open(tbInputImage.Text, Access.GA_ReadOnly);//原影像

            int imgWidth = dsSrc.RasterXSize;    //获得影像宽
            int imgHeight = dsSrc.RasterYSize;   //获得影像高
            int bandNum = dsSrc.RasterCount;     //获得波段数量

            //原影像的灰度值
            double[] dSrcArray = new double[imgHeight * imgWidth];

            Band bandSrc = dsSrc.GetRasterBand(1);

            //将原图像的像元值读取到dSrcArray数组中
            bandSrc.ReadRaster(0, 0, imgWidth, imgHeight, dSrcArray, imgWidth, imgHeight, 0, 0);

            #region 验证原影像的灰度值数据--已注释
            //for (int ii = 0; ii < dSrcArray.Length; ++ii)
            //{
            //    if (ii % 40 == 0)
            //    {
            //        Debug.WriteLine("\n");
            //    } 
            //    Debug.Write(dSrcArray[ii].ToString() + "  ");
            //}
            #endregion

            //分类影像
            Dataset dsClassified = Gdal.Open(tbInputClassified.Text, Access.GA_ReadOnly);
            int classWidth = dsClassified.RasterXSize;//获得分类影像宽度
            int classHeight = dsClassified.RasterYSize;//获得分类影像高度
            int[] dClassifiedArray = new int[classWidth * classHeight];//存储分类影像的灰度值
            Band bandClassified = dsClassified.GetRasterBand(1);

            //将原图像的像元值读取到dClassifiedArray数组中
            bandClassified.ReadRaster(0, 0, classWidth, classHeight, dClassifiedArray, classWidth, classHeight, 0, 0);
            
            
            #region  验证分类影像的灰度值数据--已注释
            //for (int ii = 0; ii < dClassifiedArray.Length; ++ii)
            //{
            //    if (ii % 40 == 0)
            //    {
            //        Debug.WriteLine("\n");
            //    }
            //    Debug.Write(dClassifiedArray[ii].ToString() + "  ");
            //}
            #endregion


            //构建位图
            //Bitmap bmp = new Bitmap(classWidth, classHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            //Bitmap b = new Bitmap(400, 400, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);


            #region 读取查找表数据,类型:typeOfShuiDao,typeOfYuMi,typeOfHuaSheng,typeOfLinDi,typeOfCaoDi,typeOfOther;回归系数:aShuiDao, aYuMi, aHuaSheng, aLinDi, aCaoDi, aOther,bShuiDao, bYuMi, bHuaSheng, bLinDi, bCaoDi, bOther

            /**************读取查找表****************/
            string[] lines = File.ReadAllLines(tbInputTxt.Text,Encoding.Default);
            int typeOfShuiDao = 1111;
            int typeOfYuMi = 1131;
            int typeOfHuaSheng = 1132;
            int typeOfLinDi = 131;
            int typeOfCaoDi = 141;
            int typeOfOther = 0;
            double aShuiDao, aYuMi, aHuaSheng, aLinDi, aCaoDi, aOther = 0;
            double bShuiDao, bYuMi, bHuaSheng, bLinDi, bCaoDi, bOther = 0;
           
            string[] temp1=lines[1].Split(new char[]{' ','\t'}, StringSplitOptions.RemoveEmptyEntries);
            aShuiDao = Convert.ToDouble(temp1[2]);
            bShuiDao = Convert.ToDouble(temp1[3]);

            string[] temp2 = lines[2].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            aYuMi = Convert.ToDouble(temp2[2]);
            bYuMi = Convert.ToDouble(temp2[3]);

            string[] temp3 = lines[3].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            aHuaSheng = Convert.ToDouble(temp3[2]);
            bHuaSheng = Convert.ToDouble(temp3[3]);

            string[] temp4 = lines[4].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            aLinDi = Convert.ToDouble(temp4[2]);
            bLinDi = Convert.ToDouble(temp4[3]);

            string[] temp5 = lines[5].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            aCaoDi = Convert.ToDouble(temp5[2]);
            bCaoDi = Convert.ToDouble(temp5[3]);

            string[] temp6 = lines[6].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            aOther = Convert.ToDouble(temp6[2]);
            bOther = Convert.ToDouble(temp6[3]);

            Debug.WriteLine(aShuiDao.ToString() + "   " + bShuiDao.ToString());
            Debug.WriteLine(aYuMi.ToString() + "   " + bYuMi.ToString());
            Debug.WriteLine(aHuaSheng.ToString() + "   " + bHuaSheng.ToString());
            Debug.WriteLine(aLinDi.ToString() + "   " + bLinDi.ToString());
            Debug.WriteLine(aCaoDi.ToString() + "   " + bCaoDi.ToString());
            Debug.WriteLine(aOther.ToString() + "   " + bOther.ToString());

            #endregion

            double[] resultArray = new double[imgHeight * imgWidth];//将该数组的数据写入新建的空白tiff中

            /*************创建TIFF***************/
            //获取指定格式的驱动,用于创建图像
            string pszFormat = "GTiff";
            //GTIiff创建为tif图像,HFA创建为Erdas的img格式,ENVI创建为ENVI的hdr文件

            Driver oDriver = Gdal.GetDriverByName(pszFormat);
            if (oDriver == null)
            {
                Console.WriteLine("格式{0}不支持Create()方法。\n", pszFormat);
                return;
            }

            /********************新建tiff文件,注意数据格式********************/
            //Dataset oDs = oDriver.Create(tbOutput.Text, imgWidth, imgHeight, 1, DataType.GDT_Byte, null);
            
       <span style="font-size:14px;color:#FF0000;">   <strong>  //支持double数据写入tiff
            Dataset oDs = oDriver.Create(tbOutput.Text, imgWidth, imgHeight, 1, DataType.GDT_Float64, null);</strong></span>
            if (oDs == null)
            {
                MessageBox.Show("创建图像{0}失败。\n", tbOutput.Text);
                return;
            }
            Band pBand = oDs.GetRasterBand(1);
            

            for (int i = 0; i < imgWidth; i++)
            {
                for (int j = 0; j < imgHeight; j++)
                {
                    //分类处理
                    switch (dClassifiedArray[i + j * imgWidth])
                    {
                        case 1111:
                            if ((dSrcArray[i + j * imgWidth] * aShuiDao + bShuiDao )<= 0)
                            {
                                resultArray[i + j * imgWidth] = 0;
                            }
                            else
                            {
                                resultArray[i + j * imgWidth] = dSrcArray[i + j * imgWidth] * aShuiDao + bShuiDao;
                            }
                            break;
                        case 1131:
                            if ((dSrcArray[i + j * imgWidth]*aYuMi+bYuMi)<= 0)
                            {
                                resultArray[i + j * imgWidth] = 0;
                            }
                            else
                            {
                                resultArray[i + j * imgWidth] = dSrcArray[i + j * imgWidth] * aYuMi + bYuMi;
                            }
                            break;
                        case 1132:
                            if ((dSrcArray[i + j * imgWidth] * aHuaSheng + bHuaSheng) <= 0)
                            {
                                resultArray[i + j * imgWidth] = 0;
                            }
                            else
                            {
                                resultArray[i + j * imgWidth] = dSrcArray[i + j * imgWidth] * aHuaSheng + bHuaSheng;
                            }
                            break;
                        case 131:
                            if ((dSrcArray[i + j * imgWidth] * aLinDi + bLinDi) <= 0)
                            {
                                resultArray[i + j * imgWidth] = 0;
                            }
                            else
                            {
                                resultArray[i + j * imgWidth] = dSrcArray[i + j * imgWidth] * aLinDi + bLinDi;
                            }
                            break;
                        case 141:
                            if ((dSrcArray[i + j * imgWidth] * aCaoDi + bCaoDi) <= 0)
                            {
                                resultArray[i + j * imgWidth] = 0;
                            }
                            else
                            {
                                resultArray[i + j * imgWidth] = dSrcArray[i + j * imgWidth] * aCaoDi + bCaoDi;
                            }
                            break;
                        case 0:
                            resultArray[i + j * imgWidth] = dSrcArray[i + j * imgWidth] * aOther + bOther;
                            break;
                        default:
                            resultArray[i + j * imgWidth] = dSrcArray[i + j * imgWidth] * aOther + bOther;
                            break;
                    }
                }
            }

            Debug.WriteLine("Max:" + resultArray.Max().ToString());
            Debug.WriteLine("Min:" + resultArray.Min().ToString());

            pBand.WriteRaster(0, 0, imgWidth, imgHeight, resultArray, imgWidth, imgHeight, 0, 0);
            oDs.FlushCache();
            oDs.Dispose();
            MessageBox.Show("数据集生成成功!\n所在目录:" + tbOutput.Text);
        }
    }
}
成功!
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
叶面积指数(Leaf Area Index,简称LAI)是用来描述植被叶面积密度的一个指标。Python可以用于叶面积指数的反演,其中一种常用的方法是利用遥感数据。 在遥感数据中,可以使用不同波段的光谱信息来推测植被的叶面积指数。以下是一个简单的步骤示例: 1. 读取遥感数据:首先,你需要读取相应的遥感影像数据。你可以使用Python中的库,如GDAL或rasterio来读取和处理遥感影像数据。 2. 数据预处理:对于遥感数据,通常需要进行预处理,例如云层去除、大气校正等。这些预处理步骤有助于提高反演结果的准确性。 3. 特征提取:根据不同的遥感数据类型和法,你可以提取不同的特征。常见的特征包括植被指数(如NDVI,Normalized Difference Vegetation Index)和植被水分指数(如NDWI,Normalized Difference Water Index)等。 4. 建立模型:根据提取的特征和已知的地面测量数据,可以使用机器学习或统计方法建立回归模型来反演叶面积指数。常见的方法包括线性回归、支持向量机(SVM)、随机森林等。 5. 模型验证:对于建立的反演模型,需要进行验证来评估其准确性和可靠性。你可以使用交叉验证或留出法等方法来评估模型的表现。 请注意,叶面积指数的反演是一个复杂的过程,具体的方法和步骤会因数据类型、研究目的和数据可用性而有所不同。以上只是一个简单的示例,你可以根据自己的需求和数据情况进行相应的调整和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值