Class: RGeo::CoordSys::SRSDatabase::UrlReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rgeo/coord_sys/srs_database/url_reader.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(opts_ = {}) ⇒ UrlReader

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.



62
63
64
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 62

def initialize(opts_={})
  @cache = opts_[:cache] ? {} : nil
end

Instance Method Details

#clear_cacheObject

Clear the cache if one is present.



96
97
98
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 96

def clear_cache
  @cache.clear if @cache
end

#get(ident_) ⇒ Object

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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