如何在半径范围内获得最接近的地理位置?

时间:2014-04-16 21:44:58

标签: postgresql postgis

我正在使用postgis运行rails 4应用程序。 为了获得最接近的点,我有以下sql

 SELECT * FROM geo_name_cities 
 WHERE ST_DWithin(lonlat, ST_Geomfromtext('POINT(11.75292 47.42321)', 1000))
 ORDER BY ST_Distance(lonlat, ST_Geomfromtext('POINT(11.75292 47.42321)',1000))
 limit 5;

出于某种原因,我确实收到以下错误:

ERROR: function st_geomfromtext(unknown, integer) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add  
explicit type casts.
Character: 57

我没有看到postgis doc的差异

 ST_GeomFromText('POINT(-71.064544 42.28787)');

1 个答案:

答案 0 :(得分:2)

如果将PostGIS安装到不在search_path上的架构上(例如,在postgis中),则需要完整提供这些功能(例如,postgis.st_geomfromtext(...))或search_path需要更新。这可以在每个会话中完成:

SET search_path = postgis, public;

永久更改数据库的search_path

ALTER DATABASE my_database SET search_path = postgis, public;

或者对于用户:

ALTER USER postgres SET search_path = postgis, public;