class Geonames::Spot

Main Ruby Model for Geonames Spot Concept

Attributes

collection[RW]
abbr[RW]
ascii[RW]
code[RW]
geom[RW]
geoname_id[RW]
gid[RW]
id[RW]
kind[RW]
lat[RW]
lon[RW]
name[RW]
nation[RW]
pop[RW]
region[RW]
table[RW]
tz[RW]
x[RW]
y[RW]
zip[RW]

Public Class Methods

from_hash(hsh) click to toggle source
# 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) click to toggle source
# File lib/geonames_local/features/spot.rb, line 92
def nearest(x, y)
  from_hash(Adapter.find_near(collection, x, y, 1)[0])
end
new(params = nil, kind = nil) click to toggle source

Geonames Spot

Every geoname type will be parsed as a spot

# 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

Public Instance Methods

alt() click to toggle source
# File lib/geonames_local/features/spot.rb, line 70
def alt
  @ele || @gtop
end
human_code(code) click to toggle source

Translate geonames ADMx to models

# 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) click to toggle source

Parse Geonames Dump Export

# 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) click to toggle source

Parse Geom to float or GeoRuby Point

# 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) click to toggle source

Parse Geonames Zip Export

# 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() click to toggle source

Parse Time

# File lib/geonames_local/features/spot.rb, line 76
def updated_at
  Time.utc(*@up.split('-'))
end