//导入.dem格式的文件 private bool ImportDEMFormat(string InFile, ref MapWinGIS.ICallback Callback) { Stream FileStream; sDEMData DEMData = new sDEMData(); try { FileStream = System.IO.File.Open(InFile, FileMode.Open, FileAccess.Read); if (!(FileStream == null)) { DEMData = ReadDEMData(FileStream, Callback); FileStream.Close(); FileStream.Dispose(); MakeGrid(DEMData, Callback); return(true); } else { return(false); } } catch (System.Exception ex) { MapWinGIS.Utility.Logger.Message("导入.dem格式的文件出现错误 " + InFile + "\r\n" + ex.Message); return(false); } }
private void MakeGrid(sDEMData DEMData, MapWinGIS.ICallback Callback) { //根据.dem文件,生成一个网格文件对象 MapWinGIS.GridHeader h = new MapWinGIS.GridHeader(); int i; int j; int m; int n; h.dX = 30; h.dY = 30; h.Notes = "DEMData.Notes"; h.NodataValue = -1; h.NumberCols = DEMData.NumCols; h.NumberRows = System.Convert.ToInt32((DEMData.MaxY() - DEMData.MinY() / 30)) + 1; h.XllCenter = DEMData.MinX(); h.YllCenter = DEMData.MinY(); MapWinGIS.Utility.Logger.Dbg("根据.dem文件和其数据文件,生成一个网格文件对象"); base.Close(); MapWinGIS.Utility.Logger.Dbg("开始创建"); base.CreateNew("", h, MapWinGIS.GridDataType.ShortDataType, h.NodataValue, true, MapWinGIS.GridFileType.Binary); MapWinGIS.Utility.Logger.Dbg("只带header的新的grid网格文件创建了"); for (i = 0; i < DEMData.NumCols; i++) { for (j = 0; j < DEMData.NumElevs[i]; j++) { base.ProjToCell(DEMData.ColStarts[i].X, System.Convert.ToInt32(DEMData.ColStarts[i].Y) + (30 * j), out m, out n); base.Value[m, n] = DEMData.Values[i, j]; } MapWinGIS.Utility.Logger.Dbg("创建 MapWinGIS grid 对象. numCols: " + i.ToString()); if (Callback != null) { Callback.Progress(base.Key, 50 + i / DEMData.NumCols * 50, "创建 MapWinGIS grid 对象"); } } MapWinGIS.Utility.Logger.Dbg("生成一个网格文件对象完成"); }
private sDEMData ReadDEMData(Stream fileStream, MapWinGIS.ICallback Callback) { // 从USGS读取dem格式的数据文件(ASCII 文本) int i, j; int numElevs; int Elev = 0; int Off2; int Off3; int Inserts; sDEMData DEMData = new sDEMData(); string chunckResult; try { //获取header数据 DEMData.Notes = GetChunk(fileStream, 1, 144); DEMData.HorizUnits = System.Convert.ToInt32(GetChunk(fileStream, 529, 6)); DEMData.VertUnits = System.Convert.ToInt32(GetChunk(fileStream, 535, 6)); DEMData.Vertices = new sVertex[4]; for (i = 0; i <= 3; i++) { DEMData.Vertices[i].X = Fdbl(GetChunk(fileStream, 547 + (48 * i), 24)); DEMData.Vertices[i].Y = Fdbl(GetChunk(fileStream, 571 + (48 * i), 24)); } DEMData.Min = System.Convert.ToDouble(Fdbl(GetChunk(fileStream, 739, 24))); DEMData.Max = System.Convert.ToDouble(Fdbl(GetChunk(fileStream, 763, 24))); DEMData.NumCols = System.Convert.ToInt32((GetChunk(fileStream, 859, 6))); DEMData.Values = new int[DEMData.NumCols - 1 + 1, 1]; DEMData.NumElevs = new int[DEMData.NumCols - 1 + 1]; MapWinGIS.Utility.Logger.Dbg("DEM 的头(Header)信息:"); MapWinGIS.Utility.Logger.Dbg("DEMData.Notes: " + DEMData.Notes); MapWinGIS.Utility.Logger.Dbg("DEMData.HorizUnits: " + DEMData.HorizUnits.ToString()); MapWinGIS.Utility.Logger.Dbg("DEMData.VertUnits: " + DEMData.VertUnits.ToString()); MapWinGIS.Utility.Logger.Dbg("DEMData.Min: " + DEMData.Min.ToString()); MapWinGIS.Utility.Logger.Dbg("DEMData.Max: " + DEMData.Max.ToString()); MapWinGIS.Utility.Logger.Dbg("DEMData.NumCols: " + DEMData.NumCols.ToString()); //获取海拔(elevation)数据 Off2 = 1024; Inserts = 0; DEMData.ColStarts = new sVertex[DEMData.NumCols - 1 + 1]; MapWinGIS.Utility.Logger.Dbg("DEM Data"); for (i = 0; i < DEMData.NumCols; i++) { //读取elevs的数量, starting x 和 starting y numElevs = Convert.ToInt32(GetChunk(fileStream, Off2 + 13, 6)); DEMData.NumElevs[i] = numElevs; DEMData.ColStarts[i].X = Fdbl(GetChunk(fileStream, Off2 + 25, 24)); DEMData.ColStarts[i].Y = Fdbl(GetChunk(fileStream, Off2 + 49, 24)); //Information.UBound(DEMData.Values, 2) if (numElevs - 1 > DEMData.Values.GetLength(1)) { DEMData.Values = (int[, ])Microsoft.VisualBasic.CompilerServices.Utils.CopyArray((Array)DEMData.Values, new int[DEMData.NumCols, numElevs]); } for (j = 0; j < numElevs; j++) { Inserts = 0; if (j > 145) { Inserts = (int)((j - 145) / 171 + 1); } Off3 = Off2 + 145 + (j * 6) + Inserts * 4; //4 spaces between each extended group chunckResult = GetChunk(fileStream, Off3, 6); if (chunckResult.Trim() != string.Empty) { if (int.TryParse(chunckResult, out Elev) == false) { Elev = -1; MapWinGIS.Utility.Logger.Dbg("ReadDEMData Error. GetChunk returned non-integer:" + chunckResult); } } DEMData.Values[i, j] = Elev; } if (numElevs <= 146) { Off2 = Off2 + 1024; } else { Inserts = (int)((numElevs - 146) / 170 + 1); Off2 = Off2 + 1024 + Inserts * 1024; } if (Callback != null) { Callback.Progress(base.Key, i / DEMData.NumCols * 50, "读取 DEM 数据"); } } MapWinGIS.Utility.Logger.Dbg("读取 DEM 数据完成"); return(DEMData); } catch (System.Exception ex) { MapWinGIS.Utility.Logger.Msg("读取DEM数据出错 : " + ex.Message); } sDEMData a = new sDEMData(); return(a); }