Skip to content

Commit

Permalink
Vebt 341 - change geocoding logic to use physical address and run job…
Browse files Browse the repository at this point in the history
… to update where mailing differs from physical address (#1190)

* change geocoding to use physical address

* update insitutions that were geocoded with mailing addy instead of physical addy

* cosmetic change to trigger stuck jenkins

---------

Co-authored-by: nfstern02 <[email protected]>
  • Loading branch information
GcioGregg and nfstern02 authored Aug 19, 2024
1 parent bec7b25 commit 0ee1c18
Show file tree
Hide file tree
Showing 9 changed files with 3,138 additions and 103 deletions.
14 changes: 7 additions & 7 deletions app/concerns/geocoder_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def search_bad_address(result, geocoded3)
# parses bad addresses and flags and issues
stopwords = %w[ave avenue road rd dr drive blvd pkwy st street parkway]
stopwords_regex = /\b(#{Regexp.union(*stopwords).source})\b/i
if result.address
add_split = result.address.split(stopwords_regex).map(&:strip)[0..1].join(' ')
new_address = [add_split, result.state, result.zip].compact.join(' ')
if result.physical_address
add_split = result.physical_address.split(stopwords_regex).map(&:strip)[0..1].join(' ')
new_address = [add_split, result.physical_state, result.physical_zip].compact.join(' ')
end

geocoded, timed_out = geocode_addy('coordinates', new_address, 0)
Expand Down Expand Up @@ -65,17 +65,17 @@ def check_search_fields(result, geo)
def text_search_numbered_address(result, geo, city, state, num)
search = []
address_number = result.institution.downcase.split('#').last.to_i
titleized_city = result.city ? result.city.titleize : ''
search << geo if city == titleized_city && state == result.state && num == address_number
titleized_city = result.physical_city ? result.physical_city.titleize : ''
search << geo if city == titleized_city && state == result.physical_state && num == address_number
search
end

def text_search(result, geo, city, state)
search = []
zip = geo.data.dig('address', 'postcode')
village = geo.data.dig('address', 'village')
city_check = result.city.titleize if result.city
search << geo if city == city_check || state == result.state || zip == result.zip || village == city_check
city_check = result.physical_city.titleize if result.physical_city
search << geo if city == city_check || state == result.physical_state || zip == result.physical_zip || village == city_check
search
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/dashboards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def export_ungeocodables
format.csv do
send_data Institution.export_ungeocodables(
Institution.ungeocodables.pluck(
:institution, :facility_code, :address_1, :address_2, :address_3, :city, :state,
:zip, :physical_country, :cross, :ope
:institution, :facility_code, :physical_address_1, :physical_address_2, :physical_address_3, :physical_city,
:physical_state, :physical_zip, :physical_country, :cross, :ope
)
), type: 'text/csv', filename: 'ungeocodables.csv'
end
Expand Down
27 changes: 14 additions & 13 deletions app/models/search_geocoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def process_geocoder_address
by_address.each_with_index do |result, idx|
log_info_status(result, idx)

address = [parse_add_fields(result, result.address),
parse_add_fields(result, result.address_1),
parse_add_fields(result, result.address_2)]
address = [parse_add_fields(result, result.physical_address),
parse_add_fields(result, result.physical_address_1),
parse_add_fields(result, result.physical_address_2)]
geocode_fields(result, address)
end
process_geocoder_country
Expand All @@ -38,33 +38,34 @@ def process_geocoder_country
country.each_with_index do |result, idx|
log_info_status(result, (idx + @by_address.size))

if result.state.present? && result.physical_country.present?
if result.physical_state.present? && result.physical_country.present?
geocoded_ct = Geocoder.coordinates(result.physical_country)
update_mismatch(result, geocoded_ct)
else
address = [parse_address(result, result.address),
parse_address(result, result.address_1),
parse_address(result, result.address_2)]
address = [parse_address(result, result.physical_address),
parse_address(result, result.physical_address_1),
parse_address(result, result.physical_address_2)]

geocode_fields(result, address)
end
end
end

def log_info_status(result, idx)
Rails.logger.info "#{idx}: processing #{result.country}: #{result.institution} " \
"#{result.address} #{result.address_1} #{result.address_2} " \
"#{result.city}, #{result.state}, #{result.zip}"
Rails.logger.info "#{idx}: processing #{result.physical_country}: #{result.institution} " \
"#{result.address} #{result.physical_address_1} #{result.physical_address_2} " \
"#{result.physical_city}, #{result.physical_state}, #{result.physical_zip}"

message = "Geocoding #{idx} of #{@total_count}"
UpdatePreviewGenerationStatusJob.perform_later(message) if (idx % 10).eql?(0)
end

def parse_add_fields(res, field)
"#{field}, #{res.city}, #{res.state}, #{res.zip}, #{res.country}" if field.present?
"#{field}, #{res.physical_city}, #{res.physical_state}, #{res.physical_zip}, #{res.physical_country}" if field.present?
end

def parse_address(res, field)
field.present? ? "#{field}, #{res.city}, #{res.physical_country}" : "#{res.city}, #{res.physical_country}"
field.present? ? "#{field}, #{res.physical_city}, #{res.physical_country}" : "#{res.physical_city}, #{res.physical_country}"
end

def geocode_fields(result, address)
Expand All @@ -84,7 +85,7 @@ def geocode_fields(result, address)

# no geocode match on first 3 address fields. Geocode based on city, state, zip
# then check bad address on result of geocoding
geocoded3, timed_out = geocode_addy('coordinates', "#{result.city}, #{result.state}, #{result.zip}", 0)
geocoded3, timed_out = geocode_addy('coordinates', "#{result.physical_city}, #{result.physical_state}, #{result.physical_zip}", 0)
return if timed_out

check_bad_address(result, geocoded3)
Expand Down
12 changes: 6 additions & 6 deletions app/views/dashboards/_geocoding_issues.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<tr class="<%= cycle('even', 'odd') -%>">
<td><%= ungeocodable.institution %></td>
<td><%= ungeocodable.facility_code %></td>
<td><%= ungeocodable.address_1 %></td>
<td><%= ungeocodable.address_2 %></td>
<td><%= ungeocodable.address_3 %></td>
<td><%= ungeocodable.city %></td>
<td><%= ungeocodable.state %></td>
<td><%= ungeocodable.zip %></td>
<td><%= ungeocodable.physical_address_1 %></td>
<td><%= ungeocodable.physical_address_2 %></td>
<td><%= ungeocodable.physical_address_3 %></td>
<td><%= ungeocodable.physical_city %></td>
<td><%= ungeocodable.physical_state %></td>
<td><%= ungeocodable.physical_zip %></td>
<td><%= ungeocodable.physical_country %></td>
<td><%= ungeocodable.cross %></td>
<td><%= ungeocodable.ope %></td>
Expand Down
Loading

0 comments on commit 0ee1c18

Please sign in to comment.