GeoDjango GDALException - OGR failure

假如想象 提交于 2021-01-28 06:08:40

问题


After installing GeoDjango, I wanted to create a 'Location' object in the admin panel which uses an address and a point on a map. After submitting the form, I get an error like so...

GDALException at /admin/maps/location/add/
OGR failure.

I have tried looking at similar questions, like here, but none of the solutions have worked. Additionally, searching for 'unable to load PROJ.4 library' (first traceback line) didn't come with any success.

Any help would be appreciated! - Let me know if I should update this question with my settings.py or other relevant files.

Full traceback:

GDAL_ERROR 6: Unable to load PROJ.4 library (libproj.dylib), creation of OGRCoordinateTransformation failed.
Internal Server Error: /admin/maps/location/add/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py", line 551, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py", line 224, in inner
    return view(request, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py", line 1508, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/Library/Python/2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py", line 1408, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py", line 1440, in _changeform_view
    if form.is_valid():
  File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 183, in is_valid
    return self.is_bound and not self.errors
  File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 175, in errors
    self.full_clean()
  File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 384, in full_clean
    self._clean_fields()
  File "/Library/Python/2.7/site-packages/django/forms/forms.py", line 402, in _clean_fields
    value = field.clean(value)
  File "/Library/Python/2.7/site-packages/django/contrib/gis/forms/fields.py", line 75, in clean
    geom.transform(self.srid)
  File "/Library/Python/2.7/site-packages/django/contrib/gis/geos/geometry.py", line 527, in transform
    g.transform(ct)
  File "/Library/Python/2.7/site-packages/django/contrib/gis/gdal/geometries.py", line 408, in transform
    capi.geom_transform_to(self.ptr, sr.ptr)
  File "/Library/Python/2.7/site-packages/django/contrib/gis/gdal/prototypes/errcheck.py", line 119, in check_errcode
    check_err(result, cpl=cpl)
  File "/Library/Python/2.7/site-packages/django/contrib/gis/gdal/error.py", line 73, in check_err
    raise e(msg)
GDALException: OGR failure.

Edit: Added my models.py and admin.py for Location

models.py:

from __future__ import unicode_literals
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings
from . import utils

COUNTRIES=((0,'UK'),(1,'USA'),(2,'CA'),(3,'AUS'),(4,'JP'),(5,'FR'))
class Location(models.Model):
    name=models.CharField(max_length=120,verbose_name='House name/number',blank=True)
    first_addr=models.CharField(max_length=45,verbose_name='Address line 1',blank=False)
    second_addr=models.CharField(max_length=45,verbose_name='Address line 2',blank=True)
    town=models.CharField(max_length=45,verbose_name='Town/City',blank=False)
    state=models.CharField(max_length=45,verbose_name='State/Province',blank=False)
    zip_code=models.CharField(max_length=12,verbose_name='Postal/Zip Code',blank=False)
    country=models.IntegerField(verbose_name='Country',blank=False,null=False,choices=COUNTRIES)
    point=models.PointField(default='POINT (0 0)',srid=4326)
    dt_created=models.DateTimeField(auto_now_add=True)
    dt_updated=models.DateTimeField(auto_now=True)
    class Meta:
        verbose_name = 'Location'
    def __unicode__(self):
        return self.name
    def __str__(self):
        return self.name
    @property
    def longitude(self):
        return self.point[0]
    @property
    def latitude(self):
        return self.point[1]

Admin.py:

from __future__ import unicode_literals
from django.contrib.gis import admin
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.auth.admin import UserAdmin
from maps.models import *

class LocationAdmin(admin.OSMGeoAdmin):
    model = Location
    list_display = ['name','first_addr','second_addr','town','state','zip_code','country','longitude','latitude','dt_created','dt_updated']
    search_fields = ['first_addr','second_addr','town','state','zip_code']

admin.site.register(Location,LocationAdmin)

回答1:


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

Adding these lines into the setting.py works for me. But when you adding the GDAL_LIBRARY_PATH check what are the gdal libraries you have and add one of the gdal library like 'gdal204'




回答2:


This error will fixed if you add following command at the top of your setting.py file

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



回答3:


If you are using Windows OS and some settings file please use following line:

GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal300'

I am using this for my Django based app.



来源:https://stackoverflow.com/questions/48035684/geodjango-gdalexception-ogr-failure

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