我正在阅读Daniel Azuma的Geo-Rails博客系列,我遇到了一个我试图复制的部分,但是使用了不同的SRID。具体来说,我引用了http://blog.daniel-azuma.com/archives/69和标题为使用位置数据的部分。
Daniel Azuma有一个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
- 但我的印象是,如果我从long / lat(4326
)<{进行适当的转换 {1}},结果应该非常接近:
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
,而Daniel Azuma的结果是kilometers
。差异为何如此之大?我转错了吗?
数据库版本:13.1
和PostgreSQL 9.3.1