gpt4 book ai didi

python - GIS/Gdal/OSGeos 在 Windows 上的 django 中导入错误

转载 作者:太空狗 更新时间:2023-10-29 21:45:11 30 4
gpt4 key购买 nike

我几个小时以来一直在尝试这个问题,但我无法解决它。我正在使用 this在 Windows 上设置 GeoDjango 的链接。我收到以下错误消息,但不知道该怎么做。所有环境变量都在 Windows 中设置,我可以使用

导入

from osgeo import gdal

在python终端成功

import gdal

已弃用。在 Postgresql 数据库中,我可以根据需要提供所有扩展,如 address_standardizerfuzzystrmatchogr_fdwpgrouting plpgsql, pointcloud, pointcloud_postgis, postgis, postgis_sfcgal, postgis_tiger_geocoderpostgis_topology

Django 项目设置:

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'postgis_24_sample',
'USER': 'postgres',
'PASSWORD': 'Hello123',
'HOST': 'localhost',
'PORT': '',
}
}

GDAL_LIBRARY_PATH = os.getenv('GDAL_LIBRARY_PATH')
GEOS_LIBRARY_PATH = os.getenv('GEOS_LIBRARY_PATH')

INSTALLED_APPS = [
...
'django.contrib.postgis',
...
]

错误:

(easy_geodj) C:\Users\dell\Desktop\easy_geodj\easy_geodj\djlocate>python manage.py runserver
Unhandled exception in thread started by <function wrapper at 0x066D6330>
Traceback (most recent call last):
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\utils\autoreload.py", line 250, in raise_last_exception
six.reraise(*_exception)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper
fn(*args, **kwargs)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\apps\registry.py", line 108, in populate
app_config.import_models()
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\apps\config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\db\models\base.py", line 124, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\db\models\base.py", line 330, in add_to_class
value.contribute_to_class(cls, name)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\db\utils.py", line 211, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\db\utils.py", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "c:\python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py", line 5, in <module>
from .features import DatabaseFeatures
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\db\backends\postgis\features.py", line 1, in <module>
from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\db\backends\base\features.py", line 4, in <module>
from django.contrib.gis.db.models import aggregates
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\db\models\__init__.py", line 3, in <module>
from django.contrib.gis.db.models.aggregates import * # NOQA
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\db\models\aggregates.py", line 1, in <module>
from django.contrib.gis.db.models.fields import ExtentField
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\db\models\fields.py", line 3, in <module>
from django.contrib.gis import forms, gdal
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\forms\__init__.py", line 3, in <module>
from .fields import ( # NOQA
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\forms\fields.py", line 4, in <module>
from django.contrib.gis.geos import GEOSException, GEOSGeometry
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\geos\__init__.py", line 5, in <module>
from .collections import ( # NOQA
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\geos\collections.py", line 11, in <module>
from django.contrib.gis.geos.geometry import GEOSGeometry, LinearGeometryMixin
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\geos\geometry.py", line 11, in <module>
from django.contrib.gis import gdal
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 43, in <module>
from django.contrib.gis.gdal.driver import Driver # NOQA
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\gdal\driver.py", line 5, in <module>
from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\gdal\prototypes\ds.py", line 9, in <module>
from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
File "C:\Users\dell\Desktop\easy_geodj\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 48, in <module>
lgdal = CDLL(lib_path)
File "c:\python27\Lib\ctypes\__init__.py", line 366, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found

最佳答案

GIS 依赖项真的很难正确处理,尤其是在 Windows 上。如果你想坚持使用这个操作系统,我建议继续使用 OsGeo4W .

对我有用的步骤如下:

确保卸载您已安装的那些 GIS 包的每个分发版,并仔细遵循 OSGeo4W installation instructions . Apache和MapServer默认是勾选安装的,不需要的可以取消勾选。

然后你可以改变你的 settings.py 看起来像这样:

GEOS_LIBRARY_PATH = 'C:\\OSGeo4W64\\bin\\geos_c.dll'
GDAL_LIBRARY_PATH = 'C:\\OSGeo4W64\\bin\\gdal201.dll'

注意 1:路径将取决于您安装 OSGeo4W 的位置,但(我记得)这些是默认值。

注意 2:如果您下载并安装 32 位版本,您的路径应该如下所示:

GEOS_LIBRARY_PATH = 'C:\\OSGeo4W\\bin\\geos_c.dll'
GDAL_LIBRARY_PATH = 'C:\\OSGeo4W\\bin\\gdal201.dll'

我曾在一个使用类似堆栈的项目中工作过,感觉在 Linux(尤其是 Ubuntu)上进行设置要容易得多。如果它只是一个学习型个人项目,或者如果它最终会托管在 Linux 机器上,我建议使用 Linux 开发环境。

例如,在 Ubuntu 上,您可以这样做:

$ sudo apt-get install binutils libproj-dev gdal-bin

GeoDjango official docs也有一些好的信息。

关于python - GIS/Gdal/OSGeos 在 Windows 上的 django 中导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48033648/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com