Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django&;地理位置:为每个桩列出的所有距离:如何将一个距离关联到一个桩_Python_Django_Distance_Geopy - Fatal编程技术网

Python Django&;地理位置:为每个桩列出的所有距离:如何将一个距离关联到一个桩

Python Django&;地理位置:为每个桩列出的所有距离:如何将一个距离关联到一个桩,python,django,distance,geopy,Python,Django,Distance,Geopy,Django——地质学 我正在寻找如何定义两点之间的距离。第一个与帖子本身相关,不会因每个帖子而改变。它表明了职位的位置。第二个将链接到用户的位置 我想计算距离btw职位和用户 问题:假设我已连接:每个帖子都列出了所有距离 我得到一个职位: [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):

Django——地质学

我正在寻找如何定义两点之间的距离。第一个与帖子本身相关,不会因每个帖子而改变。它表明了职位的位置。第二个将链接到用户的位置

我想计算距离btw职位和用户

问题:假设我已连接:每个帖子都列出了所有距离

我得到一个职位: [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
中找到了价值: 问题是我每次发帖子的距离都是一样的。 有什么建议吗

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,
  })

有人能帮我吗?你能发布你的视图的完整代码吗?如果你记录计算的距离值,你能在你的日志/控制台中看到它吗?嗨@D Malan,这是我在我的视图中对此的所有内容。我在视图的头部添加了测地线。如果我想计算btw post和post用户之间的距离,那么代码在post/models.py中工作。结果=0,因为后期本地化=用户本地化。我的主要问题是我想根据self.request.user显示结果。如果用户A正在登录,距离需要考虑到他的位置。如果你想让我发布其他内容,我很乐意这样做:)为什么模型会有self.request?为什么会否决?问题是“如何定义两点之间的距离”,而不是创建关于位置的模型……Hi@tgrandje感谢您的回答:)问题是:我无法获取连接到网站的每个用户配置文件的数据(经度/纬度)。我知道如何使用Geopy计算距离,但在我看来,我无法得到它。你知道吗?谢谢,顺便说一句,我不知道Geod的事。我来看看,我的坏,我不明白你的问题,那么!如果在上下文中推送user_lon/lat和post_lon/lat,是否可以显示它们?让我们来看看。