GDALException OGR failure.

Dear Community,
I am quite new to django and i am trying to get in touch with it.

I am trying to create django app that can be used to store spatial point location, but when I try through the admin to add a space point, I get the following ouput.

Exception Type:  GDALException
Exception Value: OGR failure.

i will report below all the steps that i have take and all the configuration that i have done to build the app.

First, i have installed gdal through pip and the following link Archived: Python Extension Packages for Windows - Christoph Gohlke which is the reference to install gdal.

After this i have edited the setting.py as follow

GDAL_LIBRARY_PATH = "C:/Users/Utente/Desktop/geodjango/v_env/Lib/site-packages/osgeo/gdal304.dll"
GEOS_LIBRARY_PATH="C:/Users/Utente/Desktop/geodjango/v_env/Lib/site- packages/osgeo/geos_c.dll"

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

Then i have creted the model inside the models.py

from django.contrib.gis.db import models as spatialmodels
from django.db import models


class SpatialData(models.Model):
    name = models.CharField(max_length=50)
    location = spatialmodels.PointField(srid=4326)

    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name_plural="Incidences"

in the admin.py i have setted as follow

from django.contrib.gis import admin
from world.models import SpatialData

admin.site.register(SpatialData, admin.GISModelAdmin)

And after this i have applied the migrations and sqlmigrate commands, but when i am trying to add the spatial position, i am getting the error.

In order to see if the GDAL and OGR packages are installed, I tried importing gdal and osgeo on the python shell, and everything is imported correctly.

I don’t know how to overcome to the problem. Can someone help me?

1 Like

