From: Tom Hughes Date: Sun, 7 Jul 2024 13:39:35 +0000 (+0100) Subject: Use named captures to simplify latlon parsing X-Git-Tag: live~381^2~2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/e5057dd57adca8e8ad6961eb15346cb365464667?ds=sidebyside;hp=-c Use named captures to simplify latlon parsing --- e5057dd57adca8e8ad6961eb15346cb365464667 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index fdc7c35a5..6fe5b2e57 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -24,7 +24,7 @@ FactoryBot/ExcessiveCreateList: # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns. # URISchemes: http, https Layout/LineLength: - Max: 248 + Max: 266 # Offense count: 29 # This cop supports unsafe autocorrection (--autocorrect-all). diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 0e0a2d8af..0dabc5a62 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -206,20 +206,16 @@ class GeocoderController < ApplicationController if query = params[:query] query.strip! - if latlon = query.match(/^([NS])\s*(\d{1,3}(\.\d*)?)\W*([EW])\s*(\d{1,3}(\.\d*)?)$/).try(:captures) || # [NSEW] decimal degrees - query.match(/^(\d{1,3}(\.\d*)?)\s*([NS])\W*(\d{1,3}(\.\d*)?)\s*([EW])$/).try(:captures) # decimal degrees [NSEW] - params.merge!(nsew_to_decdeg(latlon)).delete(:query) + if latlon = query.match(/^(?[NS])\s*(?\d{1,3}(?:\.\d+)?)°?\W*(?[EW])\s*(?\d{1,3}(?:\.\d+)?)°?$/) || # [NSEW] decimal degrees + query.match(/^(?\d{1,3}(?:\.\d+)?)°?\s*(?[NS])\W*(?\d{1,3}(?:\.\d+)?)°?\s*(?[EW])$/) || # decimal degrees [NSEW] + query.match(/^(?[NS])\s*(?\d{1,3})°?(?:\s*(?\d{1,3}(?:\.\d+)?)['′]?)\W*(?[EW])\s*(?\d{1,3})°?(?:\s*(?\d{1,3}(?:\.\d+)?)['′]?)$/) || # [NSEW] degrees, decimal minutes + query.match(/^(?\d{1,3})°?(?:\s*(?\d{1,3}(?:\.\d+)?)['′]?)\s*(?[NS])\W*(?\d{1,3})°?(?:\s*(?\d{1,3}(?:\.\d+)?)['′]?)\s*(?[EW])$/) || # degrees, decimal minutes [NSEW] + query.match(/^(?[NS])\s*(?\d{1,3})°?\s*(?\d{1,2})['′]?(?:\s*(?\d{1,3}(?:\.\d+)?)["″]?)\W*(?[EW])\s*(?\d{1,3})°?\s*(?\d{1,2})['′]?(?:\s*(?\d{1,3}(?:\.\d+)?)["″]?)$/) || # [NSEW] degrees, minutes, decimal seconds + query.match(/^(?\d{1,3})°?\s*(?\d{1,2})['′]?(?:\s*(?\d{1,3}(?:\.\d+)?)["″]?)\s*(?[NS])\W*(?\d{1,3})°?\s*(?\d{1,2})['′]?(?:\s*(?\d{1,3}(?:\.\d+)?)["″]?)\s*(?[EW])$/) # degrees, minutes, decimal seconds [NSEW] + params.merge!(to_decdeg(latlon.named_captures)).delete(:query) - elsif latlon = query.match(/^([NS])\s*(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?\W*([EW])\s*(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?$/).try(:captures) || # [NSEW] degrees, decimal minutes - query.match(/^(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?\s*([NS])\W*(\d{1,3})°?(?:\s*(\d{1,3}(\.\d*)?)?['′]?)?\s*([EW])$/).try(:captures) # degrees, decimal minutes [NSEW] - params.merge!(ddm_to_decdeg(latlon)).delete(:query) - - elsif latlon = query.match(/^([NS])\s*(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?\W*([EW])\s*(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?$/).try(:captures) || # [NSEW] degrees, minutes, decimal seconds - query.match(/^(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?\s*([NS])\W*(\d{1,3})°?\s*(\d{1,2})['′]?(?:\s*(\d{1,3}(\.\d*)?)?["″]?)?\s*([EW])$/).try(:captures) # degrees, minutes, decimal seconds [NSEW] - params.merge!(dms_to_decdeg(latlon)).delete(:query) - - elsif latlon = query.match(%r{^([+-]?\d+(\.\d*)?)(?:\s+|\s*[,/]\s*)([+-]?\d+(\.\d*)?)$}) - params.merge!(:lat => latlon[1], :lon => latlon[3]).delete(:query) + elsif latlon = query.match(%r{^(?[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?[+-]?\d+(?:\.\d+)?)$}) + params.merge!(:lat => latlon["lat"], :lon => latlon["lon"]).delete(:query) params[:latlon_digits] = true end @@ -228,39 +224,20 @@ class GeocoderController < ApplicationController params.permit(:query, :lat, :lon, :latlon_digits, :zoom, :minlat, :minlon, :maxlat, :maxlon) end - def nsew_to_decdeg(captures) - begin - Float(captures[0]) - lat = captures[2].casecmp("s").zero? ? -captures[0].to_f : captures[0].to_f - lon = captures[5].casecmp("w").zero? ? -captures[3].to_f : captures[3].to_f - rescue StandardError - lat = captures[0].casecmp("s").zero? ? -captures[1].to_f : captures[1].to_f - lon = captures[3].casecmp("w").zero? ? -captures[4].to_f : captures[4].to_f - end - { :lat => lat, :lon => lon } - end + def to_decdeg(captures) + ns = captures.fetch("ns").casecmp("s").zero? ? -1 : 1 + nsd = captures.fetch("nsd", "0").to_f + nsm = captures.fetch("nsm", "0").to_f + nss = captures.fetch("nss", "0").to_f - def ddm_to_decdeg(captures) - begin - Float(captures[0]) - lat = captures[3].casecmp("s").zero? ? -(captures[0].to_f + (captures[1].to_f / 60)) : captures[0].to_f + (captures[1].to_f / 60) - lon = captures[7].casecmp("w").zero? ? -(captures[4].to_f + (captures[5].to_f / 60)) : captures[4].to_f + (captures[5].to_f / 60) - rescue StandardError - lat = captures[0].casecmp("s").zero? ? -(captures[1].to_f + (captures[2].to_f / 60)) : captures[1].to_f + (captures[2].to_f / 60) - lon = captures[4].casecmp("w").zero? ? -(captures[5].to_f + (captures[6].to_f / 60)) : captures[5].to_f + (captures[6].to_f / 60) - end - { :lat => lat, :lon => lon } - end + ew = captures.fetch("ew").casecmp("w").zero? ? -1 : 1 + ewd = captures.fetch("ewd", "0").to_f + ewm = captures.fetch("ewm", "0").to_f + ews = captures.fetch("ews", "0").to_f + + lat = ns * (nsd + (nsm / 60) + (nss / 3600)) + lon = ew * (ewd + (ewm / 60) + (ews / 3600)) - def dms_to_decdeg(captures) - begin - Float(captures[0]) - lat = captures[4].casecmp("s").zero? ? -(captures[0].to_f + ((captures[1].to_f + (captures[2].to_f / 60)) / 60)) : captures[0].to_f + ((captures[1].to_f + (captures[2].to_f / 60)) / 60) - lon = captures[9].casecmp("w").zero? ? -(captures[5].to_f + ((captures[6].to_f + (captures[7].to_f / 60)) / 60)) : captures[5].to_f + ((captures[6].to_f + (captures[7].to_f / 60)) / 60) - rescue StandardError - lat = captures[0].casecmp("s").zero? ? -(captures[1].to_f + ((captures[2].to_f + (captures[3].to_f / 60)) / 60)) : captures[1].to_f + ((captures[2].to_f + (captures[3].to_f / 60)) / 60) - lon = captures[5].casecmp("w").zero? ? -(captures[6].to_f + ((captures[7].to_f + (captures[8].to_f / 60)) / 60)) : captures[6].to_f + ((captures[7].to_f + (captures[8].to_f / 60)) / 60) - end { :lat => lat, :lon => lon } end end diff --git a/test/controllers/geocoder_controller_test.rb b/test/controllers/geocoder_controller_test.rb index 15d4eb5f9..85baaafef 100644 --- a/test/controllers/geocoder_controller_test.rb +++ b/test/controllers/geocoder_controller_test.rb @@ -60,6 +60,19 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest end end + ## + # Test identification of integer lat/lon pairs using N/E with degrees + def test_identify_latlon_ne_d_int_deg + [ + "N50 E14", + "N50° E14°", + "50N 14E", + "50°N 14°E" + ].each do |code| + latlon_check code, 50, 14 + end + end + ## # Test identification of lat/lon pairs using N/W with degrees def test_identify_latlon_nw_d @@ -223,6 +236,31 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest end end + ## + # Test identification of lat/lon pairs with missing fractions + def test_no_identify_latlon_ne_missing_fraction_part + [ + "N50. E14.", + "N50.° E14.°", + "50.N 14.E", + "50.°N 14.°E", + "N50 1.' E14 2.'", + "N50° 1.' E14° 2.'", + "50N 1.' 14 2.'E", + "50° 1.'N 14° 2.'E", + "N50 1' 3,\" E14 2' 4.\"", + "N50° 1' 3.\" E14° 2' 4.\"", + "50N 1' 3.\" 14 2' 4.\"E", + "50° 1' 3.\"N 14° 2' 4.\"E" + ].each do |code| + get search_path(:query => code) + assert_response :success + assert_template :search + assert_template :layout => "map" + assert_equal %w[osm_nominatim], assigns(:sources).pluck(:name) + end + end + ## # Test identification of US zipcodes def test_identify_us_postcode