打开grd(ascii编码的)
例如:
DSAA //都是这样 没有什么特别的
2800 2200 //表示x方向上的网格点(xNumCol)有2800个 Y方向上的网格点(yNumRow)有2200
119 121.8 //x的最小值为119 x的最大值为 121.8
29.8 32 //y的最小值为29.8 y的最大值为32
75.248794733946 152.49487339557 //z的最小值是75.248794733946 z的最大值是152.49487339557
有类似于下面的数据
90.27496621276215 90.27466509955178 90.27437613458082 90.27409937596927 90.27383488200486 ................
(x1,y1) (x2,y1) (x3,y1) (x4,y1) (x5,y1) (x10,y1)
............... ..................... .................... ..................... ...................... ..................此处省略(2800/10)行
(x280,y1)
90.27496621276215 90.27466509955178 90.27437613458082 90.27409937596927 90.27383488200486 ................
(x1,y2) (x2,y2) (x3,y2) (x4,y2) (x5,y2) (x10,y2)
............... ..................... .................... ..................... ...................... ..................此处省略(2800/10)行
(x280,y2)
..............................................................................................................................................................................
...............................................................................................................................................................................
90.27496621276215 90.27466509955178 90.27437613458082 90.27409937596927 90.27383488200486 ................
(x1,y2200) (x2,y2200) (x3,y2200) (x4,y2200) (x5,y2200) (x10,y2200)
............... ..................... .................... ..................... ...................... ..................此处省略(2800/10)行
(x280,y2200)
//问题随意给出在xmin到xmax 和 ymin到ymax的两个字 求出z的值来 例如 x=100 y =30
判断x=100出现的位置
int xNum=0;
if(xNum<=xMin){ // 取在第一个位置
xNum=0;
}else if(xNum>=XMax){
xNum=xNumCol-1;
}else{
for(int i=0;i<xNumCol;i++){
if(xmin+x*xInterval<x&&xmin +(i+1)*xInterval>x){ //表示下标为i
xum=i;
}else{
xum=i+1;
}
}
}
同理可求出Y的位置
if (y <= yMin) { yNum = 0; } else if (y >= yMax) { yNum = NumRow - 1; } else for (int i = 0; i < NumRow; i++) { if (yMin + i * yInterval <= y && yMin + (i + 1) * yInterval > y) { if (y - (yMin + i * yInterval) <= yMin + (i + 1) * yInterval - y) { yNum = i;//y的位置 } else { yNum = i + 1; } break; } }
根据xNum 和 YNum 然后算出 要的Z值出现在哪一行和改行的哪个位置 就可以取到最佳值了
int row =y*(xnum/10+1)+5+x/10+1;//具体行数
int col =x%10;//在该行的哪个位置
然后开个流去读取文件就可以了
StreamReader sr = new StreamReader(filename,Decoding.Default);
var i=0;
string txtTag="";
while(!sr.EndOfStream){
i++;
if(i<row+2){
txtTag=sr.ReadLine();
}else{
break;
}
}
sr.close();
double factValue = txtTag.split('')[col];//取出值
//效率很低 因为要一行一行的读 不能跳这读 很闹心
其实文件格式很固定 用二进制流取就很快了 50M的文件读取随机读取只要7毫秒哟
自己看代码吧
protected void Page_Load(object sender, EventArgs e) { List<double> d = new List<double>(); Random random = new Random(); DateTime Starttime = System.DateTime.Now; for (int i = 0; i < 40; i++) { double x = random.NextDouble() * (122.452486 - 117.742635) + 117.742635; double y = random.NextDouble() * (32.463007 - 29.418809) + 29.418809; d.Add(show("~/file/grid.b.grd", x, y)); } DateTime EndTime = System.DateTime.Now; TimeSpan dt = EndTime - Starttime; double time = dt.TotalMilliseconds; } public double show(string fileAddress, double x, double y) { int NumCol; //x方向的网格数 int NumRow;//y方向的网格数 double xMin;//x的起始值 double xMax;//x的结束值 double yMin;//y的起始值 double yMax;//y的结束值 FileStream fs = new FileStream(Server.MapPath(fileAddress), FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs, Encoding.ASCII); StringBuilder sb = new StringBuilder(); for (int i = 0; i < 4; i++) { sb.Append(br.ReadChar()); } sb.Append('\n'); for (int i = 0; i < 2; i++) { sb.Append(br.ReadInt16()).Append(" "); } for (int i = 0; i < 6; i++) { sb.Append(br.ReadDouble()).Append(" "); } var list = sb.ToString().Split('\n')[1].Split(' '); NumCol = Convert.ToInt32(list[0]); NumRow = Convert.ToInt32(list[1]); xMin = Convert.ToDouble(list[2]); xMax = Convert.ToDouble(list[3]); yMin = Convert.ToDouble(list[4]); yMax = Convert.ToDouble(list[5]); double xInterval = (xMax - xMin) / NumCol; // x方向增量 double yInterval = (yMax - yMin) / NumRow; // y方向增量 int xNum = 0, yNum = 0; if (x <= xMin) { xNum = 0; } else if (x >= xMax) { xNum = NumCol - 1; } else for (int i = 0; i < NumCol; i++)//对行进行处理 { if (xMin + i * xInterval <= x && xMin + (i + 1) * xInterval > x) //判断x值所在的区间 { if (x - (xMin + i * xInterval) <= xMin + (i + 1) * xInterval - x) { xNum = i;//x在的位置 } else { xNum = i + 1; } break; } } if (y <= yMin) { yNum = 0; } else if (y >= yMax) { yNum = NumRow - 1; } else for (int i = 0; i < NumRow; i++) { if (yMin + i * yInterval <= y && yMin + (i + 1) * yInterval > y) { if (y - (yMin + i * yInterval) <= yMin + (i + 1) * yInterval - y) { yNum = i;//y的位置 } else { yNum = i + 1; } break; } } int factValue = yNum * NumCol + xNum; float z = 0.0f; br.BaseStream.Position = (factValue - 1) * 4 + 60; if (br.PeekChar() > -1) { z = br.ReadSingle(); } br.Close(); fs.Close(); return Convert.ToDouble(z); }