Did you install the PostGIS extensions into your database? (https://postgis.net/)

Are you sure you’ve matched the proper versions of the GDAL libraries with the version of Django you are running?

(It may help if you provide all the specifics regarding which versions of Python, etc that you have installed and which specific files or aggregates you used to perform the installation.)

Also, I see an excess space in:

Is that just a copy/paste error or do you have that space in your settings?

1 Like

Thanks for the reply @KenWhitesell.

For the space that you mention i think is related to copy/paste error.

I have checked what extensions are active on my postgresql database and I have the following extensions active:

plpgsql
postgis
postgis_raster

The version of the python is 3.10.4

This is my pip freeze setting

asgiref==3.6.0
autopep8==2.0.1
certifi==2022.12.7
cffi==1.15.1
charset-normalizer==3.0.1
cryptography==39.0.1
Deprecated==1.2.13
Django==4.1.7
environ==1.0
GDAL @ file:///C:/Users/Utente/Desktop/imparare_django/gdal_libraries/GDAL-3.4.3-cp310-cp310-win_amd64.whl
gitdb==4.0.10
GitPython==3.1.31
idna==3.4
ogr==0.43.0
Pillow==9.4.0
psycopg2==2.9.5
pycodestyle==2.10.0
pycparser==2.21
PyGithub==1.58.0
PyJWT==2.6.0
PyNaCl==1.5.0
python-gitlab==3.13.0
PyYAML==6.0
requests==2.28.2
requests-toolbelt==0.10.1
smmap==5.0.0
sqlparse==0.4.3
tomli==2.0.1
tzdata==2022.7
urllib3==1.26.14
wrapt==1.14.1

To retrive all the information about the postgresql database (version 15) i have used the following code

SELECT PostGIS_full_version();

and the output is the following

POSTGIS="3.3.2 3.3.2" [EXTENSION] PGSQL="150" GEOS="3.11.1-CAPI-1.17.1" PROJ="7.2.1" GDAL="GDAL 3.4.3, released 2022/04/22" LIBXML="2.9.9" LIBJSON="0.12" LIBPROTOBUF="1.2.1" WAGYU="0.5.0 (Internal)" RASTER

If you need any other information please ask me.
Thanks

As @KenWhitesell suggested i have checked the version of spatial database supported by django based on documentation GeoDjango Installation | Django documentation | Django

I will list below:

  1. Postgresql version (15) that i have installed is supported by django
  2. The PROJ version that i have installed (7.2.1) is supported by django
  3. The GDAL version that i have installed (3.4) is supported by django
  4. The latest version of Postgis (3.3) is not supported by django
  5. The latest version of GEOS (3.11) is not supported by django

So at least the Postgis and GEOS that i have installed is not supported by django.

Could you help me to understand how to change those settings in order to start creating spatial app in django?

I’m not a GIS person, I know very little about it, so I can only go by what docs I can find. (And what little GIS work I do is done on Linux.)

Hunting around a little bit, I cannot find a PostGIS 3.2 for PostgreSQL 15. The best I can find is one for PostgreSQL 14 at Index of /postgis/windows/pg14/archive/

Your first step may be to downgrade from PostgreSQL 15 to 14.

Likewise, I would suspect you could find the older versions of the libraries you may need as well.

1 Like

Thanks @KenWhitesell,

This morning i have build a docker container with the PostgreSQL 13.9 and i have installed PostGIS 3.2 but still i have the same problem

Exception Type:  GDALException
Exception Value: OGR failure.

Could you please suggest me a resource to install the GEOS version 3.10.

Also I would like to say that I am starting to use django and these problems slow down and disinvite those who would like to start using this framework. I hope that in the future django could support the latest version of postgreSQL & PostGIS and GEOS.

I honestly wish I could help you, but I just don’t do this sort of work on Windows. There are just too many problems to deal with. (I really don’t believe this issue is a “Python/PostGIS” issue as much as it is an issue of getting this to run on Windows.)

Note: Did you follow the steps as described at Windows?

Note from the docs that:

Note that older or more recent versions of these libraries may also work totally fine with GeoDjango. Your mileage may vary.

Strictly speaking, “not supported” does not necessarily mean “does not work with”, and in my experiences, that’s especially true with newer versions unless they document major API changes.

You might also want to look for an appropriate Conda package that has this bundled within it. I know a couple people who have had success in that direction.

1 Like

Thanks for the clarification @KenWhitesell.

i have followed the step reported here window but i still have the problem.

Also when I activate python environment and try to import gdal it reports me

Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import gdal
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'gdal'

but as reported before the gdal module is within the pip freeze list.

I have tried uninstalling and installing it again, following youtube videos but the problem persists, so I would ask anyone who is willing to give me a hand to overcome the problem.

Just for clarification - import gdal is importing the Python components to interface with the gdal binary libraries. Pip will show the presence of those Python components, but knows nothing about the binary libraries. This error will be thrown if Python is unable to locate or use those libraries.

I’m working through the installations now on a Windows 11 laptop. I’ll let you know how I make out.

1 Like

The first difference that I see between the instructions and what happens is that I don’t end up with a directory named “OSGeo4W64”. The directory created on my laptop is “OSGeo4W”. I don’t know yet whether that means I’ve installed the wrong package or if the name has been changed.

Can you check to see what was installed in your system?

1 Like

yes I also have the folder name “OSGeo4W”.

i have tried to change the envirnoment variables as suggested Geodjango Documentation

set OSGEO4W_ROOT=C:\OSGeo4W64
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
set PATH=%PATH%;%OSGEO4W_ROOT%\bin
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"

to

set OSGEO4W_ROOT=C:\OSGeo4W
set GDAL_DATA=%OSGEO4W_ROOT%\share\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
set PATH=%PATH%;%OSGEO4W_ROOT%\bin
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"

but still i have the same problem

GDAL_ERROR 1: b'PROJ: proj_create_from_database: Cannot find proj.db'
Internal Server Error: /admin/world/worldborder/add/
Traceback (most recent call last):
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\core\handlers\exception.py", line 56, in inner
    response = get_response(request)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\admin\options.py", line 686, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\utils\decorators.py", line 134, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\views\decorators\cache.py", line 62, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\admin\sites.py", line 242, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\admin\options.py", line 1891, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\utils\decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\utils\decorators.py", line 134, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\admin\options.py", line 1751, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\admin\options.py", line 1797, in _changeform_view
    form_validated = form.is_valid()
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\forms\forms.py", line 205, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\forms\forms.py", line 200, in errors
    self.full_clean()
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\forms\forms.py", line 437, in full_clean
    self._clean_fields()
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\forms\forms.py", line 449, in _clean_fields
    value = field.clean(value)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\forms\fields.py", line 90, in clean
    geom.transform(self.srid)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\geos\geometry.py", line 496, in transform
    g = gdal.OGRGeometry(self._ogr_ptr(), srid)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\gdal\geometries.py", line 123, in __init__
    self.srs = srs
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\gdal\geometries.py", line 289, in _set_srs
    sr = SpatialReference(srs)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\gdal\srs.py", line 114, in __init__
    self.import_epsg(srs_input)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\gdal\srs.py", line 300, in import_epsg
    capi.from_epsg(self.ptr, epsg)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\gdal\prototypes\errcheck.py", line 120, in check_errcode
    check_err(result, cpl=cpl)
  File "C:\Users\Utente\Desktop\imparare_django\geodjango\v_env\lib\site-packages\django\contrib\gis\gdal\error.py", line 59, in check_err
    raise e(msg)
django.contrib.gis.gdal.error.GDALException: OGR failure.

Another difference I see is that this path also doesn’t appear to be correct anymore:

I don’t know what this directory should be - there are two options:
\OSGeo4W\apps\gdal and \OSGeo4W\apps\gdal\share\gdal

1 Like

dear @KenWhitesell I did not change the last environmental variable as you suggested and I turned the computer on and off again and now geodjango is working properly and I can save, edit and delete each point.

Thank you very much

1 Like

Hi @agronomofiorentini. I encountered the same problem as yours. I’ve been struggling for 3 days in a row. Could you please show me how you come to the solution? Thank you so much

Hey @htb2767, I am stil new to this framework.

Please follow all the step that i have done here and please report here the error output.

Then i will try to help you.

Thank you so much for your response! I just checked my gdal version and it is 3.7.1 . I think Django does not support gdal version 3.7.x. Is there a way that I can downgrade my gdal version and see if it is compatible? Thank you once again

I fixed it. So so happy. Thank you for your response though!!!

1 Like