I'm trying to display a custom map feature on a GeoDjango map. The geometry is a multipolygon stored in a PostGIS database backend.
And I have trouble to find the correct piece of documentation that explains how to load and add my geometry from the database. This is what my PostGIS contains:
SELECT * FROM aptroomat_worldborder;
This is my model.py
equivalent in my GeoDjango project:
from django.contrib.gis.db import models
class WorldBorder(models.Model):
name = models.CharField(max_length=50)
area = models.IntegerField()
pop2005 = models.IntegerField('Population 2005')
fips = models.CharField('FIPS Code', max_length=2)
iso2 = models.CharField('2 Digit ISO', max_length=2)
iso3 = models.CharField('3 Digit ISO', max_length=3)
un = models.IntegerField('United Nations Code')
region = models.IntegerField('Region Code')
subregion = models.IntegerField('Sub-Region Code')
lon = models.FloatField()
lat = models.FloatField()
mpoly = models.MultiPolygonField()
objects = models.GeoManager()
def __str__(self):
return self.name
This is how I define my form in forms.py
:
from django.contrib.gis import forms
class WorldBorderForm(forms.Form):
world = forms.MultiPolygonField(widget =
forms.OSMWidget(attrs = {'map_width': 1024, 'map_height': 600}))
And my views.py
containing the callback:
from django.shortcuts import render
from myproject.forms import WorldBorderForm
def index(request):
form = WorldBorderForm()
context = { 'form': form }
return render(request, 'myproject/index.html', context)
This is how the result looks including my template, using an OSM base widget:
This is pretty much what I get from reading the GeoDjango documentation and following the GeoDjango tutorial on World Borders.
But the missing piece in the docs is: How to display my geometries from the PostGIS database in my GeoDjango map widget? I can't seem to figure out how to connect both.
Any ideas?
Source code is on github.com/donSchoe/sabracta.
Versions used in this project are:
- python 3.4.3
- postgresql 9.4.1
- postgis 2.1.5
- django 1.7.4
OpenLayersWidget
instead of theOSMWidget
. From the docs it seems that the one you are using is just for adding a map with a base layer. – yellowcap Mar 10 '15 at 18:57