gpt4 book ai didi

python - 使用 GeoDjango 获取相关位置字段

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:39 24 4
gpt4 key购买 nike

我正在开发一个小应用程序,它使用 GeoDjango 来查找附近 field 的演出。这是我的 models.py:

from django.contrib.gis.db import models

class Venue(models.Model):
"""
Model for a venue
"""
name = models.CharField(max_length=200)
location = models.PointField()

def __str__(self):
return self.name


class Event(models.Model):
"""
Model for an event
"""
name = models.CharField(max_length=200)
datetime = models.DateTimeField()
venue = models.ForeignKey(Venue)

def __str__(self):
return "%s - %s" % (self.name, self.venue.name)

现在,我已经设法让它进行查找,在我看来,我需要将响应序列化为 geojson 才能将其渲染在 map 上。然而,我正在努力完成它。这是我当前的 views.py:

from django.shortcuts import render_to_response
from django.views.generic.edit import FormView
from gigs.forms import LookupForm
from gigs.models import Event
from django.utils import timezone
from django.contrib.gis.geos import Point
from django.contrib.gis.db.models.functions import Distance
from django.template import RequestContext

class LookupView(FormView):
form_class = LookupForm

def get(self, request):
return render_to_response('gigs/lookup.html', RequestContext(request))

def form_valid(self, form):
# Get data
latitude = form.cleaned_data['latitude']
longitude = form.cleaned_data['longitude']

# Get next week's date
next_week = timezone.now() + timezone.timedelta(weeks=1)

# Get Point
location = Point(latitude, longitude, srid=4326)

# Look up events
events = Event.objects.filter(datetime__lte=next_week).annotate(distance=Distance('venue__location', location)).order_by('distance')[0:5]

# Render the template
return render_to_response('gigs/lookupresults.html', {
'events': events
})

如果我在获取事件后插入断点:

ipdb> from gigs.models import Venue
ipdb> from django.core.serializers import serialize
ipdb> venues = Venue.objects.all()
ipdb> serialize('geojson', venues, geometry_field='location', fields=('name',))
'{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "geometry": {"type": "Point", "coordinates": [77.13845850820013, 88.27032065635657]}, "properties": {"name": "Venue1"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-144.48274402224723, -35.87841402981486]}, "properties": {"name": "Venue2"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [15.249714163005194, -39.942840871151624]}, "properties": {"name": "Venue3"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-63.376261279235095, -6.222101297964656]}, "properties": {"name": "Venue4"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [153.29028611820962, -4.285826286375041]}, "properties": {"name": "Venue5"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-146.08227004805758, 42.4843671723977]}, "properties": {"name": "Venue6"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [149.6004493621263, 34.740389078323844]}, "properties": {"name": "Venue7"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-144.38342519084884, -55.55425529324768]}, "properties": {"name": "Venue8"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [83.32120247931198, 48.78215628903402]}, "properties": {"name": "Venue9"}}, {"type": "Feature", "geometry": {"type": "Point", "coordinates": [-28.108531225500826, 35.50271671578547]}, "properties": {"name": "Venue10"}}]}'
ipdb> serialize('geojson', events, geometry_field='venue__location', fields=('name',))
'{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"type": "Feature", "geometry": null, "properties": {"name": "Event3"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event9"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event10"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event1"}}, {"type": "Feature", "geometry": null, "properties": {"name": "Event7"}}]}'

我可以获取 field 列表的位置,但它似乎不适用于从 Event 模型获取 field 的位置。

知道我哪里出错了吗?

最佳答案

希望这不会来得太晚,但答案是您不需要序列化为 GeoJSON 即可渲染到 map 中。如果您愿意,可以使用它,但还有很多其他方法。

例如,您可以创建一个 JSON 数组,其中每个元素都是纬度、经度坐标对,您的 JavaScript 可以迭代该数组并将其添加到 map 中。

关于python - 使用 GeoDjango 获取相关位置字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238692/

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