Why am I seeing a great discrepancy between distances calculated via SRID 4326 and 3785?

460 views Asked by At

I am reading Daniel Azuma's Geo-Rails blog series, and I came across a section that I am trying to replicate, but with a different SRID. Specifically, I am referencing http://blog.daniel-azuma.com/archives/69 and the section titled Working with location data.

Daniel Azuma has a locations table with a latlon attribute:

class CreateLocations < ActiveRecord::Migration
  def change
    create_table :locations do |t|
      t.string :name
      t.point :latlon, :geographic => true
      t.timestamps
    end
  end
end

The corresponding Location class uses spherical_factory with SRID 4326:

class Location < ActiveRecord::Base
  set_rgeo_factory_for_column(:latlon,
    RGeo::Geographic.spherical_factory(:srid => 4326))
end

Two example points are created, and the distance between them is determined:

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

I am doing something similar, but I am getting a drastically different result (more than 6 kilometers of difference).

I am using SRID 3785 - but I am under the impression that if I do the appropriate conversion from long/lat (4326) to 3785, the results should be fairly close:

class AddGeopointToLocations < ActiveRecord::Migration
  def change
    add_column :locations, :geopoint, :point, srid: 3785
    add_index :locations, :geopoint, spatial: true
  end
end

My Location model:

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

Set the points via parse_wkt and calculate the distance:

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

Set the points via point and calculate the distance:

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

My result is 19.5 kilometers whereas Daniel Azuma's is 13.1. Why is the difference so great? Am I converting incorrectly?

DB versions: PostgreSQL 9.3.1 and POSTGIS="2.1.0 r11822" GEOS="3.3.8-CAPI-1.7.8" PROJ="Rel. 4.8.0, 6 March 2012"

1

There are 1 answers

0
boulder_ruby On

The units for the Popular Visualization CRS Mercator (EPSG:3785) are meters (m). The units for WGS84 (EPSG:4326) are degrees.