﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24724	GeoDjango with Django 1.8 - 'GDALAllRegister' Attribute Error	dimitri-justeau	nobody	"I used to work with geodjango using Django 1.7 on Windows 7, and everything was working well. After having upgraded to django 1.8, loading the gis app causes an error.

Here is the full Traceback:


{{{
Traceback (most recent call last):
  File ""C:\Users\justeau\PycharmProjects\Digitizing-Monitor\digitizing_monitor\manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""C:\Python34\lib\site-packages\django\core\management\__init__.py"", line 338, in execute_from_command_line
    utility.execute()
  File ""C:\Python34\lib\site-packages\django\core\management\__init__.py"", line 312, in execute
    django.setup()
  File ""C:\Python34\lib\site-packages\django\__init__.py"", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File ""C:\Python34\lib\site-packages\django\apps\registry.py"", line 108, in populate
    app_config.import_models(all_models)
  File ""C:\Python34\lib\site-packages\django\apps\config.py"", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File ""C:\Python34\lib\importlib\__init__.py"", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File ""<frozen importlib._bootstrap>"", line 2254, in _gcd_import
  File ""<frozen importlib._bootstrap>"", line 2237, in _find_and_load
  File ""<frozen importlib._bootstrap>"", line 2226, in _find_and_load_unlocked
  File ""<frozen importlib._bootstrap>"", line 1200, in _load_unlocked
  File ""<frozen importlib._bootstrap>"", line 1129, in _exec
  File ""<frozen importlib._bootstrap>"", line 1471, in exec_module
  File ""<frozen importlib._bootstrap>"", line 321, in _call_with_frames_removed
  File ""C:\Python34\lib\site-packages\django\contrib\auth\models.py"", line 41, in <module>
    class Permission(models.Model):
  File ""C:\Python34\lib\site-packages\django\db\models\base.py"", line 139, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File ""C:\Python34\lib\site-packages\django\db\models\base.py"", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File ""C:\Python34\lib\site-packages\django\db\models\options.py"", line 250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File ""C:\Python34\lib\site-packages\django\db\__init__.py"", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File ""C:\Python34\lib\site-packages\django\db\utils.py"", line 240, in __getitem__
    backend = load_backend(db['ENGINE'])
  File ""C:\Python34\lib\site-packages\django\db\utils.py"", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File ""C:\Python34\lib\importlib\__init__.py"", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File ""<frozen importlib._bootstrap>"", line 2254, in _gcd_import
  File ""<frozen importlib._bootstrap>"", line 2237, in _find_and_load
  File ""<frozen importlib._bootstrap>"", line 2226, in _find_and_load_unlocked
  File ""<frozen importlib._bootstrap>"", line 1200, in _load_unlocked
  File ""<frozen importlib._bootstrap>"", line 1129, in _exec
  File ""<frozen importlib._bootstrap>"", line 1471, in exec_module
  File ""<frozen importlib._bootstrap>"", line 321, in _call_with_frames_removed
  File ""C:\Python34\lib\site-packages\django\contrib\gis\db\backends\postgis\base.py"", line 8, in <module>
    from .features import DatabaseFeatures
  File ""C:\Python34\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:\Python34\lib\site-packages\django\contrib\gis\db\backends\base\features.py"", line 3, in <module>
    from django.contrib.gis.db.models import aggregates
  File ""C:\Python34\lib\site-packages\django\contrib\gis\db\models\__init__.py"", line 7, in <module>
    from django.contrib.gis.geos import HAS_GEOS
  File ""C:\Python34\lib\site-packages\django\contrib\gis\geos\__init__.py"", line 16, in <module>
    from .geometry import GEOSGeometry, wkt_regex, hex_regex
  File ""C:\Python34\lib\site-packages\django\contrib\gis\geos\geometry.py"", line 10, in <module>
    from django.contrib.gis.gdal.error import SRSException
  File ""C:\Python34\lib\site-packages\django\contrib\gis\gdal\__init__.py"", line 47, in <module>
    from django.contrib.gis.gdal.driver import Driver  # NOQA
  File ""C:\Python34\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:\Python34\lib\site-packages\django\contrib\gis\gdal\prototypes\raster.py"", line 25, in <module>
    register_all = void_output(lgdal.GDALAllRegister, [])
  File ""C:\Python34\lib\ctypes\__init__.py"", line 364, in __getattr__
    func = self.__getitem__(name)
  File ""C:\Python34\lib\ctypes\__init__.py"", line 369, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'GDALAllRegister' not found
}}}


After some searches, it appears that other people had this problem when using GeoDjango + Django 1.8 + Windows (example: https://gis.stackexchange.com/questions/143110/geodjango-setup-python-gdalallregister-attribute-error/144407#144407). But I could not find any fix for this.

I investigated a bit, and found out that at django.contrib.gis.gdal.libgal, we can see this:


{{{
    ..|
    47|  # This loads the GDAL/OGR C library
    48|  lgdal = CDLL(lib_path)
    49| 
    50|  # On Windows, the GDAL binaries have some OSR routines exported with
    51|  # STDCALL, while others are not.  Thus, the library will also need to
    52|  # be loaded up as WinDLL for said OSR functions that require the
    53|  # different calling convention.
    54|  if os.name == 'nt':
    55|      from ctypes import WinDLL
    56|    lwingdal = WinDLL(lib_path)
    ..|
}}}


So when the os is Windows, the GDAL library is also loaded as a DLL, so we have two GDAL libraries, lgdal and lwingdal.

But if we look at the django.contrib.gis.gdal.prototypes.raster (where the ""'GDALAllRegister' not found"" error is raised from), we can see that the one used is lgdal:


{{{
    ..|
    29|  # Raster Driver Routines
    30|  register_all = void_output(lgdal.GDALAllRegister, [])
    31|  get_driver = voidptr_output(lgdal.GDALGetDriver, [c_int])
    32|  get_driver_by_name = voidptr_output(lgdal.GDALGetDriverByName, [c_char_p], errcheck=False)
    33|  get_driver_count = int_output(lgdal.GDALGetDriverCount, [])
    34|  get_driver_description = const_string_output(lgdal.GDALGetDescription, [c_void_p])
    ..|
}}}


It seems like on Windows, lwingdal should be used, so I added this after the imports:


{{{
    ..|  
    15|  # Fix for windows users
    16|  if os.name == 'nt':
    17|      from django.contrib.gis.gdal.libgdal import lwingdal 
    18|      lgdal = lwingdal
    ..| 
}}}
 

And it solved the problem for me! Maybe there is something more, it seems a bit too easy, but I still had no issue with it, and I now can use the GIS with Django 1.8 on Windows, which was impossible before this.

Hope I helped...

Regards,

Dimitri.
"	Bug	closed	GIS	1.8	Release blocker	fixed	geodjango gdal windows rasters		Accepted	1	0	0	0	0	0
