private string findPathForFile(HgtCellCoords coords) { string filename = coordsToFilename(coords); string[] potentialPaths = { Path.Combine(_directory, filename), Path.Combine(_directory, "Eurasia", filename), Path.Combine(_directory, "Africa", filename), Path.Combine(_directory, "South_America",filename), Path.Combine(_directory, "North_America",filename), Path.Combine(_directory, "Islands", filename), }; var path = potentialPaths.FirstOrDefault(File.Exists); if (path != null) { return(path); } var foundfile = new DirectoryInfo(_directory).EnumerateFiles(filename, SearchOption.AllDirectories) .FirstOrDefault(); if (foundfile != null) { return(foundfile.FullName); } else { throw new HgtFileNotFoundException(coords); } }
protected override byte[] LoadHgtDataFromFile(HgtCellCoords coords, string filePath) { using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { return(LoadHgtDataFromStream(fileStream)); } }
protected override async Task <byte[]> LoadHgtDataFromFileAsync(HgtCellCoords coords, string filePath) { using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { return(await LoadHgtDataFromStreamAsync(fileStream)); } }
/// <summary> /// Gets elevation above MSL /// </summary> /// <param name="latitude">Latitude in degrees in WGS84 datum</param> /// <param name="longitude">Longitude in degrees in WGS84 datum</param> /// <returns></returns> public double GetElevation(double latitude, double longitude) { var coords = HgtCellCoords.ForLatLon(latitude, longitude); var cell = _cache.GetOrAdd(coords, buildCellFor); return(cell.GetElevation(latitude, longitude)); }
private IHgtDataCell buildCellFor(HgtCellCoords coords) { try { return(_cellFactory.GetCellFor(coords)); } catch (HgtFileException) { return(HgtDataCellInvalid.Invalid); } }
/// <summary> /// Gets elevation above MSL /// </summary> /// <param name="latitude"></param> /// <param name="longitude"></param> /// <returns></returns> public Task <double> GetElevationAsync(double latitude, double longitude) { var coords = HgtCellCoords.ForLatLon(latitude, longitude); IHgtDataCell cellFromCache; if (_cache.TryGetValue(coords, out cellFromCache)) { cellFromCache.GetElevationAsync(latitude, longitude); } return(buildAndCacheCellAndReturnElevationAsync(coords, latitude, longitude)); }
private async Task <double> buildAndCacheCellAndReturnElevationAsync(HgtCellCoords coords, double latitude, double longitude) { IHgtDataCell ret; try { ret = await _cellFactory.GetCellForAsync(coords); } catch (HgtFileException) { ret = HgtDataCellInvalid.Invalid; } var cell = _cache.GetOrAdd(coords, ret); return(await cell.GetElevationAsync(latitude, longitude)); }
protected override async Task <byte[]> LoadHgtDataFromFileAsync(HgtCellCoords coords, string filePath) { using (var zipArchive = ZipFile.OpenRead(filePath)) { var entry = zipArchive.Entries.Single(); long length = entry.Length; if (!HgtUtils.IsDataLengthValid(length)) { throw new HgtFileInvalidException(coords, string.Format("Invalid length - {0} bytes", length)); } using (var zipStream = entry.Open()) { return(await LoadHgtDataFromStreamAsync(zipStream)); } } }
public IHgtDataCell GetCellFor(HgtCellCoords coords) { var path = _pathResolver.FindFilePath(coords); FileStream file = null; try { int fileSize = (int)new FileInfo(path).Length; file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read); return(new HgtDataCellInFile(file, HgtUtils.PointsPerCellFromDataLength(fileSize), coords)); } catch (Exception) { if (file != null) { file.Dispose(); } throw; } }
public string FindFilePath(HgtCellCoords coords) { return(_cache.GetOrAdd(coords, findPathForFile)); }
protected override string coordsToFilename(HgtCellCoords coords) { return(coords.ToBaseName() + ".hgt"); }
protected HgtDataCellBase(int pointsPerCell, HgtCellCoords coords) { _pointsPerCell = pointsPerCell; _coords = coords; }
protected abstract Task <byte[]> LoadHgtDataFromFileAsync(HgtCellCoords coords, [NotNull] string filePath);
protected abstract byte[] LoadHgtDataFromFile(HgtCellCoords coords, [NotNull] string filePath);
public async Task <IHgtDataCell> GetCellForAsync(HgtCellCoords coords) { var data = await _loader.LoadFromFileAsync(coords); return(new HgtDataCellInMemory(data, HgtUtils.PointsPerCellFromDataLength(data.Length), coords)); }
protected HgtFileException(HgtCellCoords coords, [NotNull] string message) : base(message) { _coords = coords; }
protected abstract string coordsToFilename(HgtCellCoords coords);
public Task <IHgtDataCell> GetCellForAsync(HgtCellCoords coords) { return(Task.FromResult(GetCellFor(coords))); }
internal HgtDataCellInMemory([NotNull] byte[] hgtData, int pointsPerCell, HgtCellCoords coords) : base(pointsPerCell, coords) { _hgtData = hgtData; }
public Task <byte[]> LoadFromFileAsync(HgtCellCoords coords) { var filePath = _pathResolver.FindFilePath(coords); return(LoadHgtDataFromFileAsync(coords, filePath)); }
internal HgtDataCellInFile([NotNull] FileStream file, int fileSize, HgtCellCoords coords) : base(fileSize, coords) { _file = file; }
public IHgtDataCell GetCellFor(HgtCellCoords coords) { var data = _loader.LoadFromFile(coords); return(new HgtDataCellInMemory(data, HgtUtils.PointsPerCellFromDataLength(data.Length), coords)); }
public byte[] LoadFromFile(HgtCellCoords coords) { var filePath = _pathResolver.FindFilePath(coords); return(LoadHgtDataFromFile(coords, filePath)); }
public HgtFileException(HgtCellCoords coords, string message, Exception innerException) : base(message, innerException) { _coords = coords; }