我发现了一件奇怪的事情:
在python manage.py shell
之后,当我尝试使用GDAL接口使用django.contrib.gis.gdal.DataSource
创建数据源时,它将首先抛出错误,然后第二次尝试将成功。
from django.contrib.gis.gdal import DataSource
shp = 'someshapefile.shp'
ds = DataSource(shp)
这会使GDALException: : Unknown error code: "-474873798"
代码字符串不时变化。
但是再次尝试时:ds = DataSource(shp)
实例化数据源对象。
同样的问题也发生在ogrinspect
命令和通过LayerMapping
对象加载数据。
以下是完整的错误提示:
from django.contrib.gis.gdal import DataSource
shp = 'province.shp'
DataSource(shp)
GDALException Traceback (most recent call last)
<ipython-input-3-c4eb8c662773> in <module>()
----> 1 DataSource(shp)
//anaconda/lib/python3.5/site-packages/django/contrib/gis/gdal/datasource.py in __init__(self, ds_input, ds_driver, write, encoding)
62 self.encoding = encoding
63
---> 64 Driver.ensure_registered()
65
66 if isinstance(ds_input, six.string_types):
//anaconda/lib/python3.5/site-packages/django/contrib/gis/gdal/driver.py in ensure_registered(cls)
81 if not cls.driver_count():
82 vcapi.register_all()
---> 83 rcapi.register_all()
84
85 @classmethod
//anaconda/lib/python3.5/site-packages/django/contrib/gis/gdal/prototypes/errcheck.py in check_errcode(result, func, cargs, cpl)
117 Check the error code returned (c_int).
118 """
--> 119 check_err(result, cpl=cpl)
120
121
//anaconda/lib/python3.5/site-packages/django/contrib/gis/gdal/error.py in check_err(code, cpl)
72 raise e(msg)
73 else:
---> 74 raise GDALException('Unknown error code: "%s"' % code)
GDALException: Unknown error code: "-474873798"
DataSource(shp)
<django.contrib.gis.gdal.datasource.DataSource at 0x10cd6f4a8>
答案 0 :(得分:0)
我可以复制此行为,尝试导入gpx。调用datasource = DataSource(gpx)两次实例化DataSource。
我希望临时解决方案是在我的应用中执行此操作:
try:
datasource = DataSource(gpx)
except:
datasource = DataSource(gpx)
我为Django filed a ticket。
由此我的问题现已解决。请参阅ticket comment。