gpt4 book ai didi

python - Django & Geopy : all distance listed for each post : how to associated one distance to one post

转载 作者:行者123 更新时间:2023-12-03 20:48:13 29 4
gpt4 key购买 nike

Django——地质学
我正在寻找如何定义两点之间的距离。第一个与帖子本身有关,不会因每个帖子而改变。它表示帖子的位置。第二个将与用户的位置相关联。
我想计算发布和用户的距离。
问题:假设我已连接:每个帖子的所有距离都列出
我得到了一篇文章:
[459.414, 459.414, 459.414, 0.605, 0.605, 0.605, 0.605, 0.605, 459.414, 0.605] 公里

user/models.py
class UserProfile(models.Model):
...
latitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
longitude = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')

post/models.py
class Cuisine(models.Model):
...
latitude_user = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
longitude_user = models.DecimalField(max_digits=9, decimal_places=6, blank=True, default='0')
感谢@tgrandje,我解决了一些从 UserProfile 中寻找值(value)的问题。 :
问题是我总是为每个帖子获得相同的距离。
有什么建议吗?
def Homemainpage(request):

post = Cuisine.objects.filter(status=0).order_by('-publishing_date').all() #All cuisine

if request.user.is_authenticated:
if request.user.userprofile.full_address:
distance = []
for p in post:
post_lon = p.longitude_user
post_lat = p.latitude_user

post_situation = (post_lon, post_lat)

user_lon = UserProfile.objects.filter(user=request.user)[0].longitude
user_lat = UserProfile.objects.filter(user=request.user)[0].latitude

user_situation = (user_lon, user_lat)

distance.append(round(geodesic(post_situation, user_situation).km, 3))

# Pre commande pour demain
#post = Cuisine.objects.filter(status=0).filter(start_date=now().date() + timedelta(days=1)).order_by('-publishing_date').all()
# En commande pour aujourd'hui
#post = Cuisine.objects.filter(status=0).filter(start_date=now().date()).order_by('-publishing_date').all()

paginator = Paginator(post, 3)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)

listaliments = ListAliments.objects.filter(status=1).all()
typerepas = Typerepas.objects.filter(status=1).all()
sperepas = Sperepas.objects.filter(status=1).all()

return render(request, 'index.html', {
'post': post,
'listaliments': listaliments,
'typerepas': typerepas,
'sperepas': sperepas,
'page_obj': page_obj,
'today' : now().date(),
'tomorrow': now().date() + timedelta(days=1),
'distance': distance,
})

最佳答案

如果您准备好所有纬度/经度,则可以使用 pyproj ,它非常有效:

from pyproj import Geod
...
user_lon = UserProfile.objects.filter(user=self.request.user).get(self.longitude)
user_lat = UserProfile.objects.filter(user=self.request.user).get(self.latitude)

post_lon = Cuisine.objects.filter(slug=slug).get(self.longitude_user)
post_lat = Cuisine.objects.filter(slug=slug).get(self.latitude_user)

wgs84_geod = Geod(ellps='WGS84')
*_args, dist_meters = wgs84_geod.inv(user_lon,user_lat,post_lon,post_lat)
print(dist_meters)

编辑
def Homemainpage(request):

post = Cuisine.objects.filter(status=0).order_by('-publishing_date').all() #All cuisine

...
user_lon = UserProfile.objects.filter(user=request.user)[0].longitude
user_lat = UserProfile.objects.filter(user=request.user)[0].latitude

wgs84_geod = Geod(ellps='WGS84')
kitchen_distance_dict = dict()
for kitchen in Cuisine.objects.all():
post_lon = kitchen.longitude
post_lat = kitchen.latitude
*_args, dist_meters = wgs84_geod.inv(user_lon,user_lat,post_lon,post_lat)

kitchen_distance_dict[kitchen.pk] = dist_meters / 1000

return render(request, 'index.html', {
'post': post,
'listaliments': listaliments,
'typerepas': typerepas,
'sperepas': sperepas,
'page_obj': page_obj,
'distance': kitchen_distance_dict,
})

关于python - Django & Geopy : all distance listed for each post : how to associated one distance to one post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64556869/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com