private void MakeGrid(sFLTData FLTData, string InitialFile, MapWinGIS.ICallback Callback) { //根据.flt文件及其数据文件,创建一个grid网格对象 MapWinGIS.GridHeader h = new MapWinGIS.GridHeader(); int i, j; h.dX = FLTData.cellsize; h.dY = FLTData.cellsize; h.Notes = "Grid文件导入路径 " + System.IO.Path.GetFileName(InitialFile); h.NodataValue = FLTData.NODATA_value; h.NumberCols = FLTData.ncols; h.NumberRows = FLTData.nrows; h.XllCenter = FLTData.xllcenter; h.YllCenter = FLTData.yllcenter; base.Close(); base.CreateNew("", h, MapWinGIS.GridDataType.FloatDataType, -1, true, MapWinGIS.GridFileType.Binary); for (i = 0; i < FLTData.ncols; i++) { for (j = 0; j < FLTData.nrows; j++) { base.Value[i, j] = FLTData.Values[i, j]; } if (Callback != null) { Callback.Progress(base.Key, 50 + i / FLTData.ncols * 50, "创建 MapWinGIS grid"); } } }
/// <summary> /// 导入.flt格式的文件 /// </summary> private bool ImportFLTFormat(string InFile, ref MapWinGIS.ICallback Callback) { sFLTData FLTData = new sFLTData(); try { ReadHDRFile(FLTData, InFile.Substring(0, InFile.LastIndexOf(".")) + ".hdr"); ReadFLTData(FLTData, InFile, Callback); MakeGrid(FLTData, InFile, Callback); return(true); } catch (System.Exception ex) { MapWinGIS.Utility.Logger.Message("导入.flt格式的文件出现错误," + InFile + "\r\n" + ex.Message); return(false); } }
private void ReadFLTData(sFLTData FLTData, string DataFile, MapWinGIS.ICallback Callback) { //读取.flt网格图层的数据文件 int i, j; FileStream fs = null; BinaryReader r; try { FLTData.Values = new float[FLTData.ncols - 1 + 1, FLTData.nrows - 1 + 1]; fs = File.Open(DataFile, FileMode.Open); r = new BinaryReader(fs); fs.Seek(0, SeekOrigin.Begin); for (j = 0; j < FLTData.nrows; j++) { for (i = 0; i < FLTData.ncols; i++) { FLTData.Values[i, j] = r.ReadSingle(); } if (Callback != null) { Callback.Progress(base.Key, j / FLTData.nrows * 50, "读取 FLT 数据"); } } } catch (System.Exception ex) { MapWinGIS.Utility.Logger.Message("读取.flt网格图层的数据文件出错: " + "\r\n" + ex.Message); } try { fs.Close(); } catch { } }
private void ReadHDRFile(sFLTData FLTData, string HeaderFile) { int FileNum; string OneLine; bool isCorner = false; FileNum = FileSystem.FreeFile(); FileSystem.FileOpen(FileNum, HeaderFile, OpenMode.Input, OpenAccess.Read); while (!(FileSystem.EOF(FileNum))) { OneLine = Strings.LCase((string)(FileSystem.LineInput(FileNum))); if (OneLine.IndexOf("ncols") + 1 > 0) { int.TryParse((string)(OneLine.Replace("ncols", "").Replace("\t", "").Replace(" ", "")), out FLTData.ncols); } if (OneLine.IndexOf("nrows") + 1 > 0) { int.TryParse((string)(OneLine.Replace("nrows", "").Replace("\t", "").Replace(" ", "")), out FLTData.nrows); } if (OneLine.IndexOf("xllcorner") + 1 > 0) { isCorner = true; double.TryParse(OneLine.Replace("xllcorner", "").Replace("\t", "").Replace(" ", ""), out FLTData.xllcenter); } if (OneLine.IndexOf("yllcorner") + 1 > 0) { isCorner = true; double.TryParse(OneLine.Replace("yllcorner", "").Replace("\t", "").Replace(" ", ""), out FLTData.yllcenter); } if (OneLine.IndexOf("xllcenter") + 1 > 0) { double.TryParse(OneLine.Replace("xllcenter", "").Replace("\t", "").Replace(" ", ""), out FLTData.xllcenter); } if (OneLine.IndexOf("yllcenter") + 1 > 0) { double.TryParse(OneLine.Replace("yllcenter", "").Replace("\t", "").Replace(" ", ""), out FLTData.yllcenter); } if (OneLine.IndexOf("cellsize") + 1 > 0) { string tStr; double tDbl; tStr = OneLine.Substring(OneLine.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' })); double.TryParse(tStr, out tDbl); tDbl = Math.Round(tDbl, 13); FLTData.cellsize = tDbl; } if (OneLine.IndexOf("nodata_value") + 1 > 0) { double t = (double)FLTData.NODATA_value; double.TryParse(OneLine.Replace("nodata_value", "").Replace(" ", "").Replace("\t", ""), out t); FLTData.NODATA_value = (int)t; } if (OneLine.IndexOf("byteorder") + 1 > 0) { FLTData.byteorder = (string)(OneLine.Replace("byteorder", "").Replace(" ", "").Replace("\t", "")); } } if (isCorner) { FLTData.xllcenter += FLTData.cellsize / 2; FLTData.yllcenter += FLTData.cellsize / 2; } FileSystem.FileClose(FileNum); }