我想按距离订购,但我收到了错误
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)
答案 0 :(得分:3)
您正在混合地理和几何类型,这就是错误消息的含义。如果查看ST_DWithin文档,您会看到签名是ST_DWithin(几何,几何,距离)或ST_DWithin(地理,地理,距离)。
我对Ruby不太了解,所以我不确定您使用的GEOM_Factory
是否等同GEO_Factory
,但是如果使用ST_GeogFromText而不是ST_GeomFromText
在您的地点和订单条款中,您将处理两个地理位置,这可以解决您的问题。
由于您的坐标位于纬度/经度4326,因此使用地理数据类型是合适的。如果您想进一步了解几何与地理的实用性,请参阅this gis.stackexchange question。