Skip to content

Initial commit for SRTMv4 usage. #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/SRTM/SRTM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Sources\CGIAR\" />
</ItemGroup>

</Project>
119 changes: 119 additions & 0 deletions src/SRTM/Sources/CGIAR/AsciiGridFile.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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
/// </summary>
public class ASCIIGridFile
{
private FileStream _fileStream;
private StreamReader _streamReader;
private readonly string _filename;
private static char[] SEPARATOR = new char[] { ' ' };

List<List<string>> _data = null;
private static Dictionary<string, List<List<string>>> _tempCache = new Dictionary<string, List<List<string>>>();

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<List<string>>(metadata.Height);
while (!_streamReader.EndOfStream)
{
var line = _streamReader.ReadLine().Trim();

var values = new List<string>(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
}
}
50 changes: 50 additions & 0 deletions src/SRTM/Sources/CGIAR/CGIARSource.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Defines an CGIAR source of data for SRTMv4.
/// </summary>
public class CGIARSource : ISRTMSource
{
/// <summary>
/// The source of the data.
/// </summary>
public const string SOURCE = @"http://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/ASCII";

/// <summary>
/// Gets the missing cell.
/// </summary>
public bool GetMissingCell(string path, string name)
{
var filename = name + ".zip";
var local = System.IO.Path.Combine(path, filename);

var Logger = LogProvider.For<SRTMData>();
Logger.Info($"Downloading {name} ...");
return SourceHelpers.Download(local, SOURCE + "/" + filename);
}
}
}
8 changes: 8 additions & 0 deletions src/SRTM/Sources/CGIAR/FileMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SRTM.Sources.CGIAR
{
public class FileMetadata
{
public int Height { get; set; }
public int Width { get; set; }
}
}