Sql 无法使用postgis和rails 4中的ST_distance查找两个坐标之间的距离
我有一个具有以下模式的供应商模型Sql 无法使用postgis和rails 4中的ST_distance查找两个坐标之间的距离,sql,ruby-on-rails,ruby,postgresql,postgis,Sql,Ruby On Rails,Ruby,Postgresql,Postgis,我有一个具有以下模式的供应商模型 create_table "vendors", force: :cascade do |t| t.string "name" t.decimal "latitude", precision: 9, scale: 6 t.decimal "longitude", precision: 9, scale: 6 t.datetime "created_at", null: false t.datetime
create_table "vendors", force: :cascade do |t|
t.string "name"
t.decimal "latitude", precision: 9, scale: 6
t.decimal "longitude", precision: 9, scale: 6
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
我用伪造的gem和一些硬编码值填充数据库,以获得经度和纬度
我需要在特定的给定距离内找到附近的供应商
scope :close_to, -> (latitude, longitude, distance_in_meters = 2000) {
where(%{
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || vendors.longitude || ' ' ||
vendors.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
} % [longitude, latitude, distance_in_meters])
}
class Vendor < ActiveRecord::Base
scope :close_to, -> (latitude, longitude, distance_in_meters = 2000) {
where(%{
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || vendors.longitude || ' ' ||
vendors.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
} % [longitude, latitude, distance_in_meters])
}
scope :dist, -> {
where(%{
ST_Distance(
ST_GeographyFromText(
'SRID=4326;POINT(' || vendors.longitude || ' ' ||
vendors.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(-76.000000
39.000000)'),
) as distance;
} % )
}
end
所以如果我打电话
Vendor.close_to(39.000000, -76.000000, 500)
在rails控制台中,我得到了四行。现在,我需要找到这些行坐标和范围内提到的坐标之间的实际距离
scope :close_to, -> (latitude, longitude, distance_in_meters = 2000) {
where(%{
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || vendors.longitude || ' ' ||
vendors.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
} % [longitude, latitude, distance_in_meters])
}
class Vendor < ActiveRecord::Base
scope :close_to, -> (latitude, longitude, distance_in_meters = 2000) {
where(%{
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || vendors.longitude || ' ' ||
vendors.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
} % [longitude, latitude, distance_in_meters])
}
scope :dist, -> {
where(%{
ST_Distance(
ST_GeographyFromText(
'SRID=4326;POINT(' || vendors.longitude || ' ' ||
vendors.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(-76.000000
39.000000)'),
) as distance;
} % )
}
end
它没有给出四行的距离,实际上抛出了一个语法错误。我是rails和postgres开发新手。我相信我完全错了这个概念。请帮助我纠正它,我如何从给定坐标在特定范围内找到附近的供应商,以及从给定坐标找到每个附近供应商之间的距离您可以添加您得到的实际语法错误吗?语法错误:/home/osbox/office/refuelly/app/models/vendor.rb:19:语法错误,意外“)”尝试删除结尾(
}%)前一行中的%
。
应该是})
)我尝试了您的建议,但导致此错误ActiveRecord::StatementInvalid:PG::SyntaxError:error:SyntaxError at或附近”)“第15行:)作为距离^虽然我不确定您想要实现什么,但我猜您在39.000000);
之后缺少一个参数?大概是500
?你能添加你得到的实际语法错误吗?语法错误:/home/osboxes/office/refuelly/app/models/vendor.rb:19:语法错误,意外“,”尝试删除结尾(})前一行中的%
应该是})
)我尝试了你的建议,但导致此错误的是ActiveRecord::StatementInvalid:PG::SyntaxError:error:SyntaxError位于或接近“)行15:)作为距离^虽然我不确定您想要实现什么,但我猜您在39.000000);
之后缺少一个参数?大概是500
?