UndefinedFunction:错误:函数st_distance(地理,几何,数字)不存在

时间:2014-11-14 16:55:13

标签: postgresql ruby-on-rails-4 postgis

我想按距离订购,但我收到了错误

UndefinedFunction: ERROR:  function st_distance(geography, geometry, numeric) does not exist

.order("ST_Distance(lonlat, ST_Geomfromtext('#{point}'), #{radius}) ")

如果我删除了上面一行,它就可以正常工作。

它出了什么问题?

模型

  scope :nearby, lambda { |radius_in_km, lon, lat|
    point = GEOG_FACTORY.point(lon, lat)
    radius = radius_in_km.to_f*1000
    where("ST_DWithin(lonlat, ST_GeomFromText('#{point}'), #{radius} ) ")
    .order("ST_Distance(lonlat, ST_Geomfromtext('#{point}'), #{radius}) ")
  }

更多细节错误日志

PG::UndefinedFunction: ERROR:  function st_distance(geography, geometry, numeric) does not exist
LINE 1: ...4028 6.530438999999999)'), 100000.0 ) )  ORDER BY ST_Distanc...
                                                             ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
: SELECT  "weather_logs".* FROM "weather_logs"  WHERE (ST_DWithin(lonlat, ST_GeomFromText('POINT (101.3034028 6.530438999999999)'), 100000.0 ) )  ORDER BY ST_Distance(lonlat, ST_Geomfromtext('POINT (101.3034028 6.530438999999999)'), 100000.0) , "weather_logs"."datetime" ASC LIMIT 20000
Completed 200 OK in 161ms (Views: 0.2ms | ActiveRecord: 5.4ms)

1 个答案:

答案 0 :(得分:3)

您正在混合地理和几何类型,这就是错误消息的含义。如果查看ST_DWithin文档,您会看到签名是ST_DWithin(几何,几何,距离)或ST_DWithin(地理,地理,距离)。

我对Ruby不太了解,所以我不确定您使用的GEOM_Factory是否等同GEO_Factory,但是如果使用ST_GeogFromText而不是ST_GeomFromText在您的地点和订单条款中,您将处理两个地理位置,这可以解决您的问题。

由于您的坐标位于纬度/经度4326,因此使用地理数据类型是合适的。如果您想进一步了解几何与地理的实用性,请参阅this gis.stackexchange question