2012-09-05 28 views
0

我试图让英里远特定职位记录的数量...为什么我的rgeo/postgis与现实距离太远了?

class Post < ActiveRecord::Base 
     attr_accessor :lat, :lng 
     set_rgeo_factory_for_column(:location, RGeo::Geographic.spherical_factory(:srid => 4326)) 

     def distance_from(current_location) 
     rgeo_factory.point(current_location[:lng], current_location[:lat]).distance(location)/1.60934 
     end 

    private 

     def update_location! 
     self.location = rgeo_factory.point(lng, lat) if lat && lng 
     end 

     def rgeo_factory 
     Post.rgeo_factory_for_column(:location) 
     end 

    end 

如果我创建内华达州拉斯维加斯的位置后(纬度:36.255123,经度:-115.238348)我确认它 -

1.9.3p125 :018 > p.lat = 36.255123 
=> 36.255123 
1.9.3p125 :019 > p.lng = -115.238348 
=> -115.238348 
1.9.3p125 :019 > p  
#<Post id: 133, location: #<RGeo::Geographic::SphericalPointImpl:0x81d7130c "POINT (-115.238348 36.255123)"> 

当我试图获得该职位,并在加利福尼亚州一个点之间的距离,我得到英里一个疯狂的数量...

1.9.3p125 :027 > p.distance_from(:lat => 34.019454, :lng => -118.491191) 
=> 240327.390598477 

我在做什么错误?

回答

4

你确定distance()返回km吗?您对英里的转换似乎是这样想的。 PostGIS本地距离以米为单位返回。是否有可能正确的里程数是约240?

4

RGeo球形工厂确实以米为单位返回距离结果。大多数坐标系统以米为单位工作(或其中的一些失真)。如果您需要里程,您可以乘以换算系数(约0.000621371)。