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.


27
28
29
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 27

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

Instance Method Details

#clear_cacheObject

Clear the cache if one is present.


59
60
61
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 59

def clear_cache
  @cache&.clear
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


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rgeo/coord_sys/srs_database/url_reader.rb', line 35

def get(ident)
  ident = ident.to_s
  return @cache[ident] if @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.requestget(request)
    if response.is_a?(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