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