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.



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

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.



33
34
35
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 33

def catalog
  @catalog
end

Instance Method Details

#clear_cacheObject

Clear the cache if one exists.



56
57
58
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 56

def clear_cache
  @cache.clear if @cache
end

#get(ident_) ⇒ Object

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



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

def get(ident_)
  ident_ = ident_.to_s
  return @cache[ident_] if @cache && @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