Gandalf StormCrow Gandalf StormCrow - 3 months ago 8x
Ruby Question

How to check if lat long is in the city limits

How can I check if my lat/long is in the city limits or example, Greater London is enclosed by:


Source :

How can I check if a location (lat/lon):


Is there a gem already for this? Or what would be the best way to do this?

Bounty update:

I have a working solution but I feel its not the right one, I'm using geocoder gem, and my user has geocoded_by, and lat/long attributes. So here is how I do it :

def self.user_from_dublin(user_id)
near([53.349937,-6.261917], 15).pluck(:id).include?(user_id)

So this loads all users from Dublin and compares the id of the user to those users in Dublin. If the id is found the user is considered to be from Dublin.

I don't think this will work well when there is more users.


I'm using postgres database


If its a boolean why not use where to limit the results to only your user. This way it will just involve one SQL query that will either be empty or have a size of 1 (your user being the only record)

def self.user_from_dublin(user_id)
  !(near([53.349937,-6.261917], 15).where(id: user_id).empty?)

empty? will be true when your user is not within your boundary, so the ! at the beginning will negate this and your function will return false when the user is not within the boundary.