class RGeo::CoordSys::SRSDatabase::Proj4Data

A spatial reference database implementation backed by coordinate system files installed as part of the proj4 library. For a given Proj4Data object, you specify a single file (e.g. the epsg data file), and you can retrieve records by ID number.

Public Class Methods

new(filename_, opts_={}) click to toggle source

Connect to one of the proj4 data files. You should provide the file name, optionally the installation directory if it is not in a typical location, and several additional options.

These options are recognized:

:dir

The path for the share/proj directory that contains the requested data file. By default, the Proj4Data class will try a number of directories for you, including /usr/local/share/proj, /opt/local/share/proj, /usr/share/proj, and a few other variants. However, if you have proj4 installed elsewhere, you can provide an explicit directory using this option. You may also pass nil as the value, in which case all the normal lookup paths will be disabled, and you will have to provide the full path as the file name.

:cache

If set to true, this class caches previously looked up entries so subsequent lookups do not have to reread the file. If set to :read_all, then ALL values in the file are read in and cached the first time a lookup is done. If set to :preload, then ALL values in the file are read in immediately when the database is created. Default is false, indicating that the file will be reread on every lookup.

:authority

If set, its value is taken as the authority name for all entries. The authority code will be set to the identifier. If not set, then the authority fields of entries will be blank.

# File lib/rgeo/coord_sys/srs_database/proj4_data.rb, line 81
def initialize(filename_, opts_={})
  dir_ = nil
  if opts_.include?(:dir)
    dir_ = opts_[:dir]
  else
    ['/usr/local/share/proj', '/usr/local/proj/share/proj', '/usr/local/proj4/share/proj', '/opt/local/share/proj', '/opt/proj/share/proj', '/opt/proj4/share/proj', '/opt/share/proj', '/usr/share/proj'].each do |d_|
      if ::File.directory?(d_) && ::File.readable?(d_)
        dir_ = d_
        break
      end
    end
  end
  @path = dir_ ? "#{dir_}/#{filename_}" : filename_
  @authority = opts_[:authority]
  if opts_[:cache]
    @cache = {}
    case opts_[:cache]
    when :read_all
      @populate_state = 1
    when :preload
      _search_file(nil)
      @populate_state = 2
    else
      @populate_state = 0
    end
  else
    @cache = nil
    @populate_state = 0
  end
end

Public Instance Methods

clear_cache() click to toggle source

Clear the cache if one exists.

# File lib/rgeo/coord_sys/srs_database/proj4_data.rb, line 134
def clear_cache
  @cache.clear if @cache
  @populate_state = 1 if @populate_state == 2
end
get(ident_) click to toggle source

Retrieve the Entry for the given ID number.

# File lib/rgeo/coord_sys/srs_database/proj4_data.rb, line 115
def get(ident_)
  ident_ = ident_.to_s
  return @cache[ident_] if @cache && @cache.include?(ident_)
  result_ = nil
  if @populate_state == 0
    data_ = _search_file(ident_)
    result_ = Entry.new(ident_, :authority => @authority, :authority_code => @authority ? ident_ : nil, :name => data_[1], :proj4 => data_[2]) if data_
    @cache[ident_] = result_ if @cache
  elsif @populate_state == 1
    _search_file(nil)
    result_ = @cache[ident_]
    @populate_state = 2
  end
  result_
end