Alex
Alex

Reputation: 199

Create point by offset in RGeo

I'm writing a relatively simple app in which I'm using RGeo to calculate distances between points on the globe. I'm doing this using a RGeo::Geographic.spherical_factory.

Now I want to be able to create a new point by adding an offset to an existing point. For example, I would like to be able to find the longitude and latitude of the point 500 metres north and 200 metres east of an existing point.

How should I go about doing this?

Upvotes: 1

Views: 521

Answers (1)

Seamus Abshere
Seamus Abshere

Reputation: 8516

Maybe this helps:

a = move_point(-72.4861, 44.1853, 0, 0)     # POINT (-72.4861 44.18529999999999)
b = move_point(-72.4861, 44.1853, 100, 0)   # POINT (-72.48520168471588 44.18529999999999)
c = move_point(-72.4861, 44.1853, 0, 100)   # POINT (-72.4861 44.18594416889434)
puts a.distance(b)
puts a.distance(c)

Which gives you

99.99999999906868
99.99999999906868

Note: I'm not sure what the different between RGeo::Geographic.simple_mercator_factory and RGeo::Geographic.spherical_factory would be here.

require 'rgeo'

def move_point(lon, lat, x_offset_meters, y_offset_meters)
  wgs84 = RGeo::Geographic.simple_mercator_factory.point(lon, lat)
  wgs84_factory = wgs84.factory
  webmercator = wgs84_factory.project wgs84
  webmercator_factory = webmercator.factory
  webmercator_moved = webmercator_factory.point(webmercator.x+x_offset_meters, webmercator.y+y_offset_meters)
  wgs84_factory.unproject webmercator_moved
end

From How to move a point in Rgeo

Upvotes: 1

Related Questions