Search code examples
python-3.xgdalgeodjango

Gdal GeoDjango Windows


I use Windows 10 (x64), Python 3.6.4, Django 2.0.5.

I have installed OSGEO4W64 on my machine (C:\OSGeo4W64).

I set environment variables:

GDAL_LIBRARY_PATH = C:\OSGeo4W64\share\proj

GDAL_DATA = C:\OSGeo4W64\share\gdal

OSGEO4W_ROOT = C:\OSGeo4W64

PROJ_LIB = C:\OSGeo4W64\share\proj

Also, a have added to my djanjo project settings:

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'] = OSGEO4W + r"\share\gdal"
    os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
    os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
    GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal202'

I understand that it looks ugly, but it helped me to avoid other problems( with with unidentified libraries, for example). And now I just can not fix this mistake:

    Traceback (most recent call last):
  File "C:\oktmo_Django\oktmo_project/manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "C:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "C:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\core\management\__init__.py", line 347, in execute
    django.setup()
  File "C:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\apps\registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "C:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Python\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\contrib\gis\gdal\__init__.py", line 28, in <module>
    from django.contrib.gis.gdal.datasource import DataSource
  File "C:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\contrib\gis\gdal\datasource.py", line 39, in <module>
    from django.contrib.gis.gdal.driver import Driver
  File "C:\oktmo_Django\oktmo_project\venv\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:\oktmo_Django\oktmo_project\venv\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:\oktmo_Django\oktmo_project\venv\lib\site-packages\django\contrib\gis\gdal\libgdal.py", line 43, in <module>
    lgdal = CDLL(lib_path)
  File "C:\Python\lib\ctypes\__init__.py", line 416, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 127] The specified procedure could not be found

Solution

  • The easiest way I've found is:

    1. Install Gdal from whl, for examle: https://www.lfd.uci.edu/~gohlke/pythonlibs/, if this link unavailable, the whl you need is not so difficult to find.
    2. Add to settings.py this code:
         OSGEO_VENV = Path(__file__).parents[1] / 'venv/Lib/site-packages/osgeo/'
         GEOS_LIBRARY_PATH = str(OSGEO_VENV / 'geos_c.dll')
         GDAL_LIBRARY_PATH = str(OSGEO_VENV / 'gdal204.dll')
         os.environ["PATH"] += os.pathsep + str(OSGEO_VENV)```
      
      

    Make sure to edit the gdal204.dll to your specific gdal version!