GeoDjango GDALException-OGR失败 [英] GeoDjango GDALException - OGR failure

查看:92
本文介绍了GeoDjango GDALException-OGR失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

安装GeoDjango之后,我想在管理面板中创建一个"Location"对象,该对象使用地址和地图上的一个点.提交表单后,出现类似这样的错误...

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.

我尝试查看类似的问题,像这里,但所有解决方案均无效.此外,搜索无法加载PROJ.4库"(第一条回溯线)并没有成功.

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.

任何帮助将不胜感激!-让我知道是否应该使用我的settings.py或其他相关文件来更新此问题.

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

完整追溯:

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.

为位置添加了我的models.py和admin.py

Added my models.py and admin.py for Location

models.py:

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)

推荐答案

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

将这些行添加到setting.py中对我有用.但是,当您添加GDAL_LIBRARY_PATH时,请检查您拥有哪些gdal库,并添加一个gdal库,例如"gdal204"

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'

这篇关于GeoDjango GDALException-OGR失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