在 GeoDjango 中导入空间数据时出错 - mpoly 字段的 KeyError

Error importing spatial data in GeoDjango - KeyError for mpoly field

我正在按照 https://docs.djangoproject.com/en/1.8/ref/contrib/gis/tutorial/#importing-spatial-data 上的教程在我的机器上设置 GeoDjango。但似乎那里有一些问题。 运行 load.run() 使用 LayerMapping 导入数据时,出现以下错误:

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

然后我发现,.shp文件中没有'MULTIPOLYGON'字段:

>>> 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']

但它存在于 WorldBorder 模型中,类型为 MultiPolygonField。 因此,肯定在 world_mapping 文件中,导入 'mpoly': 'MULTIPOLYGON' 映射将失败。还有其他人遇到过这个问题吗?我希望如此,因为我已逐步按照教程进行操作。但它没有说任何关于这样的问题。如果我通过删除 mpoly 映射加载数据,会有什么影响?

这是我的 load.py 文件:

  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)

只是更新: 通过堆栈跟踪查看源代码后,我发现我无法访问 layer 模块的 field_types 属性。因此,从 python shell,当我访问 属性 时,我得到了同样的错误:

>>> 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

现在,这很奇怪,因为现在我还从 WorldBorder 模型中删除了 mpoly 字段。


更新 2:

在深入研究源代码后,我发现,在我的 gdal 版本中,OGDFieldTypes 可能没有密钥 12,如此处的源代码所示:https://github.com/django/django/blob/master/django/contrib/gis/gdal/field.py。 但它说密钥 12 和 13 将可用于 GDAL 2,这就是我安装的。现在好像真的是图书馆之间有些冲突。

我已经安装了以下库:

并且在 Amazon RDS 实例中安装了 PostGIS 版本 2.1.5。

这里的问题是version 1.8.2 Django(撰写本文时的最新版本)不支持GDAL 2.0.0 字段类型。对此的修复在 Django 主代码分支中 - 但尚未在发布版本中。

在撰写本文时,您有两个选择 - 从 Github 上的最新源代码构建 Django(即升级),或者降级 GDAL,以便本机代码 called by this line 无法 return值 12 作为字段类型。

因为 GDAL 2.0 can return 12 as a field type, the lookup into this dictionary fails with the KeyError you see using Django 1.8.2 code. On master, the dict contains the right fields for GDAL 2.0 - fixed by this commit(撰写本文时仅 9 天!)。


为了完整起见 - 此故障的诊断遵循 SOPython 聊天室中的讨论 - link 到书签对话 is here

N.B。来自 chat 如果您安装了多个 GDAL 版本,您可能必须在 django 设置文件中添加 GDAL_LIBRARY_PATH 以指向 libgdal.so 的正确版本(或者我如果你在 Windows)

,请猜对应的 .dll