Error importing spatial data in GeoDjango - KeyError for mpoly field

I followed the guide on https://docs.djangoproject.com/en/1.8/ref/contrib/gis/tutorial/#importing-spatial-data to configure GeoDjango on my machine. But there seems to be some kind of problem. When importing data using LayerMapping by running load.run() , I get the following error:

 Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/ubuntu/src/django/world/load.py", line 23, in run lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False, encoding='iso-8859-1') File "/home/ubuntu/Envs/vir-env/local/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.py", line 105, in __init__ self.check_layer() File "/home/ubuntu/Envs/vir-env/local/lib/python2.7/site-packages/django/contrib/gis/utils/layermapping.py", line 178, in check_layer ogr_field_types = self.layer.field_types File "/home/ubuntu/Envs/vir-env/local/lib/python2.7/site-packages/django/contrib/gis/gdal/layer.py", line 153, in field_types for i in range(self.num_fields)] KeyError: 12 

Then I found out that there is no MULTIPOLYGON field in the .shp file:

 >>> from django.contrib.gis.gdal import DataSource >>> ds = DataSource('world/data/TM_WORLD_BORDERS-0.3.shp') >>> layer = ds[0] >>> layer.fields [u'FIPS', u'ISO2', u'ISO3', u'UN', u'NAME', u'AREA', u'POP2005', u'REGION', u'SUBREGION', u'LON', u'LAT'] 

But it is present in the WorldBorder model as a type of MultiPolygonField . Thus, definitely in the world_mapping file, the import will fail to display 'mpoly': 'MULTIPOLYGON' . Has anyone else encountered this problem? Hope that the way I followed the tutorial step by step. But he does not say anything about such a problem. What effect will it have if I download data by deleting mpoly mapping?

Here is my load.py file:

  1 import os 2 from django.contrib.gis.utils import LayerMapping 3 from models import WorldBorder 4 5 world_mapping = { 6 'fips' : 'FIPS', 7 'iso2' : 'ISO2', 8 'iso3' : 'ISO3', 9 'un' : 'UN', 10 'name' : 'NAME', 11 'area' : 'AREA', 12 'pop2005' : 'POP2005', 13 'region' : 'REGION', 14 'subregion' : 'SUBREGION', 15 'lon' : 'LON', 16 'lat' : 'LAT', 17 'mpoly' : 'MULTIPOLYGON', 18 } 19 20 world_shp = os.path.abspath(os.path.join(os.path.dirname(__file__), 'data/TM_WORLD_BORDERS-0.3.shp')) 21 22 def run(verbose=True): 23 lm = LayerMapping(WorldBorder, world_shp, world_mapping, transform=False, encoding='iso-8859-1') 24 25 lm.save(strict=True, verbose=verbose) 

Just an update: After passing the source code through the stack trace, I realized that I could not access the field_types rule of the layer module. So, from the python shell, when I access this property, I get the same error:

 >>> from django.contrib.gis.gdal import DataSource >>> ds = DataSource(wshp) >>> layer = ds[0] >>> layer.fields [u'FIPS', u'ISO2', u'ISO3', u'UN', u'NAME', u'AREA', u'POP2005', u'REGION', u'SUBREGION', u'LON', u'LAT'] >>> layer.field_types Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/ubuntu/Envs/rj-venv/local/lib/python2.7/site-packages/django/contrib/gis/gdal/layer.py", line 153, in field_types for i in range(self.num_fields)] KeyError: 12 

Now this is strange, because now I also removed the mpoly field from WorldBorder .


Update 2:

After transcoding the source code, I found out that OGDFieldTypes in my gdal version may not have key 12, as in the source code: https://github.com/django/django/blob/master/django/contrib/gis/gdal/field. py . But he says the keys 12 and 13 will be available for GDAL 2 , and that is what I installed. In fact, there are now some conflicts between the libraries.

I installed the following libraries:

  • GEOS-3.4.2.tar.bz2
  • projected-datumgrid-1.5.tar.gz
  • projected-4.8.0.tar.gz
  • GDAL-2.0.0.tar.gz

And PostGIS version 2.1.5 is installed on the Amazon RDS instance.

+7
python django geodjango
source share
1 answer

The problem is that version 1.8.2 of Django (the latest version at the time of writing) does not support GDAL 2.0. 0 field types. The fix for this applies to the Django main code branch, but not yet been released.

At the time of writing, you have two options: build Django from the last source on Github (i.e. upgrade) or switch to GDAL so that the native code that calls this line cannot return 12 as the field type.

Since GDAL 2.0 can return 12 as a field type , a search in this dictionary does not work with KeyError , which you see using Django 1.8.2 code. The main dict contains the correct fields for GDAL 2.0 - fixed with this commit (only 9 days at the time of writing!).


For completeness - the diagnosis of this error followed the discussion in the SOPython chat room - link to the bookmark conversation here

NB from chat If you have multiple versions of GDAL, you may need to add GDAL_LIBRARY_PATH to your django settings file to indicate the correct version of libgdal.so (or, I think, the corresponding .dll if you're on Windows)

+7
source share

All Articles