Class: EnrichmentDb::Geo::Region

Inherits:
DatumModel show all
Defined in:
lib/enrichment_db/geo/region.rb

Direct Known Subclasses

Postcode, SA1, SA2, SA3, SA4, State, Suburb

Constant Summary collapse

DATABASE_NAME =
'geo'

Instance Attribute Summary collapse

Attributes inherited from DatumModel

#attrs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DatumModel

all, by_lambda, format_result, lazy_attr_reader

Constructor Details

#initialize(data) ⇒ Region

Returns a new instance of Region.



7
8
9
10
# File 'lib/enrichment_db/geo/region.rb', line 7

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/enrichment_db/geo/region.rb', line 2

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/enrichment_db/geo/region.rb', line 3

def name
  @name
end

Class Method Details

.by_boundary(values) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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