diff --git a/src/SRTM/SRTM.csproj b/src/SRTM/SRTM.csproj index 5deb36c..0a10685 100644 --- a/src/SRTM/SRTM.csproj +++ b/src/SRTM/SRTM.csproj @@ -27,4 +27,8 @@ + + + + diff --git a/src/SRTM/Sources/CGIAR/AsciiGridFile.cs b/src/SRTM/Sources/CGIAR/AsciiGridFile.cs new file mode 100644 index 0000000..e96aa27 --- /dev/null +++ b/src/SRTM/Sources/CGIAR/AsciiGridFile.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Diagnostics; +using System.Globalization; +using System.IO; +using System.Linq; + +namespace SRTM.Sources.CGIAR +{ + /// + /// An Esri grid is a raster GIS file format developed by Esri, which has two formats: + /// + /// A proprietary binary format, also known as an ARC/INFO GRID, ARC GRID and many other variations + /// A non-proprietary ASCII format, also known as an ARC/INFO ASCII GRID + /// The formats were introduced for ARC/INFO. The binary format is widely used within Esri programs, such /// as ArcGIS, while the ASCII format is used as an exchange, or export format, due to the simple and portable ASCII file structure. + /// + /// The grid defines geographic space as an array of equally sized square grid points arranged in rows and /// columns. Each grid point stores a numeric value that represents a geographic attribute (such as elevation or surface slope) for that unit of space. Each grid cell is referenced by its x,y coordinate location. + /// https://en.wikipedia.org/wiki/Esri_grid + /// Spec: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//009t0000000w000000 + /// + public class ASCIIGridFile + { + private FileStream _fileStream; + private StreamReader _streamReader; + private readonly string _filename; + private static char[] SEPARATOR = new char[] { ' ' }; + + List> _data = null; + private static Dictionary>> _tempCache = new Dictionary>>(); + + public ASCIIGridFile(string fileName) + { + this._filename = fileName; + _fileStream = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.Read); + _streamReader = new StreamReader(_fileStream, Encoding.ASCII); + } + public float GetElevationAtPoint(FileMetadata metadata, int x, int y) + { + if (_data == null) + { + ReadAllFile(metadata); + } + + string strXValue = _data[y][x]; + + float elevation = float.Parse(strXValue, CultureInfo.InvariantCulture); + return elevation; + + } + + private void ReadAllFile(FileMetadata metadata) + { + if (_tempCache.ContainsKey(_filename)) + { + _data = _tempCache[_filename]; + return; + } + string curLine = null; + _fileStream.Seek(0, SeekOrigin.Begin); + + // skip header + for (int i = 1; i <= 6 /* + (y - 1)*/; i++) + { + curLine = _streamReader.ReadLine(); + } + + _data = new List>(metadata.Height); + while (!_streamReader.EndOfStream) + { + var line = _streamReader.ReadLine().Trim(); + + var values = new List(metadata.Width); + var current = string.Empty; + foreach (char c in line) + { + if (c == ' ') + { + values.Add(current); + current = string.Empty; + } + else + { + current += c; + } + } + values.Add(current); + _data.Add(values); + } + _tempCache[_filename] = _data; + } + + #region IDisposable Support + private bool disposedValue = false; + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + _streamReader?.Dispose(); + _fileStream?.Dispose(); + } + + disposedValue = true; + } + } + + // Ce code est ajouté pour implémenter correctement le modèle supprimable. + public void Dispose() + { + Dispose(true); + } + + + #endregion + } +} diff --git a/src/SRTM/Sources/CGIAR/CGIARSource.cs b/src/SRTM/Sources/CGIAR/CGIARSource.cs new file mode 100644 index 0000000..8462e19 --- /dev/null +++ b/src/SRTM/Sources/CGIAR/CGIARSource.cs @@ -0,0 +1,50 @@ +// The MIT License (MIT) + +// Copyright (c) 2017 Alpine Chough Software, Ben Abelshausen + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using SRTM.Logging; + +namespace SRTM.Sources.CGIAR +{ + /// + /// Defines an CGIAR source of data for SRTMv4. + /// + public class CGIARSource : ISRTMSource + { + /// + /// The source of the data. + /// + public const string SOURCE = @"http://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/ASCII"; + + /// + /// Gets the missing cell. + /// + public bool GetMissingCell(string path, string name) + { + var filename = name + ".zip"; + var local = System.IO.Path.Combine(path, filename); + + var Logger = LogProvider.For(); + Logger.Info($"Downloading {name} ..."); + return SourceHelpers.Download(local, SOURCE + "/" + filename); + } + } +} \ No newline at end of file diff --git a/src/SRTM/Sources/CGIAR/FileMetadata.cs b/src/SRTM/Sources/CGIAR/FileMetadata.cs new file mode 100644 index 0000000..264ca3c --- /dev/null +++ b/src/SRTM/Sources/CGIAR/FileMetadata.cs @@ -0,0 +1,8 @@ +namespace SRTM.Sources.CGIAR +{ + public class FileMetadata + { + public int Height { get; set; } + public int Width { get; set; } + } +} \ No newline at end of file