3

I am trying to load a .shp file in GeoDjango Admin. As a newbie I followed their official documentation and based on that I am trying to load my own shapefile. No errors are being shown but I can't see my shapefile being loaded. All I can see is a world map but I can see attributes related to my shapefile.

Here is the code:

load.py

from django.contrib.gis.utils import LayerMapping
from .models import TestGeo

testgeo_mapping = {
    'state_n': 'STATE_N',
    'admin_stat': 'ADMIN_STAT',
    'geom': 'MULTIPOLYGON',

}

state_shp = os.path.abspath(
    os.path.join(os.path.dirname(__file__), 'data', 'STATE.shp'),
)

def run(verbose=True):
    lm = LayerMapping(TestGeo, state_shp, testgeo_mapping, transform=False)
    lm.save(strict=True, verbose=verbose)

models.py



class TestGeo(models.Model):
    state_n = models.CharField(max_length=50)
    admin_stat = models.IntegerField()
    geom = models.MultiPolygonField(srid=4326)

    def __str__(self):
        return self.state_n

Output: enter image description here

Output of python manage.py ogrinspect forest/data/STATE.shp TestGeo --srid=4326 --mapping --multi:



class TestGeo(models.Model):
    state_n = models.CharField(max_length=50)
    admin_stat = models.IntegerField()
    geom = models.MultiPolygonField(srid=4326)


# Auto-generated `LayerMapping` dictionary for TestGeo model
testgeo_mapping = {
    'state_n': 'STATE_N',
    'admin_stat': 'ADMIN_STAT',
    'geom': 'MULTIPOLYGON',
}

What am I doing wrong?

shapefile is completely fine I have plotted it using matplotlib and also inspected it in QGIS.

2
+50

I am not sure if this will answer your question but when I first started using geodjango the I had problems importing both the regular django models from django.db import models and the gis django models from django.contrib.gis.db import models

Give this a try:

from django.db import models
from django.contrib.gis.db import models as gismodels

class TestGeo(models.Model):
    state_n = models.CharField(max_length=50)
    admin_stat = models.IntegerField()
    geom = gismodels.MultiPolygonField(srid=4326)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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