2014-10-08 77 views
1

我有一个纬度&经度来自地理编码器,需要找出这些坐标落入哪个MultiPolygon几何体。在PostGIS的查询是:geodjango - 无法识别亚马逊RDS上的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.

回答

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功能处于公共架构中。在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, 
}