Class: FeatureCode

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/has_geo_lookup/models/feature_code.rb

Overview

Feature classification codes for geographic places from Geonames.org

This model stores the feature classification system used by Geonames.org to categorize different types of geographic features. Each feature code belongs to a feature class and provides detailed categorization for places, administrative divisions, hydrographic features, terrain features, etc.

Feature classes include:

  • A: Administrative divisions (countries, states, counties)
  • P: Populated places (cities, towns, villages)
  • H: Hydrographic features (rivers, lakes, seas)
  • T: Terrain features (mountains, hills, valleys)
  • R: Roads and railways
  • S: Spots and buildings (schools, churches, stations)
  • U: Undersea features
  • V: Vegetation features (forests, parks)
  • L: Localities and areas

Examples:

Find administrative division codes

FeatureCode.where(feature_class: "A")

Find populated place codes

FeatureCode.where(feature_class: "P")

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionString

Detailed description of what this feature represents

Returns:

  • the current value of description



33
34
35
# File 'lib/has_geo_lookup/models/feature_code.rb', line 33

def description
  @description
end

#feature_classString

Single letter feature class (A, P, H, T, R, S, U, V, L)

Returns:

  • the current value of feature_class



33
34
35
# File 'lib/has_geo_lookup/models/feature_code.rb', line 33

def feature_class
  @feature_class
end

#feature_codeString

Specific feature code (e.g., PPL, ADM1, RIV, MT)

Returns:

  • the current value of feature_code



33
34
35
# File 'lib/has_geo_lookup/models/feature_code.rb', line 33

def feature_code
  @feature_code
end

#nameString

Human-readable name of the feature type

Returns:

  • the current value of name



33
34
35
# File 'lib/has_geo_lookup/models/feature_code.rb', line 33

def name
  @name
end

Class Method Details

.admin_levelsArray<FeatureCode>

Get all administrative division levels

Returns all ADM (administrative) feature codes ordered by level, useful for understanding the administrative hierarchy.

Examples:

FeatureCode.admin_levels
# => [ADM0 (countries), ADM1 (states), ADM2 (counties), etc.]

Returns:

  • Administrative division feature codes



97
98
99
# File 'lib/has_geo_lookup/models/feature_code.rb', line 97

def self.admin_levels
  administrative_levels.to_a
end

.find_by_keyword(keyword) ⇒ FeatureCode?

Find the most appropriate feature code for a keyword

Returns the first feature code that matches the keyword, prioritizing exact name matches over description matches.

Examples:

FeatureCode.find_by_keyword("county")
# => #<FeatureCode feature_class: "A", feature_code: "ADM2", name: "second-order administrative division">

Parameters:

  • Search term

Returns:

  • Best matching feature code or nil



112
113
114
115
116
117
118
119
120
121
# File 'lib/has_geo_lookup/models/feature_code.rb', line 112

def self.find_by_keyword(keyword)
  return nil if keyword.blank?
  
  # First try exact name match
  exact_match = where("LOWER(name) = ?", keyword.to_s.downcase).first
  return exact_match if exact_match
  
  # Then try partial matches
  by_keyword(keyword).first
end

.search(keyword) ⇒ ActiveRecord::Relation

Find feature codes matching a search term

Searches both the name and description fields for the given keyword, useful for finding appropriate feature codes when you know the type of place but not the exact code.

Examples:

FeatureCode.search("county")
# => Returns feature codes related to counties
FeatureCode.search("populated")
# => Returns feature codes for populated places

Parameters:

  • Search term to match against name and description

Returns:

  • Matching feature codes



83
84
85
# File 'lib/has_geo_lookup/models/feature_code.rb', line 83

def self.search(keyword)
  by_keyword(keyword)
end

Instance Method Details

#admin_levelInteger?

Get the administrative level for administrative features

Returns:

  • Administrative level (0-4) or nil for non-administrative features



179
180
181
182
183
184
# File 'lib/has_geo_lookup/models/feature_code.rb', line 179

def admin_level
  return nil unless administrative? && feature_code.start_with?("ADM")
  
  level_match = feature_code.match(/ADM(\d)/)
  level_match ? level_match[1].to_i : nil
end

#administrative?Boolean

Check if this represents an administrative division

Returns:

  • true if feature_class is "A"



155
156
157
# File 'lib/has_geo_lookup/models/feature_code.rb', line 155

def administrative?
  feature_class == "A"
end

#display_nameString

Returns a human-readable description

Combines the name and description with the feature code for comprehensive identification.

Examples:

feature_code.display_name
# => "ADM2 - second-order administrative division (county, district)"

Returns:

  • Formatted description



149
150
151
# File 'lib/has_geo_lookup/models/feature_code.rb', line 149

def display_name
  "#{feature_code} - #{name}"
end

#full_codeString

Returns the full feature identifier

Combines feature class and feature code into a single identifier string for display or logging purposes.

Examples:

feature_code.full_code
# => "P.PPL" (for populated place)

Returns:

  • Combined feature class and code



135
136
137
# File 'lib/has_geo_lookup/models/feature_code.rb', line 135

def full_code
  "#{feature_class}.#{feature_code}"
end

#hydrographic?Boolean

Check if this represents a hydrographic feature

Returns:

  • true if feature_class is "H"



167
168
169
# File 'lib/has_geo_lookup/models/feature_code.rb', line 167

def hydrographic?
  feature_class == "H"
end

#populated_place?Boolean

Check if this represents a populated place

Returns:

  • true if feature_class is "P"



161
162
163
# File 'lib/has_geo_lookup/models/feature_code.rb', line 161

def populated_place?
  feature_class == "P"
end

#terrain?Boolean

Check if this represents a terrain feature

Returns:

  • true if feature_class is "T"



173
174
175
# File 'lib/has_geo_lookup/models/feature_code.rb', line 173

def terrain?
  feature_class == "T"
end