Passing in lng and lat via URL, integers work but not floats

Using the following gode I am able to get an response
when the lng and lat are integers, but not when they are floats.

http://localhost:3000/section/query/43,-90 # works!

http://localhost:3000/section/query/43.005,-90.336 # does not work :frowning:

How do I pass in lat and lng parameters from the url?

Thanks in advance!

  • Pete

#section_controller.rb
class SectionController < ApplicationController

def query
lat = params[:lat]
lng = params[:lng]
s = Section.find_by_sql(“SELECT twp, rng, sec FROM sections WHERE
distance( the_geom,‘POINT(#
{lng} #{lat})’) = 0”)
site = s[0]
loc = site.attributes
@twp = loc[“twp”]
@rng = loc[“rng”]
@sec = loc[“sec”]
render :action => ‘query’
end

end


section.rb

class Section < ActiveRecord::Base
set_primary_key “gid”
end


query.rhtml

Query Results<%= controller.action_name %> <%= stylesheet_link_tag 'scaffold' %>

<%= flash[:notice] %>

Township: <%= h @twp %>

Range: <%= h @rng %>

Section: <%= h @sec %>


routes.rb

ActionController::Routing::Routes.draw do |map|

map.connect ‘:controller/:action/:lat,:lng’

end


You’ll should escape the string:

link_to(:action => ‘query’, :controller => ‘section’, :lat =>
url_encode(lat), :lon => url_encode(lon))

However, it seems the only unsafe character you have is the comma. If
you
split into lat and lon as I did above, all should be fine for reassembly
in
your controller as:

@whatever = Section.find_by_lat_lon(params[:lat] + ‘,’ params[:lon])
#assuming method exists :slight_smile:

Pete DeVries-2 wrote:

s = Section.find_by_sql("SELECT twp, rng, sec FROM sections WHERE

end

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<%= flash[:notice] %>

ActionController::Routing::Routes.draw do |map|


View this message in context:
http://www.nabble.com/Passing-in-lng-and-lat-via-URL%2C-integers-work-but-not-floats-tf3554838.html#a9930745
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

Routing Errors with lat,lon. Integers work but not numbers with
decimals.
I suspect that something needs to be modified in the routing to deal
with decimal points.

Any suggestions! Thanks in advance - Pete

Here is the behavior and code…

http://localhost:3000/table_points/query/43,-90 ← no decimal points
works

renders page with…

Township: 6
Range: 4
Section: 15

http://localhost:3000/table_points/query/43.05,-90.33 ← decimals
points create routing error
renders page with …

Routing Error

no route found to match “/table_points/query/43.05,-90.33” with
{:method=>:get}

routes.rb

Install the default route as the lowest priority.

map.connect ‘table_points/query/:latitude,:longitude’, :controller
=> ‘table_points’, :action => ‘query’

table_points_controller.rb

def query
s = Section.find_by_sql(“SELECT twp, rng, sec FROM sections WHERE
distance( the_geom,‘POINT(#{lon} #{lat})’) = 0”)
site = s[0]
loc = site.attributes
@twp = loc[“twp”]
@rng = loc[“rng”]
@sec = loc[“sec”]
render :action => ‘query’
end

Use url_encode to clean up your url. It’s probably the comma that’s
causing
you grief.

Pete DeVries-2 wrote:

http://localhost:3000/table_points/query/43.05,-90.33 ← decimals

Install the default route as the lowest priority.

@twp = loc["twp"]

Pete DeVries-2 wrote:

Thanks in advance!
distance( the_geom,'POINT(#

   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


View this message in context:
http://www.nabble.com/Passing-in-lng-and-lat-via-URL%2C-integers-work-but-not-floats-tf3554838.html#a9969772
Sent from the RubyOnRails Users mailing list archive at Nabble.com.

On Apr 12, 12:42 pm, “Pete” [email protected] wrote:

Routing Errors with lat,lon. Integers work but not numbers with
decimals.
I suspect that something needs to be modified in the routing to deal
with decimal points.

Any suggestions! Thanks in advance - Pete

the periods in URLs issue discussed a couple threads down:

http://skwpspace.com/2007/02/27/rails-12-breaks-url-routing-with-dots/
http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/6e8c2c7c9f5c69a6/#

I tried my code without using decimals by multiplying the longitude
and latitudes by 1000 and
the dividing them again once they were passed to the controller. This
works so it seems
the decimal point was the problem.

However using the “requirements” in the routes.rb does not seem to fix
the problem. I still have a routing error, below is the error message
and my routes.rb

no route found to match “/table_points/query/43.05,-90.89” with
{:method=>:get}

routes.rb

map.connect ‘table_points/query/:latitude,:longitude’, :controller
=> ‘table_points’, :action => ‘query’, :requirements => { :latitude
=> /./, :longitude => /./ }

Any suggestions would be greatly appreciated :slight_smile:

Thanks in Advance,

-Pete