【问题标题】:Issue with Geodjango OGR failure - GDAL ExceptionGeodjango OGR 失败的问题 - GDAL 异常
【发布时间】:2019-08-28 18:42:10
【问题描述】:

我已经在 django 中构建了模型,但是一旦添加数据并单击保存我的应用程序崩溃

我得到 GDAL 异常 - OGR 失败,它在崩溃页面“{{ field.field }}”中突出显示作为错误原因。 这是屏幕截图Admin view for adding data to the model

i get this right away after clicking save

有人经历过吗?有什么帮助吗?

谢谢

更新: 我收到此错误:

[12/Apr/2019 14:38:46] "GET /static/admin/img/gis/move_vertex_off.svg HTTP/1.1"
200 1129
[12/Apr/2019 14:39:14] "GET /static/admin/img/gis/move_vertex_on.svg HTTP/1.1" 2
00 1129
GDAL_ERROR 4: b'Unable to open EPSG support file gcs.csv.\nTry setting the GDAL_
DATA environment variable to point to the\ndirectory containing EPSG csv files.'

Internal Server Error: /admin/trial2/shop/add/
Traceback (most recent call last):
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\core\
handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\core\
handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\core\
handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 606, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\utils
\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\views
\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\sites.py", line 223, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 1634, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\utils
\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\utils
\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 1522, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 1554, in _changeform_view
    form_validated = form.is_valid()
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 185, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 180, in errors
    self.full_clean()
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 381, in full_clean
    self._clean_fields()
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 399, in _clean_fields
    value = field.clean(value)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\forms\fields.py", line 79, in clean
    geom.transform(self.srid)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\geos\geometry.py", line 471, in transform
    g = gdal.OGRGeometry(self._ogr_ptr(), srid)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\geometries.py", line 115, in __init__
    self.srs = srs
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\geometries.py", line 284, in _set_srs
    sr = SpatialReference(srs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\srs.py", line 92, in __init__
    self.import_epsg(srs_input)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\srs.py", line 277, in import_epsg
    capi.from_epsg(self.ptr, epsg)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\prototypes\errcheck.py", line 118, in check_errcode
    check_err(result, cpl=cpl)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\error.py", line 59, in check_err
    raise e(msg)
django.contrib.gis.gdal.error.GDALException: OGR failure.
[12/Apr/2019 14:39:45] "POST /admin/trial2/shop/add/ HTTP/1.1" 500 160518

admin.py:

from django import forms
from django.contrib.gis import admin

from django.contrib.gis.db import models



from django.contrib.gis.admin import OSMGeoAdmin
from .models import Shop

@admin.register(Shop)
class ShopAdmin(OSMGeoAdmin):
    list_display = ('name', 'location')

模型.py

from __future__ import unicode_literals

from django.contrib.gis.db import models
from django.contrib.gis.geos import Point


class Shop(models.Model):
    name = models.CharField(max_length=100)
    location = models.PointField()
    address = models.CharField(max_length=100)
    city = models.CharField(max_length=50)

【问题讨论】:

  • 您能否分享有关您问题中的错误的更多信息?请添加以下内容: - 您尝试填写的模型 - 您使用的管理表单 - 错误跟踪(不是截图)尝试遵循该指南:stackoverflow.com/help/how-to-ask
  • 感谢约翰的回复,我已经更新了问题
  • 你在windows上吗?如果是,这个答案对您有帮助吗:stackoverflow.com/questions/49139044/…?
  • 嗨,约翰,是的,我在 Windows 上,我已经看到了这个答案,我做了这些步骤,(OSGEO 64)等等......但没有用
  • 好的,所以我找到了解决方案,谢谢

标签: leaflet django-admin postgis geodjango django-leaflet


【解决方案1】:

抱歉复制/粘贴,只是稍微修改了@Kiwi 的答案。我尝试在原始答案中添加注释,但无法添加格式化代码。

现在您似乎无法再安装 64 位版本的 OSGEO4W。我安装了唯一可用的版本here 并修改了@Kiwi 的答案以反映缺少 64 位。我在 Windows 10 上。下面的一切正常。

import platform
import environ

WINDOWS = platform.system() == "Windows"

if WINDOWS:
    # the below needs to change for linux
    GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal303.dll'
    GEOS_LIBRARY_PATH = r'C:\OSGeo4W\bin\geos_c.dll'
    OSGEO4W = r"C:\OSGeo4W"
    os.environ['OSGEO4W_ROOT'] = OSGEO4W
    os.environ['GDAL_DATA'] = "C:\Program Files\GDAL\gdal-data"  # OSGEO4W + r"\share\gdal"
    os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
    os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']

【讨论】:

    【解决方案2】:
    if os.name == 'nt':
        import platform
        OSGEO4W = r"C:\OSGeo4W"
        if '64' in platform.architecture()[0]:
            OSGEO4W += "64"
        assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
        os.environ['OSGEO4W_ROOT'] = OSGEO4W
        os.environ['GDAL_DATA'] = "C:\Program Files\GDAL\gdal-data" #OSGEO4W + r"\share\gdal"
        os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
        os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
    

    问题是这段代码通过 geodjango 强制执行其中定义的路径,所以即使你设置了系统变量,这段代码仍然会支配系统变量。

    如果有人对 OGR 有任何疑问,请随时发表评论

    【讨论】:

    • 我添加了它,但仍然有同样的问题,当我尝试使用 geom 查看数据库中的实例时出现错误
    • 你使用的是windows 32位版本吗?
    • 我遇到了同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多