Skip to content

Instantly share code, notes, and snippets.

@dodecaphonic
Created January 21, 2015 18:32
Show Gist options
  • Save dodecaphonic/a53b6f84fd58edb27f2d to your computer and use it in GitHub Desktop.
Save dodecaphonic/a53b6f84fd58edb27f2d to your computer and use it in GitHub Desktop.
require "rgeo"
WGS84 = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
WGS84_UTM = "+proj=utm +zone=22 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
x0, y0 = -48.10311146131618, -15.894902327065926
x1, y1 = -48.103022948419394, -15.894858472899413
mercator = RGeo::Geographic.simple_mercator_factory
geographic = RGeo::Geographic.projected_factory(projection_proj4: WGS84)
spherical = RGeo::Geographic.spherical_factory
utm = RGeo::Geographic.projected_factory(projection_proj4: WGS84_UTM)
p0m = mercator.point(x0, y0)
p1m = mercator.point(x1, y1)
p0g = geographic.point(x0, y0)
p1g = geographic.point(x1, y1)
p0s = spherical.point(x0, y0)
p1s = spherical.point(x1, y1)
p0u = utm.point(x0, y0)
p1u = utm.point(x1, y1)
p0ul = utm.point(x0, y0).projection
p1ul = utm.point(x1, y1).projection
dist = Math.sqrt((p1ul.x - p0ul.x)**2 + (p1ul.y - p0ul.y)**2)
puts "Mercator: #{p0m.distance(p1m)}"
puts "Geographic: #{p0g.distance(p1g) * 111_300}"
puts "Spherical: #{p0s.distance(p1s)}"
puts "UTM: #{p0u.distance(p1u)}"
puts "UTM Linear: #{dist}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment