G
G
Gasoid2012-08-21 16:17:03
Python
Gasoid, 2012-08-21 16:17:03

How to determine if a geopoint (with latitude, longitude) is included in the radius?

Those. there is a circle on the map with a center, for example [51.526613503445766, 46.02093849218558]
there is a radius of 5 km.
It is required to determine which points are included in this radius, which are not.
I found a library for determining the distance.
code.google.com/p/geopy

Answer the question

In order to leave comments, you need to log in

10 answer(s)
G
Gasoid, 2012-08-21
@Gasoid

In general, the algorithm is as follows, there is a database with users, each user has coordinate fields lat, lon
, for example, you need to find all users in a square of 20 km
example for django

        dist = 20 #дистанция 20 км
        mylon = 51.5289156201 # долгота центра
        mylat = 46.0209384922 # широта 
        lon1 = mylon-dist/abs(math.cos(math.radians(mylat))*111.0) # 1 градус широты = 111 км
        lon2 = mylon+dist/abs(math.cos(math.radians(mylat))*111.0)
        lat1 = mylat-(dist/111.0)
        lat2 = mylat+(dist/111.0)
        profiles = UserProfile.objects.filter(lat__range=(lat1, lat2)).filter(lon__range=(lon1, lon2))

S
Skull, 2012-08-21
@Skull

Why a library? There is a formula in 1 line that determines the distance in meters.
$distance = 2 * asin(sqrt( pow(sin(deg2rad( ($lat1-$lat2) / 2)), 2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
pow( sin(deg2rad(($lng1- $lng2) / 2)), 2))) * 6378245;
If the points are stored in the database, choose not all, but only within certain limits. And for them already calculate the distance

A
alexmuz, 2012-08-21
@alexmuz

Consider the distance from the point to the center of your circle, if the distance is less than or equal to 5 km, then it is included, if more, then it is not included.

G
Gasoid, 2012-08-21
@Gasoid

On optimizing search by point-to-square for Mysql
www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

A
Alexander Skakunov, 2013-01-07
@alex4

There is a small, but still difference between "enters the radius" and "enters the square". The square solution is simpler, it doesn’t need serious GIS artillery at all, and it works with lightning speed.
This is done like this:
0. I assume that the coordinates of the point in radians are stored in the database, in the lat and lng fields.
1. Create one index for both fields to speed up queries.
2. In the code, convert 10 km to radians (it seems that there are 0.1988 radians in one meter, but check).
3. To determine which points are included in a square with a side of 10 km with a given point in the center, write the following query:

SET @lat = 51.526613503445766; # дано в условии
SET @lng = 46.02093849218558;
SET @half= [10 км в радианах] / 2 ;


SELECT id
FROM points
WHERE lat BETWEEN @lat - @half AND @lat + @half
  AND lng BETWEEN @lng - @half AND @lng + @half;

The first 3 lines (SET) must be removed and instead of them in the request, use variables from your application server language (Python, as I understand it).
If you need an exact value (the earth is not a flat ball) or a radius, then see the solutions of other authors.

A
Alexey Huseynov, 2012-08-21
@kibergus

This is not where you should start. What are the requirements for the accuracy and speed of the algorithm? How many points should be checked for occurrence in one circle? Does it matter to you the flattening of the Earth, as well as the relief? Do you understand that a circle on the map will not look like a circle?

I
iwitaly, 2012-08-21
@iwitaly

If I understood you correctly, just from the circle equation (x - x_0)^2 + (y - y_0)^2 <= R^2 where x_0 and y_0 are the coordinates of the center in your coordinate system. Just take the coordinates of the point and check.

W
webmeddoc, 2019-11-09
@webmeddoc

Answered on freelancing, in short it would look like this:

import  math
dist = math.hypot(x-Sx, y-Sy)
if  dist < r: print("Входит")
else: print("Не входит")

S
spleanfan, 2019-11-11
@spleanfan

so you need to answer here or on fl?

H
Hoksmur, 2019-11-26
@Hoksmur

Whether or not to use data from external resources, such as from Google or otherwise, is up to the TS.
If you do not use or it is permissible to take the sphere for the surface of the Earth, then you must calculate the length of the arc on the sphere between two points. The wiki formula is easy to find, even in latitude and longitude. 349227e7ebb7664d6354a0b970956f994a71f4b6
You can even get rid of trigonometry by leaving +-*/ if a linear approximation is acceptable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question