Skip to content

Commit e4ec593

Browse files
committed
Add logging for file read failures
1 parent 286fcc5 commit e4ec593

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/InMemoryElevationProvider.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
5-
using System.Linq;
65
using System.Threading.Tasks;
76
using ICSharpCode.SharpZipLib.Core;
87
using Microsoft.AspNetCore.Hosting;
@@ -19,7 +18,7 @@ public class InMemoryElevationProvider : IElevationProvider
1918
{
2019
private readonly ILogger<InMemoryElevationProvider> _logger;
2120
private readonly IFileProvider _fileProvider;
22-
private readonly ConcurrentDictionary<string, Task<BytesAndSamples>> _initializationTaskPerLatLng;
21+
private readonly ConcurrentDictionary<Coordinate, Task<BytesAndSamples>> _initializationTaskPerLatLng;
2322

2423
/// <summary>
2524
/// Constructor
@@ -51,13 +50,21 @@ public async Task Initialize()
5150
continue;
5251
}
5352
var key = ElevationHelper.FileNameToKey(hgtFile.Name);
54-
_initializationTaskPerLatLng[key.ToString()] = Task.Run(() =>
53+
_initializationTaskPerLatLng[key] = Task.Run(() =>
5554
{
56-
var stream = hgtFile.CreateReadStream();
57-
using var memoryStream = new MemoryStream();
58-
StreamUtils.Copy(stream, memoryStream, new byte[4096]);
59-
var bytes = memoryStream.ToArray();
60-
return new BytesAndSamples(bytes, ElevationHelper.SamplesFromLength(bytes.Length));
55+
try
56+
{
57+
var stream = hgtFile.CreateReadStream();
58+
using var memoryStream = new MemoryStream();
59+
StreamUtils.Copy(stream, memoryStream, new byte[4096]);
60+
var bytes = memoryStream.ToArray();
61+
return new BytesAndSamples(bytes, ElevationHelper.SamplesFromLength(bytes.Length));
62+
}
63+
catch (Exception ex)
64+
{
65+
_logger.LogWarning($"Failed to read file {hgtFile} hgtFile, {ex.Message}");
66+
throw;
67+
}
6168
});
6269
}
6370

@@ -72,14 +79,13 @@ public async Task<double[]> GetElevation(double[][] latLngs)
7279
foreach (var latLng in latLngs)
7380
{
7481
var key = new Coordinate(Math.Floor(latLng[0]), Math.Floor(latLng[1]));
75-
if (_initializationTaskPerLatLng.ContainsKey(key.ToString()) == false)
82+
if (_initializationTaskPerLatLng.ContainsKey(key) == false)
7683
{
77-
_logger.LogWarning($"Unable to find elevation file key: {key}, keys are {string.Join(";",_initializationTaskPerLatLng.Keys)}");
7884
elevation.Add(0);
7985
continue;
8086
}
8187

82-
var info = await _initializationTaskPerLatLng[key.ToString()];
88+
var info = await _initializationTaskPerLatLng[key];
8389

8490
var exactLocation = new Coordinate(Math.Abs(latLng[0] - key.X) * (info.Samples - 1),
8591
(1 - Math.Abs(latLng[1] - key.Y)) * (info.Samples - 1));

0 commit comments

Comments
 (0)