Class: Geonames::Spot

Inherits:
Object
  • Object
show all
Defined in:
lib/geonames_local/features/spot.rb

Overview

Main Ruby Model for Geonames Spot Concept

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil, kind = nil) ⇒ Spot

Geonames Spot

Every geoname type will be parsed as a spot



18
19
20
21
22
23
24
25
# File 'lib/geonames_local/features/spot.rb', line 18

def initialize(params = nil, kind = nil)
  return unless params.instance_of? String
  kind == :zip ? parse_zip(params) : parse(params)
  if @kind == :region
    @name.gsub!(/Estado d\w\s/, '')
    @name.gsub!(/Federal District/, 'Distrito Federal')
  end
end

Class Attribute Details

.collectionObject

Returns the value of attribute collection.



90
91
92
# File 'lib/geonames_local/features/spot.rb', line 90

def collection
  @collection
end

Instance Attribute Details

#abbrObject

Returns the value of attribute abbr.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def abbr
  @abbr
end

#asciiObject

Returns the value of attribute ascii.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def ascii
  @ascii
end

#codeObject

Returns the value of attribute code.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def code
  @code
end

#geomObject

Returns the value of attribute geom.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def geom
  @geom
end

#gidObject Also known as: geoname_id

Returns the value of attribute gid.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def gid
  @gid
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def id
  @id
end

#kindObject Also known as: table

Returns the value of attribute kind.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def kind
  @kind
end

#latObject Also known as: y

Returns the value of attribute lat.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def lat
  @lat
end

#lonObject Also known as: x

Returns the value of attribute lon.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def lon
  @lon
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def name
  @name
end

#nationObject

Returns the value of attribute nation.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def nation
  @nation
end

#popObject

Returns the value of attribute pop.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def pop
  @pop
end

#regionObject

Returns the value of attribute region.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def region
  @region
end

#tzObject

Returns the value of attribute tz.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def tz
  @tz
end

#zipObject

Returns the value of attribute zip.



6
7
8
# File 'lib/geonames_local/features/spot.rb', line 6

def zip
  @zip
end

Class Method Details

.from_hash(hsh) ⇒ Object



96
97
98
99
100
101
# File 'lib/geonames_local/features/spot.rb', line 96

def from_hash(hsh)
  spot = new
  hsh.each { |key, val| spot.instance_variable_set("@#{key}", val) }
  spot.geom = GeoRuby::SimpleFeatures::Point.from_x_y(*spot.geom)
  spot
end

.nearest(x, y) ⇒ Object



92
93
94
# File 'lib/geonames_local/features/spot.rb', line 92

def nearest(x, y)
  from_hash(Adapter.find_near(collection, x, y, 1)[0])
end

Instance Method Details

#altObject



70
71
72
# File 'lib/geonames_local/features/spot.rb', line 70

def alt
  @ele || @gtop
end

#human_code(code) ⇒ Object

Translate geonames ADMx to models



81
82
83
84
85
86
87
# File 'lib/geonames_local/features/spot.rb', line 81

def human_code(code)
  case code
  when 'ADM1' then :region
  when 'ADM2', 'ADM3', 'ADM4' then :city
  else :other
  end
end

#parse(row) ⇒ Object

Parse Geonames Dump Export



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/geonames_local/features/spot.rb', line 30

def parse(row)
  gid, name, @ascii, @alternates, lat, lon, feat, kind,
  @nation, _cc2, @region, @code, _adm3, _adm4, @pop, @ele,
  @gtop, @tz, @up = row.split(/\t/)

  @name = name #name.encode(Encoding::ISO_8859_1)
  @gid = @geoname_id = gid.to_i
  @kind = human_code(kind)

  @abbr = @alternates.split(',').find { |n| n =~ /^[A-Z]{2,3}$/ }

  parse_geom(lat, lon)
  # puts "#{@kind} - #{@code} - #{@region}"
end

#parse_geom(lat, lon) ⇒ Object

Parse Geom to float or GeoRuby Point



60
61
62
63
64
65
66
67
68
# File 'lib/geonames_local/features/spot.rb', line 60

def parse_geom(lat, lon)
  @lat, @lon = lat.to_f, lon.to_f

  if defined? GeoRuby
    @geom = GeoRuby::SimpleFeatures::Point.from_x_y(@lon, @lat)
  else
    { lat: @lat, lon: @lon }
  end
end

#parse_zip(row) ⇒ Object

Parse Geonames Zip Export



48
49
50
51
52
53
54
55
# File 'lib/geonames_local/features/spot.rb', line 48

def parse_zip(row)
  _nation, @zip, @name, _a1, _a1c, _a2, @code, _a3, _a3c,
  lat, lon, _acc = row.split(/\t/)

  @kind = :city
  parse_geom(lat, lon)
  # puts "#{row}\n---\n#{@kind} - #{@code} - #{@zip} #{lat}x#{lon}"
end

#updated_atObject

Parse Time



76
77
78
# File 'lib/geonames_local/features/spot.rb', line 76

def updated_at
  Time.utc(*@up.split('-'))
end