GIS/Gdal/OSGeos Import error in django on Windows

末鹿安然 提交于 2021-02-07 12:51:48

问题


I am trying this from hours now and I couldn't get get it solved. I am using this link to setup GeoDjango on Windows. I am getting following error message and I have no clue what to do. All the environment variables are set in Windows and I am able to import with

from osgeo import gdal

successfully in python terminal as

import gdal

is deprecated. In Postgresql database I have all extensions available as required like address_standardizer, fuzzystrmatch, ogr_fdw, pgrouting, plpgsql, pointcloud, pointcloud_postgis, postgis, postgis_sfcgal, postgis_tiger_geocoder and postgis_topology.

Django Project Settings:

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

ERROR:

(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

回答1:


GIS dependencies are really hard to get right, especially on Windows. If you want to stick with this OS I'd recommend keep using OsGeo4W.

The steps that worked for me are the following:

Make sure to uninstall every distribution of those GIS packages you've installed and carefully follow the OSGeo4W installation instructions. Apache and MapServer are checked to be installed by default, you can uncheck them as they are not needed.

Than you can change your settings.py to look like this:

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

Note 1: the paths will depend on where you installed OSGeo4W but (as I remember) those are the defaults.

Note 2: if you download and install the 32-bit version your paths should look like this instead:

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

I've worked on a project using a similar stack and felt it was a lot easier to set things up on Linux (Ubuntu, particularly). If it's just a learning personal project or if it'll end up hosted on a Linux machine anyway, I'd suggest going for a Linux dev environment.

On Ubuntu, for example, you can just do:

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

Also the GeoDjango official docs have some good info to.




回答2:


I would ignore all attempts to manually handle the installation yourself, and use an anaconda environment. GIS dependencies are notoriously fickle to install, and anaconda has smoothed a large number of rough edges.




回答3:


First type python ins cmd and check which version is installed. it shows a result as

... [MSC v.1600 32 bit ..] ... well you can find that python is compiled with MSC compiler 32bit version 1600 using this link download gdal based on compiler

http://www.gisinternals.com/release.php

for example you must download this version based on above python output

MSVC 2010 win32 release-1600-gdal-2-2-3-mapserver-7-0-7

download both GDAL-2.2.3.win32-py3.4.msi and gdal-202-1600-core.msi and install them.

Add these two variable into windows Environment

- `GDAL_DATA` = `C:\Program Files (x86)\GDAL\gdal-data` - `path` = add this at the start `C:\Program Files (x86)\GDAL\`

and finally add these to setting.py

GEOS_LIBRARY_PATH = 'C:\\Program Files (x86)\\GDAL\\geos_c.dll' GDAL_LIBRARY_PATH = 'C:\\Program Files (x86)\\GDAL\\gdal202.dll'



来源:https://stackoverflow.com/questions/48033648/gis-gdal-osgeos-import-error-in-django-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!