class GeoElevation::Srtm

Test:

EGM2008_URL = ‘localhost/Und_min2.5x2.5_egm2008_isw=82_WGS84_TideFree_SE.gz

Public Class Methods

new() click to toggle source
# File lib/geoelevation.rb, line 18
def initialize
    # Just in case...
    json = Retriever::prepare_folder
    # Dictionary with all files and urls (as is saved in ~/.elevations.rb/list.json)
    @files = JSON.load(json)
    @cached_srtm_files = {}
end

Public Instance Methods

find_file_name_and_url(file_name, srtm_version) click to toggle source
# File lib/geoelevation.rb, line 68
def find_file_name_and_url(file_name, srtm_version)
    for candidate_file_name in @files[srtm_version].keys
        if candidate_file_name.index(file_name) == 0
            return [candidate_file_name, "#{GeoElevation::SRTM_BASE_URL}#{@files[srtm_version][candidate_file_name]}"]
        end 
    end

    nil
end
get_elevation(latitude, longitude) click to toggle source
# File lib/geoelevation.rb, line 26
def get_elevation(latitude, longitude)
    srtm_file = get_file(latitude, longitude)

    if not srtm_file
        return nil
    end

    srtm_file.get_elevation(latitude, longitude)
end
get_file(latitude, longitude) click to toggle source
# File lib/geoelevation.rb, line 36
def get_file(latitude, longitude)
    file_name = get_file_name(latitude, longitude)

    if @cached_srtm_files.has_key?(file_name)
        return @cached_srtm_files[file_name]
    end

    file, url = find_file_name_and_url(file_name, 'srtm1')
    if ! file && ! url
        file, url = find_file_name_and_url(file_name, 'srtm3')
    end
    if ! file && ! url
        @cached_srtm_files[file_name] = nil
        return nil
    end

    file_name = file.sub('.zip', '')
    local_file_name = File.join(GeoElevation::DIR_NAME, file_name)
    if ! File.exist?(local_file_name)
        puts "Retrieving #{file_name} because #{local_file_name} not found"
        file_contents = open(url).read
        file_contents = GeoElevation::Utils::unzip(file_contents, file_name)
        open(local_file_name, 'wb').write(file_contents)
    end

    file = GeoElevation::SrtmFile.new(local_file_name)

    @cached_srtm_files[file_name] = file

    file
end
get_file_name(latitude, longitude) click to toggle source

Return the file name no matter if the actual SRTM file exists.

# File lib/geoelevation.rb, line 81
def get_file_name(latitude, longitude)
    north_south = latitude >= 0 ? 'N' : 'S'
    east_west = longitude >= 0 ? 'E' : 'W'

    lat = latitude.floor.to_i.abs.to_s.rjust(2, '0')
    lon = longitude.floor.to_i.abs.to_s.rjust(3, '0')

    "#{north_south}#{lat}#{east_west}#{lon}.hgt"
end