geodjango - 无法识别Amazon RDS上的PostGIS功能

时间:2014-10-08 23:28:06

标签: geodjango

我有一个纬度&经度来自地理编码器,需要找出这些坐标落入哪个MultiPolygon几何体。 PostGIS查询将是:

select * from gis.zipcodes where ST_Contains(geom, ST_GeomFromText('POINT(-74.0863037109375 40.704586878965245)', 4269));

其中geom是多面几何列。我试过这样做:

pnt = GEOSGeometry('SRID=4269;POINT(-74.0863037109375 40.704586878965245)') Zipcode.objects.filter(geom__contains=pnt)

但收到错误“没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。”

然后我尝试制作一个原始查询,如:

Zipcode.objects.raw("select * from gis.zipcodes where ST_Contains(geom, ST_GeomFromText('POINT(-74.0863037109 40.704586879)', 4269))")

并收到以下错误。我将此查询复制并粘贴到psql中,它按预期工作。我的猜测是Django没有认识到PostGIS扩展,但我不确定为什么。我已将django.contrib.gis添加到INSTALLED_APPS,将db ENGINE设置为django.contrib.gis.db.backends.postgis,将POSTGIS_VERSION =(2,1)和POSTGIS_TEMPLATE ='template1'添加到我的设置并运行CREATE EXTENSION postgis;在我的数据库上。我还尝试了一些其他查询并收到了类似的错误。

Traceback (most recent call last): File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 111, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view return view_func(*args, **kwargs) File "/Library/Python/2.7/site-packages/django/views/generic/base.py", line 69, in view return self.dispatch(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 403, in dispatch response = self.handle_exception(exc) File "/Library/Python/2.7/site-packages/rest_framework/views.py", line 400, in dispatch response = handler(request, *args, **kwargs) File "/Library/Python/2.7/site-packages/rest_framework/decorators.py", line 50, in handler return func(*args, **kwargs) File "/data/django-apis/hcpro/proapp/views.py", line 47, in zipcode_from_coord print raw_qs[0] File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1598, in __getitem__ return list(self)[k] File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1535, in __iter__ query = iter(self.query) File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 76, in __iter__ self._execute_query() File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 90, in _execute_query self.cursor.execute(self.sql, self.params) File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 81, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) ProgrammingError: function st_geomfromtext(unknown, integer) does not exist LINE 1: ...elect * from gis.zipcodes where ST_Contains(geom, ST_GeomFro... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.

1 个答案:

答案 0 :(得分:0)

这是由于未正确设置搜索路径造成的。我使用以下方法在模型中正确设置了架构:

class Zipcode
    ...
    zip = models.CharField(max_length=5, null=False, blank=False)
    ...
    geom = models.GeometryField(srid=4269)

    class Meta:
        managed = False
        db_table = 'zipcodes'
        in_db = 'gis'

    objects = models.GeoManager()

当我在settings.py文件中定义数据库时,我对搜索路径使用了相同的“gis”模式。问题是,GIS功能在公共模式中。在settings.py中定义这样的数据库解决了这个问题:

'gis': {
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': "gis",
    'OPTIONS': {
        'options': '-c search_path=gis,public,pg_catalog'
    },
    'USER': DB_DEFAULT.user,
    'PASSWORD': DB_DEFAULT.password,
    'HOST': DB_DEFAULT.host,
    'PORT': DB_DEFAULT.port,
}