From: Anton Khorev Date: Sat, 13 Jul 2024 14:40:01 +0000 (+0300) Subject: Parse lat and lon independently when using dms notation X-Git-Tag: live~376^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/7917a7db800eec3b35a71ceb74404dde1518a9dc Parse lat and lon independently when using dms notation --- diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 312762216..02ebc8a1b 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -206,13 +206,9 @@ 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+)?)°?$/) || # [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) + if latlon = query.match(/^(?[NS])\s*#{dms_regexp('ns')}\W*(?[EW])\s*#{dms_regexp('ew')}$/) || + query.match(/^#{dms_regexp('ns')}\s*(?[NS])\W*#{dms_regexp('ew')}\s*(?[EW])$/) + params.merge!(to_decdeg(latlon.named_captures.compact)).delete(:query) elsif latlon = query.match(%r{^(?[+-]?\d+(?:\.\d+)?)(?:\s+|\s*[,/]\s*)(?[+-]?\d+(?:\.\d+)?)$}) params.merge!(:lat => latlon["lat"], :lon => latlon["lon"]).delete(:query) @@ -224,6 +220,14 @@ class GeocoderController < ApplicationController params.permit(:query, :lat, :lon, :latlon_digits, :zoom, :minlat, :minlon, :maxlat, :maxlon) end + def dms_regexp(name_prefix) + / + (?: (?<#{name_prefix}d>\d{1,3}(?:\.\d+)?)°? ) | + (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2}(?:\.\d+)?)['′]? ) | + (?: (?<#{name_prefix}d>\d{1,3})°?\s*(?<#{name_prefix}m>\d{1,2})['′]?\s*(?<#{name_prefix}s>\d{1,2}(?:\.\d+)?)["″]? ) + /x + end + def to_decdeg(captures) ns = captures.fetch("ns").casecmp("s").zero? ? -1 : 1 nsd = BigDecimal(captures.fetch("nsd", "0")) diff --git a/test/controllers/geocoder_controller_test.rb b/test/controllers/geocoder_controller_test.rb index 4d5d345c3..5ea02f76d 100644 --- a/test/controllers/geocoder_controller_test.rb +++ b/test/controllers/geocoder_controller_test.rb @@ -261,6 +261,17 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest end end + # + # Test identification of lat/lon pairs with mixed precision + def test_identify_latlon_ne_mixed_precision + latlon_check "N1 5 E15", 1.083333, 15 + latlon_check "N1 5 9 E15", 1.085833, 15 + latlon_check "N1 5 9 E1 5", 1.085833, 1.083333 + latlon_check "N15 E1 5", 15, 1.083333 + latlon_check "N15 E1 5 9", 15, 1.085833 + latlon_check "N1 5 E1 5 9", 1.083333, 1.085833 + end + # # Test identification of lat/lon pairs with values close to zero def test_identify_latlon_close_to_zero