1

I have made it to which the model in django is built, but once adding data and clicking save my app crash

i get GDAL exception - OGR Failure, and it highlights in the crash page " {{ field.field }}" as the reason for the error. here's screen shot Admin view for adding data to the model

i get this right away after clicking save

Anyone been through this? any help?

Thanks

Update : I'm getting this error :

[12/Apr/2019 14:38:46] "GET /static/admin/img/gis/move_vertex_off.svg HTTP/1.1"
200 1129
[12/Apr/2019 14:39:14] "GET /static/admin/img/gis/move_vertex_on.svg HTTP/1.1" 2
00 1129
GDAL_ERROR 4: b'Unable to open EPSG support file gcs.csv.\nTry setting the GDAL_
DATA environment variable to point to the\ndirectory containing EPSG csv files.'

Internal Server Error: /admin/trial2/shop/add/
Traceback (most recent call last):
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\core\
handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\core\
handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\core\
handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 606, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\utils
\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\views
\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\sites.py", line 223, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 1634, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\utils
\decorators.py", line 45, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\utils
\decorators.py", line 142, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 1522, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\admin\options.py", line 1554, in _changeform_view
    form_validated = form.is_valid()
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 185, in is_valid
    return self.is_bound and not self.errors
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 180, in errors
    self.full_clean()
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 381, in full_clean
    self._clean_fields()
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\forms
\forms.py", line 399, in _clean_fields
    value = field.clean(value)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\forms\fields.py", line 79, in clean
    geom.transform(self.srid)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\geos\geometry.py", line 471, in transform
    g = gdal.OGRGeometry(self._ogr_ptr(), srid)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\geometries.py", line 115, in __init__
    self.srs = srs
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\geometries.py", line 284, in _set_srs
    sr = SpatialReference(srs)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\srs.py", line 92, in __init__
    self.import_epsg(srs_input)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\srs.py", line 277, in import_epsg
    capi.from_epsg(self.ptr, epsg)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\prototypes\errcheck.py", line 118, in check_errcode
    check_err(result, cpl=cpl)
  File "C:\Users\lgcge\Documents\copypostgis\venv\lib\site-packages\django\contr
ib\gis\gdal\error.py", line 59, in check_err
    raise e(msg)
django.contrib.gis.gdal.error.GDALException: OGR failure.
[12/Apr/2019 14:39:45] "POST /admin/trial2/shop/add/ HTTP/1.1" 500 160518

admin.py :

from django import forms
from django.contrib.gis import admin

from django.contrib.gis.db import models



from django.contrib.gis.admin import OSMGeoAdmin
from .models import Shop

@admin.register(Shop)
class ShopAdmin(OSMGeoAdmin):
    list_display = ('name', 'location')

model.py

from __future__ import unicode_literals

from django.contrib.gis.db import models
from django.contrib.gis.geos import Point


class Shop(models.Model):
    name = models.CharField(max_length=100)
    location = models.PointField()
    address = models.CharField(max_length=100)
    city = models.CharField(max_length=50)
kiwis
  • 65
  • 1
  • 9
  • Can you share more information about the error in your question? Please add the following: - The model you are trying to fill - The admin form that you use - An error trace (not a screenshot of one) Try to follow that guideline: https://stackoverflow.com/help/how-to-ask – John Moutafis Apr 11 '19 at 08:48
  • Thanks John for your reply, I have updated the issue – kiwis Apr 12 '19 at 13:04
  • Are you on windows? If yes, does this answer help you: https://stackoverflow.com/questions/49139044/geodjango-on-windows-could-not-find-the-gdal-library-oserror-winerror-12? – John Moutafis Apr 13 '19 at 23:07
  • Hi John, Yes, I'm on windows, I have already seen this answer, i did these steps, (OSGEO 64) etc.. and didn't work – kiwis Apr 14 '19 at 12:01
  • alright so i found a solution to this thanks – kiwis Apr 14 '19 at 18:17

3 Answers3

3

Apologies for copy/pasting and only slightly modifying @Kiwi's answer. I tried adding a comment to the original answer but I could not add formatted code.

It seems now that you cannot install the 64 bit version of OSGEO4W any more. I installed the only version available here and modified @Kiwi's answer to reflect the lack of 64 bit. I'm on Windows 10. All works fine with the below.

import platform
import environ

WINDOWS = platform.system() == "Windows"

if WINDOWS:
    # the below needs to change for linux
    GDAL_LIBRARY_PATH = r'C:\OSGeo4W\bin\gdal303.dll'
    GEOS_LIBRARY_PATH = r'C:\OSGeo4W\bin\geos_c.dll'
    OSGEO4W = r"C:\OSGeo4W"
    os.environ['OSGEO4W_ROOT'] = OSGEO4W
    os.environ['GDAL_DATA'] = "C:\Program Files\GDAL\gdal-data"  # OSGEO4W + r"\share\gdal"
    os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
    os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']
andyw
  • 3,505
  • 2
  • 30
  • 44
2
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'] = "C:\Program Files\GDAL\gdal-data" #OSGEO4W + r"\share\gdal"
    os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
    os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']

the issue was that this code enforces the paths defined in it over geodjango, so even if you set system variables, still this code would dominate over the system variables.

If anyone has issue with OGR don't hesitate to comment

kiwis
  • 65
  • 1
  • 9
0

I had the same problem and this is how I was able to solve it. first check your GDAL version and GEOS by running following scripts

gdal-config --version

and

geos-config --version

If the versions of GDAL and GEOS do not match, you will need to either install a compatible version of GEOS or update GDAL to a version that is compatible with the version of GEOS you have installed.

For example if you have GEOS version 3.8.0 or above you will need to upgrade GDAL >= 3.1.2