X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/29dba7318a6c962da05751da42928d4096a73087..e5c5776e65f1a461d7d1aa678e843c9cf83316c2:/app/controllers/geocoder_controller.rb diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 312762216..43f276efa 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,13 +220,21 @@ 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 + ns = captures.fetch("ns").casecmp?("s") ? -1 : 1 nsd = BigDecimal(captures.fetch("nsd", "0")) nsm = BigDecimal(captures.fetch("nsm", "0")) nss = BigDecimal(captures.fetch("nss", "0")) - ew = captures.fetch("ew").casecmp("w").zero? ? -1 : 1 + ew = captures.fetch("ew").casecmp?("w") ? -1 : 1 ewd = BigDecimal(captures.fetch("ewd", "0")) ewm = BigDecimal(captures.fetch("ewm", "0")) ews = BigDecimal(captures.fetch("ews", "0"))