原谅,这是刚开始学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;
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";
{
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;
}
if (DEMdata == null)
{
MessageBox.Show("不能打开:" + fileName);
return;
}
int size = DEMdata.RasterCount;//影像大小
int xsize = DEMdata.RasterXSize;//行
int ysize = DEMdata.RasterYSize;//列
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();
}
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);
double[] adfGeoTransform = new double[6];
DEMdata.GetGeoTransform(adfGeoTransform);
string strOrigin = adfGeoTransform[0] + "," + adfGeoTransform[3];
string strPixelsize = adfGeoTransform[1] + "," + adfGeoTransform[5];
string strPixelsize = adfGeoTransform[1] + "," + adfGeoTransform[5];
double a;
DEMband.GetNoDataValue(out a, out hasval);
DEMband.GetNoDataValue(out a, out hasval);
int[] DEM = new int[xsize * ysize];
DEMband.ReadRaster(0, 0, xsize, ysize, DEM, xsize, ysize, 0, 0);
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 = new int[xsize, ysize];
for (int i = 0; i < xsize; i++)
{
for (int j = 0; j < ysize; j++)
{
AllDEM[i, j] = DEM[t1];
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]);
}
//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++;
{
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("数据获取完毕,四叉分成功");
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();
//{ {
// //读取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 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++)
{
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++)
{