Use named captures to simplify latlon parsing
This commit is contained in:
parent
f047f86c1d
commit
e5057dd57a
3 changed files with 60 additions and 45 deletions
|
@ -24,7 +24,7 @@ FactoryBot/ExcessiveCreateList:
|
||||||
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
|
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns.
|
||||||
# URISchemes: http, https
|
# URISchemes: http, https
|
||||||
Layout/LineLength:
|
Layout/LineLength:
|
||||||
Max: 248
|
Max: 266
|
||||||
|
|
||||||
# Offense count: 29
|
# Offense count: 29
|
||||||
# This cop supports unsafe autocorrection (--autocorrect-all).
|
# This cop supports unsafe autocorrection (--autocorrect-all).
|
||||||
|
|
|
@ -206,20 +206,16 @@ class GeocoderController < ApplicationController
|
||||||
if query = params[:query]
|
if query = params[:query]
|
||||||
query.strip!
|
query.strip!
|
||||||
|
|
||||||
if latlon = query.match(/^([NS])\s*(\d{1,3}(\.\d*)?)\W*([EW])\s*(\d{1,3}(\.\d*)?)$/).try(:captures) || # [NSEW] decimal degrees
|
if latlon = query.match(/^(?<ns>[NS])\s*(?<nsd>\d{1,3}(?:\.\d+)?)°?\W*(?<ew>[EW])\s*(?<ewd>\d{1,3}(?:\.\d+)?)°?$/) || # [NSEW] decimal degrees
|
||||||
query.match(/^(\d{1,3}(\.\d*)?)\s*([NS])\W*(\d{1,3}(\.\d*)?)\s*([EW])$/).try(:captures) # decimal degrees [NSEW]
|
query.match(/^(?<nsd>\d{1,3}(?:\.\d+)?)°?\s*(?<ns>[NS])\W*(?<ewd>\d{1,3}(?:\.\d+)?)°?\s*(?<ew>[EW])$/) || # decimal degrees [NSEW]
|
||||||
params.merge!(nsew_to_decdeg(latlon)).delete(:query)
|
query.match(/^(?<ns>[NS])\s*(?<nsd>\d{1,3})°?(?:\s*(?<nsm>\d{1,3}(?:\.\d+)?)['′]?)\W*(?<ew>[EW])\s*(?<ewd>\d{1,3})°?(?:\s*(?<ewm>\d{1,3}(?:\.\d+)?)['′]?)$/) || # [NSEW] degrees, decimal minutes
|
||||||
|
query.match(/^(?<nsd>\d{1,3})°?(?:\s*(?<nsm>\d{1,3}(?:\.\d+)?)['′]?)\s*(?<ns>[NS])\W*(?<ewd>\d{1,3})°?(?:\s*(?<ewm>\d{1,3}(?:\.\d+)?)['′]?)\s*(?<ew>[EW])$/) || # degrees, decimal minutes [NSEW]
|
||||||
|
query.match(/^(?<ns>[NS])\s*(?<nsd>\d{1,3})°?\s*(?<nsm>\d{1,2})['′]?(?:\s*(?<nss>\d{1,3}(?:\.\d+)?)["″]?)\W*(?<ew>[EW])\s*(?<ewd>\d{1,3})°?\s*(?<ewm>\d{1,2})['′]?(?:\s*(?<ews>\d{1,3}(?:\.\d+)?)["″]?)$/) || # [NSEW] degrees, minutes, decimal seconds
|
||||||
|
query.match(/^(?<nsd>\d{1,3})°?\s*(?<nsm>\d{1,2})['′]?(?:\s*(?<nss>\d{1,3}(?:\.\d+)?)["″]?)\s*(?<ns>[NS])\W*(?<ewd>\d{1,3})°?\s*(?<ewm>\d{1,2})['′]?(?:\s*(?<ews>\d{1,3}(?:\.\d+)?)["″]?)\s*(?<ew>[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
|
elsif latlon = query.match(%r{^(?<lat>[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?<lon>[+-]?\d+(?:\.\d+)?)$})
|
||||||
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!(:lat => latlon["lat"], :lon => latlon["lon"]).delete(:query)
|
||||||
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)
|
|
||||||
|
|
||||||
params[:latlon_digits] = true
|
params[:latlon_digits] = true
|
||||||
end
|
end
|
||||||
|
@ -228,39 +224,20 @@ class GeocoderController < ApplicationController
|
||||||
params.permit(:query, :lat, :lon, :latlon_digits, :zoom, :minlat, :minlon, :maxlat, :maxlon)
|
params.permit(:query, :lat, :lon, :latlon_digits, :zoom, :minlat, :minlon, :maxlat, :maxlon)
|
||||||
end
|
end
|
||||||
|
|
||||||
def nsew_to_decdeg(captures)
|
def to_decdeg(captures)
|
||||||
begin
|
ns = captures.fetch("ns").casecmp("s").zero? ? -1 : 1
|
||||||
Float(captures[0])
|
nsd = captures.fetch("nsd", "0").to_f
|
||||||
lat = captures[2].casecmp("s").zero? ? -captures[0].to_f : captures[0].to_f
|
nsm = captures.fetch("nsm", "0").to_f
|
||||||
lon = captures[5].casecmp("w").zero? ? -captures[3].to_f : captures[3].to_f
|
nss = captures.fetch("nss", "0").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 ddm_to_decdeg(captures)
|
ew = captures.fetch("ew").casecmp("w").zero? ? -1 : 1
|
||||||
begin
|
ewd = captures.fetch("ewd", "0").to_f
|
||||||
Float(captures[0])
|
ewm = captures.fetch("ewm", "0").to_f
|
||||||
lat = captures[3].casecmp("s").zero? ? -(captures[0].to_f + (captures[1].to_f / 60)) : captures[0].to_f + (captures[1].to_f / 60)
|
ews = captures.fetch("ews", "0").to_f
|
||||||
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 = ns * (nsd + (nsm / 60) + (nss / 3600))
|
||||||
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 = ew * (ewd + (ewm / 60) + (ews / 3600))
|
||||||
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
|
|
||||||
|
|
||||||
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 }
|
{ :lat => lat, :lon => lon }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -60,6 +60,19 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
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
|
# Test identification of lat/lon pairs using N/W with degrees
|
||||||
def test_identify_latlon_nw_d
|
def test_identify_latlon_nw_d
|
||||||
|
@ -223,6 +236,31 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
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
|
# Test identification of US zipcodes
|
||||||
def test_identify_us_postcode
|
def test_identify_us_postcode
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue