【问题标题】:geodjango - not recognizing PostGIS function on Amazon RDSgeodjango - 无法识别 Amazon RDS 上的 PostGIS 功能
【发布时间】:2014-12-03 18:29:46
【问题描述】:

我有一个地理编码器的纬度和经度结果,需要找出这些坐标属于哪个 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.

【问题讨论】:

    标签: geodjango


    【解决方案1】:

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

    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,
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-15
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-06
      • 2014-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多