Django - 确定地理坐标是否在圆内

发布于 2024-10-10 13:06:18 字数 187 浏览 0 评论 0原文

django 是否有任何东西可以查看地理坐标(十进制纬度/经度)并确定是否在具有特定半径(假设 100 公里)的圆内?

我有某种类型的数据,每个数据都有一个纬度/经度,我想在数据库中进行搜索,看看该数据是否位于具有指定半径大小的圆内部。

我可能可以自己写一些东西来处理这个问题,但是我想知道是否已经写了一些东西可以处理这个问题。

Does django have anything that will look at a geographic coordinate (decimal lat/long) and determine if is inside a circle with a certain radius (let's say 100 Km)?

I have certain type of data, each has a lat/long and I would like to make a search in the database to see if that data is located inside of a circle with a specified radius size.

I could probably write something myself that will handle this but I wander if there is something written already that will handle this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨与橙与城 2024-10-17 13:06:18

如果你不介意非常好的精度,这个问题可以用纯 SQL 来解决。

您可以使用此特定 SQL 查询查找 GPS 位置周围的点:

# find point around :
latitude = 46.2037010192871
longitude = 5.20353984832764
query= "SELECT ID, NOM, LAT, LON, 3956 * 2 * ASIN(SQRT(POWER(SIN((%s - LAT) * 0.0174532925 / 2), 2) + COS(%s * 0.0174532925) * COS(LAT * 0.0174532925) * POWER(SIN((%s - LON) * 0.0174532925 / 2), 2) )) as distance from POI  having distance < 50 ORDER BY distance ASC " % ( latitude, latitude, longitude)

这将为您提供 50 公里区域内包含 GPS 记录的所有记录。

您可以使用 :

from django.db import connection
cursor = connection.cursor()
cursor.execute( query )
rows = cursor.fetchall()

django 轻松地将其插入 django 中原始查询

This problem can be solved in pure SQL if you dont mind about very good precision.

You can find points around a GPS position with this specific SQL query :

# find point around :
latitude = 46.2037010192871
longitude = 5.20353984832764
query= "SELECT ID, NOM, LAT, LON, 3956 * 2 * ASIN(SQRT(POWER(SIN((%s - LAT) * 0.0174532925 / 2), 2) + COS(%s * 0.0174532925) * COS(LAT * 0.0174532925) * POWER(SIN((%s - LON) * 0.0174532925 / 2), 2) )) as distance from POI  having distance < 50 ORDER BY distance ASC " % ( latitude, latitude, longitude)

This will give you all records with gps records in a 50km area.

You can easily plug this in django with :

from django.db import connection
cursor = connection.cursor()
cursor.execute( query )
rows = cursor.fetchall()

or with django raw queries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文