也许这有点矫枉过正,但如果你正在使用postgres http://www.postgresql.org/你可以安装postgis http://postgis.net/管理空间数据的扩展。然后在一个irb你可以这样做:
result = ActiveRecord::Base.connection.execute('SELECT
ST_Contains(ST_SetSRID(ST_MakeBox2D(ST_Point(-0.489, 51.28), ST_Point(0.236, 51.686)), 4326),
ST_SetSRID(ST_Point(-0.1265, 51.483), 4326))')
查询使用以下命令检查该点是否在给定的 bbox 内ST_contains函数 http://postgis.refractions.net/docs/ST_Contains.html
这将返回:
=> #<PG::Result:0x007fa517fcbe08 @connection=#<PG::Connection:0x007fa5167f8970 @socket_io=nil, @notice_receiver=nil, @notice_processor=nil>>
然后你可以这样做:
result.first
这将返回:
{"st_contains"=>"t"}
有了这一点-0.7265, 44.483(框外的点)结果将是:
{"st_contains"=>"f"}
如果您不想使用原始 sql,您可以使用 gems 来管理空间数据。一个非常好的是:rgeo https://github.com/dazuma/rgeo。我建议阅读作者的blog http://blog.daniel-azuma.com/archives/28
Using rgeo https://github.com/dazuma/rgeo您可以使用“地理类型”(例如点、多边形等)为模型定义属性,然后使用以下函数包含? http://dazuma.github.io/rgeo/rdoc/RGeo/Feature/Geometry.html#method-i-contains-3F
我给你留下一个gist https://gist.github.com/ldlsegovia/7389724有非常基本的安装说明postgis与乌班图。