class RGeo::CoordSys::SRSDatabase::UrlReader

A spatial reference database implementation that fetches data from internet URLs.

Public Class Methods

new(opts_={}) click to toggle source

Create a URL-based spatial reference database.

Options:

:cache

If set to true, lookup results are cached so if the same URL is requested again, the result is served from cache rather than issuing another HTTP request. Default is false.

# File lib/rgeo/coord_sys/srs_database/url_reader.rb, line 62
def initialize(opts_={})
  @cache = opts_[:cache] ? {} : nil
end

Public Instance Methods

clear_cache() click to toggle source

Clear the cache if one is present.

# File lib/rgeo/coord_sys/srs_database/url_reader.rb, line 96
def clear_cache
  @cache.clear if @cache
end
get(ident_) click to toggle source

Retrieve the given URL and return an Entry. Returns nil if the URL cannot be read as an OGC WKT or Proj4 coordinate system

# File lib/rgeo/coord_sys/srs_database/url_reader.rb, line 71
def get(ident_)
  ident_ = ident_.to_s
  return @cache[ident_] if @cache && @cache.include?(ident_)
  uri_ = ::URI.parse(ident_)
  result_ = nil
  ::Net::HTTP.start(uri_.host, uri_.port) do |http_|
    request_ = uri_.path
    request_ = "#{request_}?#{uri_.query}" if uri_.query
    response_ = http_.request_get(request_)
    if response_.kind_of?(::Net::HTTPSuccess)
      response_ = response_.body.strip
      if response_[0,1] == '+'
        result_ = Entry.new(ident_, :proj4 => response_)
      else
        result_ = Entry.new(ident_, :coord_sys => response_)
      end
    end
  end
  @cache[ident_] = result_ if @cache
  result_
end