Example #1
0
        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);
            }
        }
Example #2
0
 protected override byte[] LoadHgtDataFromFile(HgtCellCoords coords, string filePath)
 {
     using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         return(LoadHgtDataFromStream(fileStream));
     }
 }
Example #3
0
 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));
     }
 }
Example #4
0
        /// <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));
        }
Example #5
0
 private IHgtDataCell buildCellFor(HgtCellCoords coords)
 {
     try
     {
         return(_cellFactory.GetCellFor(coords));
     }
     catch (HgtFileException)
     {
         return(HgtDataCellInvalid.Invalid);
     }
 }
Example #6
0
        /// <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));
        }
Example #7
0
        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));
        }
Example #8
0
        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));
                }
            }
        }
Example #9
0
        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;
            }
        }
Example #10
0
 public string FindFilePath(HgtCellCoords coords)
 {
     return(_cache.GetOrAdd(coords, findPathForFile));
 }
Example #11
0
 protected override string coordsToFilename(HgtCellCoords coords)
 {
     return(coords.ToBaseName() + ".hgt");
 }
Example #12
0
 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);
Example #15
0
        public async Task <IHgtDataCell> GetCellForAsync(HgtCellCoords coords)
        {
            var data = await _loader.LoadFromFileAsync(coords);

            return(new HgtDataCellInMemory(data, HgtUtils.PointsPerCellFromDataLength(data.Length), coords));
        }
Example #16
0
 protected HgtFileException(HgtCellCoords coords, [NotNull] string message)
     : base(message)
 {
     _coords = coords;
 }
Example #17
0
 protected abstract string coordsToFilename(HgtCellCoords coords);
Example #18
0
 public Task <IHgtDataCell> GetCellForAsync(HgtCellCoords coords)
 {
     return(Task.FromResult(GetCellFor(coords)));
 }
Example #19
0
 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));
        }
Example #21
0
 internal HgtDataCellInFile([NotNull] FileStream file, int fileSize, HgtCellCoords coords) : base(fileSize, coords)
 {
     _file = file;
 }
Example #22
0
        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));
        }
Example #24
0
 public HgtFileException(HgtCellCoords coords, string message, Exception innerException)
     : base(message, innerException)
 {
     _coords = coords;
 }