Reputation: 9476
I'm working on a little application that uses GeoDjango to find gigs at venues nearby. Here's my 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)
Now, I've managed to get it to do the lookup, and it looks to me like I need to serialize the response into geojson in order to render it on a map. However, I'm struggling to get it done. Here's my current 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
})
If I insert a breakpoint after I get the 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"}}]}'
I can get the locations for the list of venues, but it doesn't seem to work for getting the location for a venue from the Event
model.
Any idea where I went wrong?
Upvotes: 1
Views: 650
Reputation: 53734
Hope this doesn't come too late, but the answer is that you don't need to serialize into GeoJSON to render into the map. You could use it if you wanted to but there are plenty of other ways.
For example, you could make a JSON array where each element is an lat,lng coordinate pair and your javascript can iterate through the same and add to the map.
Upvotes: 1