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/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/src/SRTM/Sources/SourceHelpers.cs b/src/SRTM/Sources/SourceHelpers.cs index b583fb4..f2c1585 100644 --- a/src/SRTM/Sources/SourceHelpers.cs +++ b/src/SRTM/Sources/SourceHelpers.cs @@ -6,54 +6,68 @@ 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)); + + // 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(); + + 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..7a3237a 100644 --- a/test/SRTM.Tests.Functional/Program.cs +++ b/test/SRTM.Tests.Functional/Program.cs @@ -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."); } + } } 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