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.



64
65
66
67
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 64

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.



71
72
73
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 71

def catalog
  @catalog
end

Instance Method Details

#clear_cacheObject

Clear the cache if one exists.



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

def clear_cache
  @cache.clear if @cache
end

#get(ident_) ⇒ Object

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rgeo/coord_sys/srs_database/sr_org.rb', line 77

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_.kind_of?(::Net::HTTPSuccess)
    response_ = http_.request_get("/ref/#{@catalog}/#{ident_}/proj4/")
    proj4_ = response_.body if response_.kind_of?(::Net::HTTPSuccess)
  end
  result_ = Entry.new(ident_, :coord_sys => coord_sys_.strip, :proj4 => proj4_.strip)
  @cache[ident_] = result_ if @cache
  result_
end