class EnrichmentDb::Geo::Region

Constants

DATABASE_NAME

Attributes

id[R]
name[R]

Public Class Methods

by_boundary(values) click to toggle source
# File lib/enrichment_db/geo/region.rb, line 29
def self.by_boundary(values)
  puts "Finding #{object_type} intersecting with some geohash"
  field_names = fields
  table = make_table_name
  query = "SELECT #{field_names} FROM #{DATABASE_NAME}.#{table} where ST_Within(ST_SetSRID(ST_GeomFromGeoHash($1), 4283), boundary)"

  begin
    result = EnrichmentDb.request(DATABASE_NAME, query, values)

    if result.ntuples == 1 
      puts "Found #{object_type}"
      result[0]
    else 
      puts "Nothing found"
      nil
    end
  rescue PG::InternalError => e
    # This usually is GEOSContains: TopologyException: side location conflict at #{lat} #{long}
    LexerAPI.error e.class
    LexerAPI.error e.message
    return nil
  end
end
by_id(id) click to toggle source
# File lib/enrichment_db/geo/region.rb, line 12
def self.by_id(id)
  puts "Finding #{object_type} with id = '#{id}'."
  field_names = fields
  table = make_table_name
  query = "SELECT #{field_names} FROM #{DATABASE_NAME}.#{table} where id = $1"
  values = [id]
  
  result = EnrichmentDb.request(DATABASE_NAME, query, values)
  if result.ntuples == 1 
    puts "Found #{object_type}"
    result[0]
  else 
    puts "Nothing found"
    nil
  end
end
new(data) click to toggle source
# File lib/enrichment_db/geo/region.rb, line 7
def initialize(data)
  @id = data['id']
  @name = data['name']
end

Private Class Methods

make_table_name() click to toggle source
# File lib/enrichment_db/geo/region.rb, line 59
def self.make_table_name
  "#{object_type}s"
end
object_type() click to toggle source
# File lib/enrichment_db/geo/region.rb, line 55
def self.object_type
  to_s.split(':').last.downcase.to_sym
end