简体   繁体   English

使用来自第三方api和geodjango的JSON响应

[英]Using the JSON response from 3rd party api with geodjango

I am try to create a way to narrow down posts on a django app by location & radius. 我试图通过位置和半径创建一种缩小django app上的帖子的方法。 The way I am trying to do this is: 我试图这样做的方式是:

  • Use 3rd party (OpenCage) API to convert location to lat / lng 使用第三方(OpenCage)API将位置转换为lat / lng
  • Gather the JSON response, filter for the most correct one (first response in US) 收集JSON响应,过滤最正确的响应(美国的第一个响应)
  • Use the response lat & lng to filter listings via GIS 使用响应lat&lng通过GIS过滤列表

What I have done is successfully connect to the API, and get a lat & lng back, however I am having to filter the response in my templates rather than in my models. 我所做的是成功连接到API,并获得一个lat&lng,但是我不得不在模板中而不是在我的模型中过滤响应。 I have also found the following which appears to be the solution needed for narrowing results in a radius: 我还发现以下似乎是缩小半径结果所需的解决方案:

from django.contrib.gis.geos import Point
from django.contrib.gis.measure import Distance  


lat = 52.5
lng = 1.0
radius = 10
point = Point(lng, lat)    
Place.objects.filter(location__distance_lt=(point, Distance(km=radius)))

What I need to solve, and am hoping for your help with is how do I 我需要解决的问题是,我希望得到你的帮助

  1. filter the api responses and only get what I need from them (components country, geometry lat, geometry lng) 过滤api响应,只得到我需要的东西(组件国家,几何lat,几何lng)

  2. gather the lat & lng for use in the above code? 收集lat&lng用于上面的代码?

views.py views.py

from posts import services


def search(request):
    queryset_list = posts.objects.order_by('id')
    if 'location' in request.GET:
        q = request.GET['location']
        locations_list = services.get_location(q)
        context = {
            'posts': queryset_list,
            'locations_list': locations_list
        }
    return render(request, 'posts/search.html', context)

services.py services.py

import requests

def get_location(location):
    url = 'https://api.opencagedata.com/geocode/v1/json'
    params = {'q': location, 'key': '###', 'language': 'en', 'pretty': 1}
    r = requests.get(url, params=params)
    locations = r.json()
    return locations

I think you can try like this(Based on demo JSON response provided by OpenCage): 我想你可以这样试试(基于OpenCage提供的演示JSON响应):

import requests
from django.db.models import Q

def get_location(location):
    url = 'https://api.opencagedata.com/geocode/v1/json'
    params = {'q': location, 'key': '###', 'language': 'en', 'pretty': 1}
    r = requests.get(url, params=params)
    locations = r.json()
    filter_query = Q()
    radius = 10
    for item in locations.get('results', []):
         geo = item.get('geometry')
         lat = geo.get('lat')
         lng = geo.get('lng')
         point = Point(lng, lat)    
         filter_query |= Q(location__distance_lt=(point, Distance(km=radius)))
    return Place.objects.filter(filter_query)

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

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