How to check if lat long is in the city limits

2020-06-25 04:46发布

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

[bbox=-0.489,51.28,0.236,51.686]

Source :

http://wiki.openstreetmap.org/wiki/Bounding_Box

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

51.55238,0.047032

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)
end

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.

Update

I'm using postgres database

9条回答
Lonely孤独者°
2楼-- · 2020-06-25 05:18

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?)
end

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.

查看更多
家丑人穷心不美
3楼-- · 2020-06-25 05:19

You don't need anything special for this.

Bounding Box is a rectangle:

bbox = left,bottom,right,top

  +-----------(10,0)
  |              |
  |              |
  |              |
  |              |
  |              |
(0,10)-----------+

The weird (left-bottom) to (top-right) numbering is because, Longitude is measured left-to-right (to simplify, -180° is left of the globe, +180° is right of the globe), whereas Latitude is measured bottom-to-top (to simplify, -90° is bottom of the globe, +90° is top of the globe).

So as per the example,

(0,10)   = left, bottom = min longitude, min latitude
(10,10)  = right, top   = max longitude, max latitude

Now you want to know whether a point (5,5) falls inside it.

  +-----------(10,10)
  |              |
  |              |
  |     (5,5)    |
  |              |
  |              |
(0,0)------------+

So all you have to do is just create a table for cities in Postgres (or any database for that matter):

CREATE TABLE cities(
  city_name      VARCHAR(xxx),
  min_longitude  DECIMAL,
  min_latitude   DECIMAL,
  max_longitude  DECIMAL,
  max_latitude   DECIMAL
);

Now suppose you want to find out which city falls under a position latitude=5, longitude=6, then just use the following query:

SELECT city_name 
FROM cities 
WHERE (min_latitude  >= 6 AND max_latitude  <= 6)
AND   (min_longitude >= 5 AND max_longitude <= 5)

Substitute 5 and 6 with the user's latitude/longitude, and you will get the city where the user falls under.

EDIT: Small mix-up, minor formatting

查看更多
Rolldiameter
4楼-- · 2020-06-25 05:19

You don't exactly say... but if what you want is the name of the nearest city to a Lat/Lon coordinate anywhere, then you could use google maps Geocoder services - set up a GeocoderRequest with a LatLng to search and issue a request.

查看更多
登录 后发表回答