GEODJANGO中不存在几何对象。

3
我已经安装了Geodjango所有的依赖项!现在我正在按照它的教程https://docs.djangoproject.com/en/1.2/ref/contrib/gis/tutorial/进行操作 -- 问题:命令 python manage.py syncdb 在mpoly geometry(multipolygon 4326) not null处生成错误,因为该几何图形不存在。
from django.contrib.gis.db import models

class WorldBorders(models.Model):
    # Regular Django fields corresponding to the attributes in the
    # world borders shapefile.
    name = models.CharField(max_length=50)
    area = models.IntegerField()
    pop2005 = models.IntegerField('Population 2005')
    fips = models.CharField('FIPS Code', max_length=2)
    iso2 = models.CharField('2 Digit ISO', max_length=2)
    iso3 = models.CharField('3 Digit ISO', max_length=3)
    un = models.IntegerField('United Nations Code')
    region = models.IntegerField('Region Code')
    subregion = models.IntegerField('Sub-Region Code')
    lon = models.FloatField()
    lat = models.FloatField()

    # GeoDjango-specific: a geometry field (MultiPolygonField), and
    # overriding the default manager with a GeoManager instance.
    mpoly = models.MultiPolygonField() //ERROR HERE

如何解决这个错误?
更新:
运行 python manage.py sqlall world 会生成以下输出:
BEGIN;
CREATE TABLE "world_worldborders" (
    "id" serial NOT NULL PRIMARY KEY,
    "name" varchar(50) NOT NULL,
    "area" integer NOT NULL,
    "pop2005" integer NOT NULL,
    "fips" varchar(2) NOT NULL,
    "iso2" varchar(2) NOT NULL,
    "iso3" varchar(3) NOT NULL,
    "un" integer NOT NULL,
    "region" integer NOT NULL,
    "subregion" integer NOT NULL,
    "lon" double precision NOT NULL,
    "lat" double precision NOT NULL,
    "mpoly" geometry(MULTIPOLYGON,4326) NOT NULL
)
;
CREATE INDEX "world_worldborders_mpoly_id" ON "world_worldborders" USING GIST ( "mpoly" );

COMMIT;

运行python manage.py syncdb命令后,会生成以下错误信息:程序错误:类型“geometry”不存在

root@cvp-linux:~/geodjango# python manage.py syncdb Syncing... Creating tables ... Creating table django_admin_log Creating table auth_permission Creating table auth_group_permissions Creating table auth_group Creating table auth_user_groups Creating table auth_user_user_permissions Creating table auth_user Creating table django_content_type Creating table django_session Creating table world_worldborders Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute output = self.handle(*args, **options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 415, in handle return self.handle_noargs(**options) File "/usr/local/lib/python2.7/dist-packages/south/management/commands/syncdb.py", line 92, in handle_noargs syncdb.Command().execute(**options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute output = self.handle(*args, **options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 415, in handle return self.handle_noargs(**options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 107, in handle_noargs cursor.execute(statement) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 69, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute return self.cursor.execute(sql, params) File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 99, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 51, in execute return self.cursor.execute(sql) django.db.utils.ProgrammingError: type "geometry" does not exist LINE 14: "mpoly" geometry(MULTIPOLYGON,4326) NOT NULL


1
你解决了吗?能和我们分享一下吗?谢谢。 - Armance
5个回答

5

需要检查的几个事项:

  • 您已经运行了 CREATE EXTENSION postgis;
  • 在项目的 settings.py 文件中设置了 GEOS_LIBRARY_PATH

第二点让我有些麻烦,但最终第一点解决了问题。我在 Cygwin 64 位上运行,所以需要做一些额外的工作才能使其正常工作。如果您没有在 Cygwin 环境下运行,则您要找的文件很可能是 libgeos_c.so;我需要将其指向 cyggeos_c-1.dll(仍在 Windows 上运行,因此需要找到 .dll 扩展名)。

我确信自己已经运行了 CREATE EXTENSION 命令,但可能我没有在 Cygwin 环境下运行它。或者我曾尝试过,但忘记了我的配置非常复杂,导致 postgres 服务器未在 localhost 上运行,因此我避免使用 psql。这就是我的愚蠢。


0

在发布问题之前,我阅读了这个内容,但我不理解那个票证。您能否请解释一下解决这个问题的步骤? - Tameen Malik
尝试运行 python manage.py sqlall world 命令,并将输出内容发布在你的问题中。 - dhana
django.core.exceptions.ImproperlyConfigured: 无法确定数据库 "geodjango" 的 PostGIS 版本。GeoDjango 至少需要 PostGIS 版本 1.3。该数据库是从空间数据库模板创建的吗?@dhana - Tameen Malik
1
请查看此链接:https://dev59.com/KGs05IYBdhLWcg3wCNhG - dhana
在您的settings.py文件中添加此GEOS_LIBRARY_PATH = '/home/bob/local/lib/libgeos_c.so',参考链接在这里https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/geolibs/#can-t-find-geos-library。 - dhana
显示剩余2条评论

0

尝试在您的models.py中,在mpoly之后添加

objects = models.GeoManager()


你应该[编辑]你的答案,添加一个描述来解释如何回答这个问题。 - Artjom B.

0
在我的情况下,问题是命令:
-- Enable PostGIS (includes raster)
CREATE EXTENSION postgis;
-- Enable Topology
CREATE EXTENSION postgis_topology;
-- fuzzy matching needed for Tiger
CREATE EXTENSION fuzzystrmatch;
-- Enable US Tiger Geocoder
CREATE EXTENSION postgis_tiger_geocoder;

应该在特定数据库的上下文中运行,因此您需要首先连接到your_db

\connect your_db

然后运行以上代码!


0
假设您正在使用postgis与公共模式中的表/模型:
创建扩展postgis;
假设您为表/模型创建了另一个模式,在settings.py中添加选项:
DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'OPTIONS': {
            'options': '-c search_path=YOUR_SCHEMA_NAME,public'
        },
        'NAME': 'DATABASE_NAME',
        'USER': 'DATABASE_USER',
        'PASSWORD': 'DATABASE_PASSWORD',
        'HOST': 'DATABASE_HOST',
        'PORT': 'DATABASE_PORT',
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接