Sha256: 2f9a6bb02c03fb7fb97e1ae82c1c0f2a73cc48ac4f85496921820ab09787c182

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

class EnrichmentDb::Geo::Region < EnrichmentDb::DatumModel
  attr_reader :id
  attr_reader :name

  DATABASE_NAME = 'geo'

  def initialize(data)
    @id = data['id']
    @name = data['name']
  end

  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

  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
  
  private

  def self.object_type
    to_s.split(':').last.downcase.to_sym
  end

  def self.make_table_name
    "#{object_type}s"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
enrichment_db-0.1.15 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.14 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.13 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.12 lib/enrichment_db/geo/region.rb
enrichment_db-0.1.11 lib/enrichment_db/geo/region.rb