Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting single coordinates for RGeo Point

RGeo provides built in methods for POINT features, for example getter methods lat() and lon() to pull latitude and longitude values from a POINT object. Unfortunately, these don't work as setters. For example:

point = RGeo::Geographic.spherical_factory(:srid => 4326).point(3,5)     // => #<RGeo::Geographic::SphericalPointImpl:0x817e521c "POINT (3.0 5.0)">

I can do this:

point.lat      // => 5.0
point.lon      // => 3.0

But I can't do:

point.lat = 4    // => NoMethodError: undefined method `lat=' for #<RGeo::Geographic::SphericalPointImpl:0x00000104024770>

Any suggestions as to how to implement setter methods? Would you do it in the Model or extend the Feature class?

like image 244
donsteffenski Avatar asked Nov 17 '12 17:11

donsteffenski


1 Answers

I'm the author of RGeo, so you can consider this answer authoritative on that basis.

In short, PLEASE AVOID DOING THIS. RGeo objects intentionally have no setter methods because they are meant to be immutable objects. This is so that they can be cached, used as hash keys, used across threads, etc. Some of the RGeo calculations assume that the value of a feature object will never change, so making changes like this could have unexpected and unpredictable consequences.

If you really want a "changed" value, create a new object. For example:

p1 = my_create_a_point()
p2 = p1.factory.point(p1.lon + 20.0, p2.lat)
like image 96
Daniel Azuma Avatar answered Oct 10 '22 06:10

Daniel Azuma