Class: RGeo::CoordSys::SRSDatabase::SrOrg

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

Overview

A spatial reference database implementation that fetches data from the spatialreference.org website.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog, opts = {}) ⇒ SrOrg

Create a database backed by the given catalog of the spatialreference.org website. Catalogs currently supported by spatialreference.org are “epsg”, “esri”, “iau2000” and “sr-org”.

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.


29
30
31
32
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 29

def initialize(catalog, opts = {})
  @catalog = catalog.to_s.downcase
  @cache = opts[:cache] ? {} : nil
end

Instance Attribute Details

#catalogObject (readonly)

The spatialreference.org catalog used by this database.


35
36
37
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 35

def catalog
  @catalog
end

Instance Method Details

#clear_cacheObject

Clear the cache if one exists.


58
59
60
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 58

def clear_cache
  @cache&.clear
end

#get(ident) ⇒ Object

Retrieve the Entry from a spatialreference.org catalog given an integer ID.


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 40

def get(ident)
  ident = ident.to_s
  return @cache[ident] if @cache&.include?(ident)
  coord_sys = nil
  proj4 = nil
  Net::HTTP.start("spatialreference.org") do |http|
    response = http.request_get("/ref/#{@catalog}/#{ident}/ogcwkt/")
    coord_sys = response.body if response.is_a?(Net::HTTPSuccess)
    response = http.request_get("/ref/#{@catalog}/#{ident}/proj4/")
    proj4 = response.body if response.is_a?(Net::HTTPSuccess)
  end
  result = Entry.new(ident, coord_sys: coord_sys.strip, proj4: proj4.strip)
  @cache[ident] = result if @cache
  result
end