Skip to content
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

Develop cached credentials #13

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ TestResult*.xml
srtm-cache*/


islands_*.geojson
islands_*.geojson
*.dat
12 changes: 12 additions & 0 deletions src/SRTM/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\netstandard2.0\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/SRTM/SRTM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<PropertyGroup>
<DefineConstants>LIBLOG_PORTABLE</DefineConstants>
<Version>0.0.7</Version>
<Version>0.0.8</Version>
<Authors>Ben Abelshausen</Authors>
<Company>Itinero</Company>
<Description>A library to read SRTM data, loads missing files on-the-fly.</Description>
Expand Down
96 changes: 55 additions & 41 deletions src/SRTM/Sources/SourceHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,68 @@

namespace SRTM.Sources
{
public class SourceHelpers
public class SourceHelpers
{
/// <summary>
/// Downloads a remote file and stores the data in the local one.
/// </summary>
public static bool Download(string local, string remote, bool logErrors = false)
{
/// <summary>
/// Downloads a remote file and stores the data in the local one.
/// </summary>
public static bool Download(string local, string remote, bool logErrors = false)
var client = new HttpClient();
return PerformDownload(client, local, remote, logErrors);
}

/// <summary>
/// Downloads a remote file and stores the data in the local one. The given credentials are used for authorization.
/// </summary>
public static bool DownloadWithCredentials(NetworkCredential credentials, string local, string remote,
bool logErrors = false)
{
// Ideally the cookie container will be persisted to/from file
CookieContainer myContainer = new CookieContainer();

// Create a credential cache for authenticating when redirected to Earthdata Login
CredentialCache cache = new CredentialCache();
cache.Add(new Uri(credentials.Domain), "Basic", new NetworkCredential(credentials.UserName, credentials.Password));

// Modify the simple client handler to using cached Credentials
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = cache;
handler.CookieContainer = myContainer;
handler.PreAuthenticate = false;
handler.AllowAutoRedirect = true;

var client = new HttpClient(handler);

return PerformDownload(client, local, remote, logErrors);
}

private static bool PerformDownload(HttpClient client, string local, string remote, bool logErrors = false)
{
var Logger = LogProvider.For<SourceHelpers>();

try
{
if (File.Exists(local))
{
var client = new HttpClient();
return PerformDownload(client, local, remote, logErrors);
File.Delete(local);
}

/// <summary>
/// Downloads a remote file and stores the data in the local one. The given credentials are used for authorization.
/// </summary>
public static bool DownloadWithCredentials(NetworkCredential credentials, string local, string remote,
bool logErrors = false)
using (var stream = client.GetStreamAsync(remote).Result)
using (var outputStream = File.OpenWrite(local))
{
HttpClientHandler handler = new HttpClientHandler {Credentials = credentials};
var client = new HttpClient(handler);
return PerformDownload(client, local, remote, logErrors);
stream.CopyTo(outputStream);
}

private static bool PerformDownload(HttpClient client, string local, string remote, bool logErrors = false)
return true;
}
catch (Exception ex)
{
if (logErrors)
{
var Logger = LogProvider.For<SourceHelpers>();

try
{
if (File.Exists(local))
{
File.Delete(local);
}

using (var stream = client.GetStreamAsync(remote).Result)
using (var outputStream = File.OpenWrite(local))
{
stream.CopyTo(outputStream);
}
return true;
}
catch (Exception ex)
{
if (logErrors)
{
Logger.ErrorException("Download failed.", ex);
}
}
return false;
Logger.ErrorException("Download failed.", ex);
}
}
return false;
}
}
}
158 changes: 82 additions & 76 deletions test/SRTM.Tests.Functional/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,85 +23,91 @@
using Serilog;
using System;
using SRTM.Sources.USGS;
using System.Net;
using SRTM.Sources.NASA;

