简体   繁体   English

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

[英]Django - determining if geographic coordinates are inside of an circle

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)? django是否有任何可以查看地理坐标(十进制纬度/长度)并确定是否在具有一定半径的圆圈内(比如100公里)?

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. 我本可以自己编写一些可以处理这个问题的东西但是如果有一些已经写好的东西可以解决这个问题我会徘徊。

This problem can be solved in pure SQL if you dont mind about very good precision. 如果您不介意非常好的精度,可以在纯SQL中解决此问题。

You can find points around a GPS position with this specific SQL query : 您可以使用此特定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)

This will give you all records with gps records in a 50km area. 这将为您提供50公里区域内所有gps记录的记录。

You can easily plug this in django with : 你可以在django中轻松插入:

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

or with django raw queries 或者使用django原始查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM