public static MapData ReadMapData(string path, BinaryFileMetadata metadata) { float[,] data = new float[metadata.GetColumns(), metadata.GetRows()]; float min = float.MaxValue, max = float.MinValue; using (BinaryReader input = new BinaryReader(StreamUtil.GetFileStream(path))) { for (int y = 0; y < metadata.GetRows(); y++) { for (int x = 0; x < metadata.GetColumns(); x++) { float next = ReadNext(input, metadata); data[x, y] = next; if (next >= lowestPossibleHeight) { min = Mathf.Min(min, next); max = Mathf.Max(max, next); } } } } metadata.SetMinHeight(min); metadata.SetMaxHeight(max); ReplaceNoDataValuesWithMinHeight(metadata, data); return(new MapData(data, metadata)); }
private const float lowestPossibleHeight = -11034f; // Mariana trench, source: Wikipedia public static BinaryFileMetadata ReadMetadata(string path) { BinaryFileMetadata metadata = new BinaryFileMetadata(); using (StreamReader input = new StreamReader(StreamUtil.GetFileStream(path))) { if (input.ReadLine() != fileStart) { throw new System.ArgumentException( "Binary file header should start with 'ENVI'!" ); } string line, key = null; while ((line = input.ReadLine()) != null) { if (line.Contains(separator)) { string[] keyValue = line.Split(separator.ToCharArray()); if (keyValue.Length != 2) { throw new System.ArgumentException( "Invalid header data; multiple separators in a line" ); } key = keyValue[0]; metadata.Add(key, keyValue[1]); } else { metadata.Add(key, line); // Extends the key with more data } } } metadata.Process(); return(metadata); }
private static void ReadXmlDocument(string path, XmlDocument xmlDoc) { try { xmlDoc.Load(StreamUtil.GetFileStream(path)); } catch (Exception e) { Debug.Log("Got an exception in reading trail data file."); Debug.Log(e.ToString()); } }
public static MapData ReadMapData(string path, ASCIIGridMetadata metadata) { if (metadata.nrows < 1 || metadata.ncols < 1) { return(null); } float[,] mapData = new float[metadata.ncols, metadata.nrows]; float minHeight = float.MaxValue, maxHeight = float.MinValue; using (StreamReader input = new StreamReader(StreamUtil.GetFileStream(path))) { string line; int x = 0, y = 0; while ((line = input.ReadLine()) != null) { if (line[0] == ' ') // Data lines start with a space { string[] values = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); x = 0; foreach (string value in values) { try { float height = float.Parse(value); if (height != metadata.nodatavalue) { if (height < minHeight) { minHeight = height; } if (height > maxHeight) { maxHeight = height; } } mapData[x, y] = height; } catch (Exception e) { Debug.Log(x + ", " + y); Debug.Log(e.ToString()); } x++; } y++; } } } metadata.Set(ASCIIGridMetadata.minheightKey, minHeight.ToString()); metadata.Set(ASCIIGridMetadata.maxheightKey, maxHeight.ToString()); ReplaceNoDataValuesWithMinHeight(metadata, mapData); return(new MapData(mapData, metadata)); }
public static ASCIIGridMetadata ReadMetadata(string path) { ASCIIGridMetadata metadata = new ASCIIGridMetadata(); using (StreamReader input = new StreamReader(StreamUtil.GetFileStream(path))) { string line; bool keepGoing = true; while (keepGoing && (line = input.ReadLine()) != null) { switch (line[0]) { case ' ': keepGoing = false; // We hit the actual data, stop reading break; default: string[] keyValue = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); metadata.Set(keyValue[0], keyValue[1]); break; } } } return(metadata); }