我正在閱讀Daniel Azuma的Geo-Rails博客系列,我遇到了一個我試圖複製的部分,但是使用了不同的SRID。具體來說,我參考了http://blog.daniel-azuma.com/archives/69和標題爲的部分使用位置數據。爲什麼我看到通過SRID 4326和3785計算的距離有很大差異?
丹尼爾東具有locations
表與latlon
屬性:
class CreateLocations < ActiveRecord::Migration
def change
create_table :locations do |t|
t.string :name
t.point :latlon, :geographic => true
t.timestamps
end
end
end
相應Location
類使用spherical_factory
與SRID
4326
:
class Location < ActiveRecord::Base
set_rgeo_factory_for_column(:latlon,
RGeo::Geographic.spherical_factory(:srid => 4326))
end
兩個示例點的創建,和它們之間的距離確定爲:
ruby-1.9.3-p0 :001 > loc = Location.create
ruby-1.9.3-p0 :003 > loc.latlon = "POINT(-122.193963 47.675086)"
ruby-1.9.3-p0 :005 > loc2 = Location.create(:name => 'Space Needle',
:latlon => 'POINT(-122.349341 47.620471)')
ruby-1.9.3-p0 :006 > puts "Distance is %.02f meters" %
loc.latlon.distance(loc2.latlon)
Distance is 13143.18 meters
我正在做點什麼類似,但我得到了一個截然不同的結果(超過6
公里的差異)。
我使用SRID
3785
- 但我的印象是,如果我做適當的轉換從長/ LAT(4326
)到3785
,結果應該是相當接近:
class AddGeopointToLocations < ActiveRecord::Migration
def change
add_column :locations, :geopoint, :point, srid: 3785
add_index :locations, :geopoint, spatial: true
end
end
我Location
型號:
class Location < ActiveRecord::Base
RGEO_FACTORY = RGeo::Geographic.simple_mercator_factory
set_rgeo_factory_for_column(:geopoint, RGEO_FACTORY.projection_factory)
def geopoint_geographic
RGEO_FACTORY.unproject(self.geopoint)
end
def geopoint_geographic=(value)
self.geopoint = RGEO_FACTORY.project(value)
end
end
通過parse_wkt
設置點和計算距離:
2.0.0p247 :001 > loc.geopoint_geographic =
::Location::RGEO_FACTORY.parse_wkt("POINT (-122.193963 47.675086)")
2.0.0p247 :002 > loc2.geopoint_geographic =
::Location::RGEO_FACTORY.parse_wkt("POINT (-122.349341 47.620471)")
2.0.0p247 :003 > loc.geopoint.distance(loc2.geopoint)
=> 19509.352351913036
通過point
設置點和計算距離:
2.0.0p247 :004 > loc.geopoint_geographic =
::Location::RGEO_FACTORY.point(-122.193963, 47.675086)
2.0.0p247 :005 > loc2.geopoint_geographic =
::Location::RGEO_FACTORY.point(-122.349341, 47.620471)
2.0.0p247 :006 > loc.geopoint.distance(loc2.geopoint)
=> 19509.352351913036
我的結果是19.5
kilometers
而丹尼爾東的是13.1
。爲什麼差異如此之大?我轉換不正確?
DB版本:PostgreSQL 9.3.1
和POSTGIS="2.1.0 r11822" GEOS="3.3.8-CAPI-1.7.8" PROJ="Rel. 4.8.0, 6 March 2012"
這是你的問題的答案... –