I'm using the LayerMapping feature of Django (https://docs.djangoproject.com/en/1.11/ref/contrib/gis/layermapping/) but I can't get the polygon to import into the DB. I'm using a PostGIS DB. I'm basing my work from the above link and from http://www.paolocorti.net/2009/04/01/a-day-with-geodjango/
My model is:
from django.db import models
from django.contrib.gis.db import models
class HazardMessageDistricts(models.Model):
# Outlines of districts that 311 info is available for
districtName = models.CharField("District Name", max_length=250, null = True)
# Spatial fields
# Default CRS -> WGS84
districts = models.PolygonField(null = True)
objects = models.GeoManager() # Required to conduct geographic queries
My script to import into the model is:
from django.contrib.gis.utils import LayerMapping
from django.db import models
from django.contrib.gis.db import models
from hazard_message_districts import HazardMessageDistricts
path = path_to_shapefile
mapping = {'districtName':'NameInShapeFile','districts':'Polygon'}
lm = LayerMapping(HazardMessageDistricts, path, mapping, transform=True, encoding='iso-8859-1')
lm.save(strict=True, verbose=True)
It's somewhat working as the NameInShapeFile is correctly passed into the DB but the geom field is left empty.