From 810ad909ce932998eabea75d56ebbc0f3256fc98 Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Mon, 31 Mar 2025 03:07:41 +0300 Subject: [PATCH] Create latlon search query resource --- app/abilities/ability.rb | 2 +- app/controllers/geocoder_controller.rb | 46 +--------------- .../searches/latlon_queries_controller.rb | 43 +++++++++++++++ config/routes.rb | 3 +- test/abilities/abilities_test.rb | 2 +- test/controllers/geocoder_controller_test.rb | 47 ---------------- .../latlon_queries_controller_test.rb | 55 +++++++++++++++++++ 7 files changed, 102 insertions(+), 96 deletions(-) create mode 100644 app/controllers/searches/latlon_queries_controller.rb create mode 100644 test/controllers/searches/latlon_queries_controller_test.rb diff --git a/app/abilities/ability.rb b/app/abilities/ability.rb index c605ee9b4..8c943b880 100644 --- a/app/abilities/ability.rb +++ b/app/abilities/ability.rb @@ -10,7 +10,7 @@ class Ability can :search, :direction can [:index, :permalink, :edit, :help, :fixthemap, :offline, :export, :about, :communities, :preview, :copyright, :id], :site can [:finish, :embed], :export - can [:create, :search, :search_latlon, :search_osm_nominatim, :search_osm_nominatim_reverse], :geocoder + can [:create, :search, :search_osm_nominatim, :search_osm_nominatim_reverse], :geocoder if Settings.status != "database_offline" can [:read, :feed], Changeset diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 9b8637a95..7f5416e8d 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -16,7 +16,7 @@ class GeocoderController < ApplicationController if params[:lat] && params[:lon] @sources.push(:name => "latlon", :url => root_path, - :fetch_url => url_for(params.permit(:lat, :lon, :latlon_digits, :zoom).merge(:action => "search_latlon"))) + :fetch_url => search_latlon_query_path(params.permit(:lat, :lon, :latlon_digits, :zoom))) @sources.push(:name => "osm_nominatim_reverse", :url => nominatim_reverse_query_url(:format => "html"), :fetch_url => url_for(params.permit(:lat, :lon, :zoom).merge(:action => "search_osm_nominatim_reverse"))) elsif params[:query] @@ -31,50 +31,6 @@ class GeocoderController < ApplicationController end end - def search_latlon - lat = params[:lat].to_f - lon = params[:lon].to_f - - if params[:latlon_digits] - # We've got two nondescript numbers for a query, which can mean both "lat, lon" or "lon, lat". - @results = [] - - if lat.between?(-90, 90) && lon.between?(-180, 180) - @results.push(:lat => params[:lat], :lon => params[:lon], - :zoom => params[:zoom], - :name => "#{params[:lat]}, #{params[:lon]}") - end - - if lon.between?(-90, 90) && lat.between?(-180, 180) - @results.push(:lat => params[:lon], :lon => params[:lat], - :zoom => params[:zoom], - :name => "#{params[:lon]}, #{params[:lat]}") - end - - if @results.empty? - @error = "Latitude or longitude are out of range" - render "searches/queries/error" - else - render "searches/queries/create" - end - else - # Coordinates in a query have come with markers for latitude and longitude. - if !lat.between?(-90, 90) - @error = "Latitude #{lat} out of range" - render "searches/queries/error" - elsif !lon.between?(-180, 180) - @error = "Longitude #{lon} out of range" - render "searches/queries/error" - else - @results = [{ :lat => params[:lat], :lon => params[:lon], - :zoom => params[:zoom], - :name => "#{params[:lat]}, #{params[:lon]}" }] - - render "searches/queries/create" - end - end - end - def search_osm_nominatim # ask nominatim response = fetch_xml(nominatim_query_url(:format => "xml")) diff --git a/app/controllers/searches/latlon_queries_controller.rb b/app/controllers/searches/latlon_queries_controller.rb new file mode 100644 index 000000000..6bf221184 --- /dev/null +++ b/app/controllers/searches/latlon_queries_controller.rb @@ -0,0 +1,43 @@ +module Searches + class LatlonQueriesController < QueriesController + def create + lat = params[:lat].to_f + lon = params[:lon].to_f + + if params[:latlon_digits] + # We've got two nondescript numbers for a query, which can mean both "lat, lon" or "lon, lat". + @results = [] + + if lat.between?(-90, 90) && lon.between?(-180, 180) + @results.push(:lat => params[:lat], :lon => params[:lon], + :zoom => params[:zoom], + :name => "#{params[:lat]}, #{params[:lon]}") + end + + if lon.between?(-90, 90) && lat.between?(-180, 180) + @results.push(:lat => params[:lon], :lon => params[:lat], + :zoom => params[:zoom], + :name => "#{params[:lon]}, #{params[:lat]}") + end + + if @results.empty? + @error = "Latitude or longitude are out of range" + render :action => "error" + end + else + # Coordinates in a query have come with markers for latitude and longitude. + if !lat.between?(-90, 90) + @error = "Latitude #{lat} out of range" + render :action => "error" + elsif !lon.between?(-180, 180) + @error = "Longitude #{lon} out of range" + render :action => "error" + else + @results = [{ :lat => params[:lat], :lon => params[:lon], + :zoom => params[:zoom], + :name => "#{params[:lat]}, #{params[:lon]}" }] + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index e0119a981..c947a7a4d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -334,12 +334,11 @@ OpenStreetMap::Application.routes.draw do # geocoder get "/search" => "geocoder#search" - post "/geocoder/search_latlon" => "geocoder#search_latlon" post "/geocoder/search_osm_nominatim" => "geocoder#search_osm_nominatim" post "/geocoder/search_osm_nominatim_reverse" => "geocoder#search_osm_nominatim_reverse" resource :search, :only => [] do scope :module => :searches do - # TODO + resource :latlon_query, :only => :create end end diff --git a/test/abilities/abilities_test.rb b/test/abilities/abilities_test.rb index 297d9fc4f..f6cba39e2 100644 --- a/test/abilities/abilities_test.rb +++ b/test/abilities/abilities_test.rb @@ -9,7 +9,7 @@ class GuestAbilityTest < AbilityTest test "geocoder permission for a guest" do ability = Ability.new nil - [:create, :search, :search_latlon, :search_osm_nominatim, + [:create, :search, :search_osm_nominatim, :search_osm_nominatim_reverse].each do |action| assert ability.can?(action, :geocoder), "should be able to #{action} geocoder" end diff --git a/test/controllers/geocoder_controller_test.rb b/test/controllers/geocoder_controller_test.rb index 6c8f576e7..3cca57db0 100644 --- a/test/controllers/geocoder_controller_test.rb +++ b/test/controllers/geocoder_controller_test.rb @@ -8,10 +8,6 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest { :path => "/search", :method => :get }, { :controller => "geocoder", :action => "search" } ) - assert_routing( - { :path => "/geocoder/search_latlon", :method => :post }, - { :controller => "geocoder", :action => "search_latlon" } - ) assert_routing( { :path => "/geocoder/search_osm_nominatim", :method => :post }, { :controller => "geocoder", :action => "search_osm_nominatim" } @@ -323,49 +319,6 @@ class GeocoderControllerTest < ActionDispatch::IntegrationTest search_check "foo bar baz", %w[osm_nominatim] end - ## - # Test the builtin latitude+longitude search - def test_search_latlon - post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true - results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 - - post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true - results_check_error "Latitude -91.23 out of range" - - post geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true - results_check_error "Latitude 91.23 out of range" - - post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true - results_check_error "Longitude -180.23 out of range" - - post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true - results_check_error "Longitude 180.23 out of range" - end - - def test_search_latlon_digits - post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true - results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 }, - { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 }) - - post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true - results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16 - - post geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true - results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16 - - post geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true - results_check_error "Latitude or longitude are out of range" - - post geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true - results_check_error "Latitude or longitude are out of range" - - post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true - results_check_error "Latitude or longitude are out of range" - - post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true - results_check_error "Latitude or longitude are out of range" - end - ## # Test the nominatim forward search def test_search_osm_nominatim diff --git a/test/controllers/searches/latlon_queries_controller_test.rb b/test/controllers/searches/latlon_queries_controller_test.rb new file mode 100644 index 000000000..ee13c7862 --- /dev/null +++ b/test/controllers/searches/latlon_queries_controller_test.rb @@ -0,0 +1,55 @@ +require_relative "queries_controller_test" + +module Searches + class LatlonQueriesControllerTest < QueriesControllerTest + ## + # test all routes which lead to this controller + def test_routes + assert_routing( + { :path => "/search/latlon_query", :method => :post }, + { :controller => "searches/latlon_queries", :action => "create" } + ) + end + + def test_create + post search_latlon_query_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true + results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 + + post search_latlon_query_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true + results_check_error "Latitude -91.23 out of range" + + post search_latlon_query_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true + results_check_error "Latitude 91.23 out of range" + + post search_latlon_query_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true + results_check_error "Longitude -180.23 out of range" + + post search_latlon_query_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true + results_check_error "Longitude 180.23 out of range" + end + + def test_create_digits + post search_latlon_query_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true + results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 }, + { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 }) + + post search_latlon_query_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true + results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16 + + post search_latlon_query_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true + results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16 + + post search_latlon_query_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true + results_check_error "Latitude or longitude are out of range" + + post search_latlon_query_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true + results_check_error "Latitude or longitude are out of range" + + post search_latlon_query_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true + results_check_error "Latitude or longitude are out of range" + + post search_latlon_query_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true + results_check_error "Latitude or longitude are out of range" + end + end +end -- 2.39.5