1

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.

1 Answer 1

1

If anyone stumbles upon this. It turns out that if you 'View top 100 rows' in pgAdmin it will show the geometry column as empty, but if you do an SQL query 'select * from table' it will show up. So my code was working the entire time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.