Class: Geonames::Spot
- Inherits:
-
Object
- Object
- Geonames::Spot
- Defined in:
- lib/geonames_local/features/spot.rb
Overview
Main Ruby Model for Geonames Spot Concept
Class Attribute Summary collapse
-
.collection ⇒ Object
Returns the value of attribute collection.
Instance Attribute Summary collapse
-
#abbr ⇒ Object
Returns the value of attribute abbr.
-
#ascii ⇒ Object
Returns the value of attribute ascii.
-
#code ⇒ Object
Returns the value of attribute code.
-
#geom ⇒ Object
Returns the value of attribute geom.
-
#gid ⇒ Object
(also: #geoname_id)
Returns the value of attribute gid.
-
#id ⇒ Object
Returns the value of attribute id.
-
#kind ⇒ Object
(also: #table)
Returns the value of attribute kind.
-
#lat ⇒ Object
(also: #y)
Returns the value of attribute lat.
-
#lon ⇒ Object
(also: #x)
Returns the value of attribute lon.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nation ⇒ Object
Returns the value of attribute nation.
-
#pop ⇒ Object
Returns the value of attribute pop.
-
#region ⇒ Object
Returns the value of attribute region.
-
#tz ⇒ Object
Returns the value of attribute tz.
-
#zip ⇒ Object
Returns the value of attribute zip.
Class Method Summary collapse
Instance Method Summary collapse
- #alt ⇒ Object
-
#human_code(code) ⇒ Object
Translate geonames ADMx to models.
-
#initialize(params = nil, kind = nil) ⇒ Spot
constructor
Geonames Spot.
-
#parse(row) ⇒ Object
Parse Geonames Dump Export.
-
#parse_geom(lat, lon) ⇒ Object
Parse Geom to float or GeoRuby Point.
-
#parse_zip(row) ⇒ Object
Parse Geonames Zip Export.
-
#updated_at ⇒ Object
Parse Time.
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
.collection ⇒ Object
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
#abbr ⇒ Object
Returns the value of attribute abbr.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def abbr @abbr end |
#ascii ⇒ Object
Returns the value of attribute ascii.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def ascii @ascii end |
#code ⇒ Object
Returns the value of attribute code.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def code @code end |
#geom ⇒ Object
Returns the value of attribute geom.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def geom @geom end |
#gid ⇒ Object 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 |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def id @id end |
#kind ⇒ Object 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 |
#lat ⇒ Object 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 |
#lon ⇒ Object 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 |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def name @name end |
#nation ⇒ Object
Returns the value of attribute nation.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def nation @nation end |
#pop ⇒ Object
Returns the value of attribute pop.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def pop @pop end |
#region ⇒ Object
Returns the value of attribute region.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def region @region end |
#tz ⇒ Object
Returns the value of attribute tz.
6 7 8 |
# File 'lib/geonames_local/features/spot.rb', line 6 def tz @tz end |
#zip ⇒ Object
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
#alt ⇒ Object
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_at ⇒ Object
Parse Time
76 77 78 |
# File 'lib/geonames_local/features/spot.rb', line 76 def updated_at Time.utc(*@up.split('-')) end |