Skip to content

Commit

Permalink
Add county to geoloc
Browse files Browse the repository at this point in the history
This new field would be useful in a lot of countries that you can check here - https://en.wikipedia.org/wiki/County.
On top of that, some of the already used geocoders here are returning this specific field.
eg:
  - FCC: county
  - Google: administrative_area_level_2
  - OpenCage: county
  - OpenStretMap: county
  • Loading branch information
dsantosmerino committed Jan 22, 2020
1 parent 3ee032d commit 8cf5ec7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/geokit/geo_loc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GeoLoc < LatLng
# 100 Spear St, San Francisco, CA, 94101, US
# Street number and street name are extracted from the street address
# attribute if they don't exist
attr_accessor :state_name, :state_code, :zip, :country_code, :country
attr_accessor :county, :state_name, :state_code, :zip, :country_code, :country
attr_accessor :all, :district, :sub_premise,
:neighborhood
attr_writer :state, :full_address, :street_number, :street_name, :formatted_address
Expand Down Expand Up @@ -58,6 +58,7 @@ def initialize(h = {})
@street_number = nil
@street_name = nil
@city = h[:city]
@county = h[:county]
@state = h[:state]
@state_code = h[:state_code]
@state_name = h[:state_name]
Expand Down Expand Up @@ -109,9 +110,9 @@ def formatted_address
# gives you all the important fields as key-value pairs
def hash
res = {}
fields = [:success, :lat, :lng, :country_code, :city, :state, :zip,
:street_address, :district, :provider, :full_address, :is_us?,
:ll, :precision, :district_fips, :state_fips, :block_fips, :sub_premise]
fields = %i[success lat lng country_code city county state zip
street_address district provider full_address is_us?
ll precision district_fips state_fips block_fips sub_premise]
fields.each { |s| res[s] = send(s.to_s) }
res
end
Expand All @@ -133,8 +134,8 @@ def street_address=(address)
end

# Returns a comma-delimited string consisting of the street address, city,
# state, zip, and country code. Only includes those attributes that are
# non-blank.
# state, zip, and country code. Only includes those attributes
# that are non-blank.
def to_geocodeable_s
a = [street_address, district, city, state, zip, country_code].compact
a.delete_if { |e| !e || e == '' }
Expand All @@ -156,6 +157,7 @@ def to_s
["Provider: #{provider}",
"Street: #{street_address}",
"City: #{city}",
"County: #{county}",
"State: #{state}",
"Zip: #{zip}",
"Latitude: #{lat}",
Expand Down
1 change: 1 addition & 0 deletions test/test_geo_loc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def test_to_yaml
assert_equal [
'city', 'San Francisco',
'country_code', 'US',
'county', '',
'full_address', '',
'lat', '',
'lng', '',
Expand Down

0 comments on commit 8cf5ec7

Please sign in to comment.