From 3c233ba4a757296494500f5050a9fb393a4472e2 Mon Sep 17 00:00:00 2001 From: Kurniawan Jatmika Date: Sat, 2 Oct 2021 16:17:19 +0700 Subject: [PATCH 1/3] adding cached credentials method and username & password storing in file --- .gitignore | 3 +- .../PublishProfiles/FolderProfile.pubxml | 12 ++ src/SRTM/Sources/SourceHelpers.cs | 143 +++++++++++----- test/SRTM.Tests.Functional/Program.cs | 157 +++++++++--------- test/SRTM.Tests.Functional/SettingReader.cs | 35 ++++ 5 files changed, 232 insertions(+), 118 deletions(-) create mode 100644 src/SRTM/Properties/PublishProfiles/FolderProfile.pubxml create mode 100644 test/SRTM.Tests.Functional/SettingReader.cs diff --git a/.gitignore b/.gitignore index be174e8..04d6ab0 100644 --- a/.gitignore +++ b/.gitignore @@ -51,4 +51,5 @@ TestResult*.xml srtm-cache*/ -islands_*.geojson \ No newline at end of file +islands_*.geojson +*.dat diff --git a/src/SRTM/Properties/PublishProfiles/FolderProfile.pubxml b/src/SRTM/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..7d5583d --- /dev/null +++ b/src/SRTM/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,12 @@ + + + + + Release + Any CPU + bin\Release\netstandard2.0\publish\ + FileSystem + + \ No newline at end of file diff --git a/src/SRTM/Sources/SourceHelpers.cs b/src/SRTM/Sources/SourceHelpers.cs index b583fb4..9ed0e18 100644 --- a/src/SRTM/Sources/SourceHelpers.cs +++ b/src/SRTM/Sources/SourceHelpers.cs @@ -6,54 +6,115 @@ namespace SRTM.Sources { - public class SourceHelpers + public class SourceHelpers + { + /// + /// Downloads a remote file and stores the data in the local one. + /// + public static bool Download(string local, string remote, bool logErrors = false) { - /// - /// Downloads a remote file and stores the data in the local one. - /// - public static bool Download(string local, string remote, bool logErrors = false) + var client = new HttpClient(); + return PerformDownload(client, local, remote, logErrors); + } + + /// + /// Downloads a remote file and stores the data in the local one. The given credentials are used for authorization. + /// + 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)); + + 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); + } + //public static bool DownloadWithCredentials2(NetworkCredential credentials, string local, string remote, bool logErrors = false) + //{ + // var Logger = LogProvider.For(); + // logErrors = true; + + // // 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)); + + // try + // { + // if (File.Exists(local)) + // { + // File.Delete(local); + // } + + // // Execute the request + // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remote); + // request.Method = "GET"; + // request.Credentials = cache; + // request.CookieContainer = myContainer; + // request.PreAuthenticate = false; + // request.AllowAutoRedirect = true; + + // // Now access the data + // using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) + // using (Stream stream = response.GetResponseStream()) + // using (StreamReader reader = new StreamReader(stream)) + // using (var outputStream = File.OpenWrite(local)) + // { + // stream.CopyTo(outputStream); + // } + + // return true; + // } + // catch (Exception ex) + // { + // if (logErrors) + // { + // Logger.ErrorException("Download failed.", ex); + // } + // } + // return false; + //} + + private static bool PerformDownload(HttpClient client, string local, string remote, bool logErrors = false) + { + var Logger = LogProvider.For(); + //logErrors = true; + + try + { + if (File.Exists(local)) { - var client = new HttpClient(); - return PerformDownload(client, local, remote, logErrors); + File.Delete(local); } - /// - /// Downloads a remote file and stores the data in the local one. The given credentials are used for authorization. - /// - 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(); - - 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; } + } } diff --git a/test/SRTM.Tests.Functional/Program.cs b/test/SRTM.Tests.Functional/Program.cs index 23d8cdc..40eb03e 100644 --- a/test/SRTM.Tests.Functional/Program.cs +++ b/test/SRTM.Tests.Functional/Program.cs @@ -23,85 +23,90 @@ 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; + + 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()); + + 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 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."); } + } } diff --git a/test/SRTM.Tests.Functional/SettingReader.cs b/test/SRTM.Tests.Functional/SettingReader.cs new file mode 100644 index 0000000..03e9ac1 --- /dev/null +++ b/test/SRTM.Tests.Functional/SettingReader.cs @@ -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 + { + + /// + /// Read a setting from a predefined file + /// + /// + /// + /// + 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; + } + } +} \ No newline at end of file From e929547b42a86c38bcec04c99503b980550a1715 Mon Sep 17 00:00:00 2001 From: Kurniawan Jatmika Date: Sat, 2 Oct 2021 16:18:12 +0700 Subject: [PATCH 2/3] remarking & cleanups --- src/SRTM/Sources/SourceHelpers.cs | 49 +------------------------------ 1 file changed, 1 insertion(+), 48 deletions(-) diff --git a/src/SRTM/Sources/SourceHelpers.cs b/src/SRTM/Sources/SourceHelpers.cs index 9ed0e18..f2c1585 100644 --- a/src/SRTM/Sources/SourceHelpers.cs +++ b/src/SRTM/Sources/SourceHelpers.cs @@ -30,6 +30,7 @@ public static bool DownloadWithCredentials(NetworkCredential credentials, string 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; @@ -40,58 +41,10 @@ public static bool DownloadWithCredentials(NetworkCredential credentials, string return PerformDownload(client, local, remote, logErrors); } - //public static bool DownloadWithCredentials2(NetworkCredential credentials, string local, string remote, bool logErrors = false) - //{ - // var Logger = LogProvider.For(); - // logErrors = true; - - // // 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)); - - // try - // { - // if (File.Exists(local)) - // { - // File.Delete(local); - // } - - // // Execute the request - // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(remote); - // request.Method = "GET"; - // request.Credentials = cache; - // request.CookieContainer = myContainer; - // request.PreAuthenticate = false; - // request.AllowAutoRedirect = true; - - // // Now access the data - // using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) - // using (Stream stream = response.GetResponseStream()) - // using (StreamReader reader = new StreamReader(stream)) - // using (var outputStream = File.OpenWrite(local)) - // { - // stream.CopyTo(outputStream); - // } - - // return true; - // } - // catch (Exception ex) - // { - // if (logErrors) - // { - // Logger.ErrorException("Download failed.", ex); - // } - // } - // return false; - //} private static bool PerformDownload(HttpClient client, string local, string remote, bool logErrors = false) { var Logger = LogProvider.For(); - //logErrors = true; try { From b0f1db77a85c5f7106531e665b334e617b174ce1 Mon Sep 17 00:00:00 2001 From: Kurniawan Jatmika Date: Sat, 2 Oct 2021 16:43:07 +0700 Subject: [PATCH 3/3] increasing version and repointing test using NASA source (USGS link is broken) --- src/SRTM/SRTM.csproj | 2 +- test/SRTM.Tests.Functional/Program.cs | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/SRTM/SRTM.csproj b/src/SRTM/SRTM.csproj index 8fc8364..bdfaffe 100644 --- a/src/SRTM/SRTM.csproj +++ b/src/SRTM/SRTM.csproj @@ -10,7 +10,7 @@ LIBLOG_PORTABLE - 0.0.7 + 0.0.8 Ben Abelshausen Itinero A library to read SRTM data, loads missing files on-the-fly. diff --git a/test/SRTM.Tests.Functional/Program.cs b/test/SRTM.Tests.Functional/Program.cs index 40eb03e..7a3237a 100644 --- a/test/SRTM.Tests.Functional/Program.cs +++ b/test/SRTM.Tests.Functional/Program.cs @@ -37,20 +37,21 @@ static void Main(string[] args) .CreateLogger(); Log.Logger = log; - USGSTest(); - //BilinearInterpolationTest(); + NASATest(); + //BilinearInterpolationTest(); //USGS is broken Console.WriteLine("Testing finished."); Console.ReadLine(); } - static void USGSTest() + static void NASATest() { - Console.WriteLine("Start USGSTest."); + Console.WriteLine("Start NASATest."); - // https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/ + //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)); @@ -60,20 +61,20 @@ static void USGSTest() 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? 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); + 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); + int? elevationNamibia1 = srtmData.GetElevation(-20, 19.89597); + Console.WriteLine("Elevation of namibia1 returns {0}", elevationNamibia1); - Console.WriteLine("End USGSTest."); + Console.WriteLine("End NASATest."); } static void BilinearInterpolationTest()