namespace SRTM.Tests.Functional
{
class Program
class Program
{
static void Main(string[] args)
{
static void Main(string[] args)
{
var log = new LoggerConfiguration()
.WriteTo.ColoredConsole(outputTemplate: "{Timestamp:HH:mm} [{Level}] ({Name:l}) {Message}{NewLine}{Exception}")
.CreateLogger();
Log.Logger = log;

USGSTest();
BilinearInterpolationTest();

Console.WriteLine("Testing finished.");
Console.ReadLine();
}

static void USGSTest()
{
Console.WriteLine("Start USGSTest.");

// https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/
var srtmData = new SRTMData(@"srtm-cache", new USGSSource());

int? elevationInnsbruck = srtmData.GetElevation(47.267222, 11.392778);
Console.WriteLine("Elevation of Innsbruck: {0}m", elevationInnsbruck);

int? elevationLaPaz = srtmData.GetElevation(-16.5, -68.15);
Console.WriteLine("Elevation of La Paz: {0}m", elevationLaPaz);

int? elevationKathmandu = srtmData.GetElevation(27.702983735525862f, 85.2978515625f);
Console.WriteLine("Elevation of Kathmandu {0}m", elevationKathmandu);

int? elevationHanoi = srtmData.GetElevation(21.030673628606102f, 105.853271484375f);
Console.WriteLine("Elevation of Ha Noi {0}m", elevationHanoi);

// tries to get elevation from an empty cell.
int? elevationSomeplace1 = srtmData.GetElevation(52.02237f, 2.55853224f);
Console.WriteLine("Elevation of nowhere returns {0}", elevationSomeplace1);

int? elevationNamibia1 = srtmData.GetElevation(-20, 19.89597);
Console.WriteLine("Elevation of namibia1 returns {0}", elevationNamibia1);

Console.WriteLine("End USGSTest.");
}

static void BilinearInterpolationTest()
{
Console.WriteLine("Start BilinearInterpolationTest.");

// https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/
var srtmData = new SRTMData(@"srtm-cache", new USGSSource());

double? elevationInnsbruck = srtmData.GetElevationBilinear(47.267222, 11.392778);
Console.WriteLine("Bilinear elevation of Innsbruck: {0}m", elevationInnsbruck);

double? elevationLaPaz = srtmData.GetElevationBilinear(-16.5, -68.15);
Console.WriteLine("Elevation of La Paz: {0}m", elevationLaPaz);

double? elevationKathmandu = srtmData.GetElevationBilinear(27.702983735525862f, 85.2978515625f);
Console.WriteLine("Elevation of Kathmandu {0}m", elevationKathmandu);

double? elevationHanoi = srtmData.GetElevationBilinear(21.030673628606102f, 105.853271484375f);
Console.WriteLine("Elevation of Ha Noi {0}m", elevationHanoi);

// tries to get elevation from an empty cell.
double? elevationSomeplace1 = srtmData.GetElevationBilinear(52.02237f, 2.55853224f);
Console.WriteLine("Elevation of nowhere returns {0}", elevationSomeplace1);

double? elevationNamibia1 = srtmData.GetElevationBilinear(-20, 19.89597);
Console.WriteLine("Elevation of namibia1 returns {0}", elevationNamibia1);

//Testing interpolation across cell edges
double? elevationOxted = srtmData.GetElevationBilinear(51.2525, 0.00001);
Console.WriteLine("Elevation of Oxted {0}m", elevationOxted);

Console.WriteLine("End BilinearInterpolationTest.");
}
var log = new LoggerConfiguration()
.WriteTo.ColoredConsole(outputTemplate: "{Timestamp:HH:mm} [{Level}] ({Name:l}) {Message}{NewLine}{Exception}")
.CreateLogger();
Log.Logger = log;

NASATest();
//BilinearInterpolationTest(); //USGS is broken

Console.WriteLine("Testing finished.");
Console.ReadLine();
}

static void NASATest()
{
Console.WriteLine("Start NASATest.");

//https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/
//var srtmData = new SRTMData(@"srtm-cache", new USGSSource());

//put username.dat & password.dat in executable directory using plain text (no encryption yet)
var credentials = new NetworkCredential(SettingReader.ReadSettingFile("username.dat"), SettingReader.ReadSettingFile("password.dat"), @"https://urs.earthdata.nasa.gov");
var srtmData = new SRTMData(@"srtm-cache", new NASASource(credentials));

int? elevationInnsbruck = srtmData.GetElevation(47.267222, 11.392778);
Console.WriteLine("Elevation of Innsbruck: {0}m", elevationInnsbruck);

int? elevationLaPaz = srtmData.GetElevation(-16.5, -68.15);
Console.WriteLine("Elevation of La Paz: {0}m", elevationLaPaz);

int? elevationKathmandu = srtmData.GetElevation(27.702983735525862f, 85.2978515625f);
Console.WriteLine("Elevation of Kathmandu {0}m", elevationKathmandu);

int? elevationHanoi = srtmData.GetElevation(21.030673628606102f, 105.853271484375f);
Console.WriteLine("Elevation of Ha Noi {0}m", elevationHanoi);

// tries to get elevation from an empty cell.
int? elevationSomeplace1 = srtmData.GetElevation(52.02237f, 2.55853224f);
Console.WriteLine("Elevation of nowhere returns {0}", elevationSomeplace1);

int? elevationNamibia1 = srtmData.GetElevation(-20, 19.89597);
Console.WriteLine("Elevation of namibia1 returns {0}", elevationNamibia1);

Console.WriteLine("End NASATest.");
}

static void BilinearInterpolationTest()
{
Console.WriteLine("Start BilinearInterpolationTest.");

// https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/
var srtmData = new SRTMData(@"srtm-cache", new USGSSource());

double? elevationInnsbruck = srtmData.GetElevationBilinear(47.267222, 11.392778);
Console.WriteLine("Bilinear elevation of Innsbruck: {0}m", elevationInnsbruck);

double? elevationLaPaz = srtmData.GetElevationBilinear(-16.5, -68.15);
Console.WriteLine("Elevation of La Paz: {0}m", elevationLaPaz);

double? elevationKathmandu = srtmData.GetElevationBilinear(27.702983735525862f, 85.2978515625f);
Console.WriteLine("Elevation of Kathmandu {0}m", elevationKathmandu);

double? elevationHanoi = srtmData.GetElevationBilinear(21.030673628606102f, 105.853271484375f);
Console.WriteLine("Elevation of Ha Noi {0}m", elevationHanoi);

// tries to get elevation from an empty cell.
double? elevationSomeplace1 = srtmData.GetElevationBilinear(52.02237f, 2.55853224f);
Console.WriteLine("Elevation of nowhere returns {0}", elevationSomeplace1);

double? elevationNamibia1 = srtmData.GetElevationBilinear(-20, 19.89597);
Console.WriteLine("Elevation of namibia1 returns {0}", elevationNamibia1);

//Testing interpolation across cell edges
double? elevationOxted = srtmData.GetElevationBilinear(51.2525, 0.00001);
Console.WriteLine("Elevation of Oxted {0}m", elevationOxted);

Console.WriteLine("End BilinearInterpolationTest.");
}
}
}
35 changes: 35 additions & 0 deletions test/SRTM.Tests.Functional/SettingReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.IO;

namespace SRTM.Tests.Functional
{
/// Author : Kurniawan
/// Desc : Class untuk membaca override setting project per file
/// Modified : 2018-09-03
public class SettingReader
{

/// <summary>
/// Read a setting from a predefined file
/// </summary>
/// <param name="file_name"></param>
/// <param name="default_value"></param>
/// <returns></returns>
public static string ReadSettingFile(string file_name, string default_value = "")
{
string setting = default_value;
string setting_file_location = AppDomain.CurrentDomain.BaseDirectory + file_name;
try
{
setting = File.ReadAllText(setting_file_location);
}
catch (Exception)
{
//Console.WriteLine(e.ToString());
Console.Write("console:message >> ");
Console.WriteLine(setting_file_location + " not found");
}
return setting;
}
}
}