C# 基于GDAL读取影像数据并用四叉树存储

这是一个使用C#通过GDAL库读取地理影像数据,并利用四叉树进行存储的示例。代码首先打开指定路径的TIFF文件,然后读取其像素数据,接着通过四叉树算法对数据进行分块和存储。程序还包含了数据预处理和一些辅助函数,如数组扩展、分块和四叉树构建。虽然代码较为初级,但能够完成基本的影像数据处理和存储功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原谅,这是刚开始学C#学的代码,所以比较凌乱,但是是可以运行的

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.Data.OracleClient;
using System.Data.OleDb;
namespace GDALRead
{
    public struct point
    {
        public float x, y;
        public point(float x, float y)
        {
            this.x = x; this.y = y;
        }
    }
    public partial class Form1 : Form
    {
        int[,] _data = new int[16, 16];
        List<int[]> _QTree = new List<int[]>();
        int s = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Gdal.AllRegister();
            // string fileName = @"E:\img\jiangxueying.img";
            string fileName = @"E:\img\11.tif";
            Dataset DEMdata = Gdal.Open(fileName, Access.GA_ReadOnly);
            if (DEMdata == null)
            {
                MessageBox.Show("不能打开:" + fileName);
                return;
            }
            int size = DEMdata.RasterCount;//影像大小
            int xsize = DEMdata.RasterXSize;//行
            int ysize = DEMdata.RasterYSize;//列
            Band DEMband = DEMdata.GetRasterBand(1);//波段
            double val;
            int hasval;
            DEMband.GetMinimum(out val, out hasval);
            if (hasval != 0)
            {
                string strmin = val.ToString();
            }
            DEMband.GetMaximum(out val, out hasval);
            if (hasval != 0)
            {
                string strmax = val.ToString();
            }
            string strProjection = DEMdata.GetProjectionRef();//投影信息
            //输出图像的坐标和分辨率信息
            double[] adfGeoTransform = new double[6];
            DEMdata.GetGeoTransform(adfGeoTransform);
            string strOrigin = adfGeoTransform[0] + "," + adfGeoTransform[3];
            string strPixelsize = adfGeoTransform[1] + "," + adfGeoTransform[5];
            double a;
            DEMband.GetNoDataValue(out a, out hasval);

            int[] DEM = new int[xsize * ysize];
            DEMband.ReadRaster(0, 0, xsize, ysize, DEM, xsize, ysize, 0, 0);
            int t1 = 0;
            AllDEM = new int[xsize, ysize];
            for (int i = 0; i < xsize; i++)
            {
                for (int j = 0; j < ysize; j++)
                {
                    AllDEM[i, j] = DEM[t1];
                    t1++;
                }
            }
            //OpenFileDialog opF = new OpenFileDialog();
            //string[] lines = System.IO.File.ReadAllLines(@"E:\新建文本文档.txt", Encoding.Default);//读取文件
            //double[,] data = new double[lines.Length, lines[0].Split(',').Length];//将相应的数据存入数组中并赋值;
            //for (int i = 0; i < lines.Length; i++)
            //{//
            //for (int j = 0; j < lines[0].Split(',').Length; j++)
            //{
            //data[i, j] = double.Parse(lines[i].Split(',')[j]);
            //}
            //}
            int[,] element_data = element_array(AllDEM, 256);
            int[][,] child = new int[element_data.GetLength(0) * element_data.GetLength(1) / 256 / 256][,];
            int count = 0;
            string[][] all = new string[child.GetLength(0) * 256 * 256][];
            Point[][] p = depart(element_data, 256);
            string[][,] result = new string[child.GetLength(0)][,];
            string lll;
            for (int i = 0; i < child.GetLength(0); i++)
            {
                child[i] = index(element_data, p[i]);
                if (i == 132)
                    lll = "";
                result[i] = quadtree(child[i]);
                for (int j = 0; j < result[i].GetLength(0); j++)
                {
                    if (result[i][j, 0] == null && result[i][j, 1] == null || result[i][j, 1] == "0")
                        continue;
                    all[count] = new string[3];
                    all[count][0] = result[i][j, 0]; all[count][1] = result[i][j, 1]; all[count][2] = i.ToString();
                    count++;
                }
            }
            MessageBox.Show("数据获取完毕,四叉分成功");
        }

        ////计算morton值的方法
        //public int GetMorton(int row, int column)
        //{
        //    int i = 0;
        //    string c2 = "";
        //    string r = Convert.ToString(row, 2);
        //    string c = Convert.ToString(column, 2);
        //    if (r.Length > c.Length)
        //    {
        //        i = r.Length - c.Length;
        //        for (int a = 0; a < i; a++)
        //        {
        //            c2 = "0" + c2;
        //        }
        //        c = c2 + c;
        //    }
        //    else
        //    {
        //        i = c.Length - r.Length;
        //        for (int a = 0; a < i; a++)
        //        {
        //            c2 = "0" + c2;
        //        }
        //        r = c2 + r;
        //    }
        //    StringBuilder strBu = new StringBuilder();
        //    char[] rchar = r.ToCharArray();
        //    char[] cchar = c.ToCharArray();
        //    for (int j = 0; j < r.Length; j++)
        //    {
        //        strBu.Append(rchar[j]);
        //        strBu.Append(cchar[j]);
        //    }
        //    string result = strBu.ToString();
        //    int result10 = Convert.ToInt32(result, 2);
        //    return result10;
        //}
        //private void button2_Click(object sender, EventArgs e)
        //{ {
        //    //读取txt文件
        //    //1.读取morton
        //    OpenFileDialog opF = new OpenFileDialog();

        //    }
        //    string s = "2";
        //}
        //非静态字段访问有疑问,上个程序可以直接访问非静态的
        //public static double[,] result = new double[265 * 265, 3];

        public static string[,] quad = new string[2048 * 2048, 2];
        public static int[][,] value1 = new int[2048 * 2048 + 1][,];
        /*     extern */
        private int[,] AllDEM;
        public static int[,] element_array(int[,] x, int a) //将数组的行列数扩大至64的倍数,补充的元素的数值默认为0;
        {
            int a1 = x.GetLength(0), a2 = x.GetLength(1);
            int add1 = a1 % a, add2 = a2 % a;
            int new_x = a1 + a - add1, new_y = a2 + a - add2;//存放新数组的行列号;
            if (add1 == 0)
            {
                new_x = a1;
            }
            if (add2 == 0)
            {
                new_y = a2;
            }
            int[,] extend = new int[new_x, new_y];
            for (int i = 0; i < x.GetLength(0); i++)
            {
                for (int j = 0; j < x.GetLength(1); j++)
                {
                    extend[i, j] = x[i, j];
                }
            }
            return extend;
        }
        public static Point[][] depart(int[,] x, int a)//将图像分成a*a的小块,并以坐标形式返回他们小块的坐上和右下的行列号(对角上的两点可以确定一个矩形);
        {
            int t2 = x.GetLength(0) * x.GetLength(1) / a / a; //表示分成的总块数
            int t1 = 0;
            Point[][] position = new Point[t2][];
            for (int i = 0; i < t2; i++)
            {
                position[i] = new Point[2];//以二维坐标数组存放块所在的位置
            }
            for (int i = 0; i < x.GetLength(0) / a; i++)
            {
                for (int j = 0; j < x.GetLength(1) / a; j++)
                {
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值