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.