ajhit406
ajhit406

Reputation: 1405

Handling lat/long and mysql spatial points in rails

There have been a decent amount of questions about mysql spatial datatypes, however mine is more specific to how best to deal with them within a rails MVC architecture.

I have an input form where an admin user can create a new point of interest, let's say, a restaurant and input some information. They can also input a human-readable latitude and longitude in decimal format.

However, for distance calculations, etc... I am storing the location data as a spatial point in the database.

My question therefore, is how to best handle this in the MVC architecture in rails?

Here are some ideas I had, but nothing really seems clean:

Any other ideas? I think storing the lat/long is redundant since I'll really be using the spatial point for distance calculations, etc... but it might be necessary if I'm allowing for human editing.

Upvotes: 2

Views: 3374

Answers (2)

ideasasylum
ideasasylum

Reputation: 2130

Check out the geokit-rails plugin for Rails which does distance calculations using plain lat/lng columns as floats (and uses the geokit gem). However, if you'd like to use your database's geo-spatial abilities, GeoRuby supports the basic spatial features like Point or LineString as column types. I hope these help.

Upvotes: 3

Beffa
Beffa

Reputation: 915

I agree with hopeless, geokit is nice, I use it too.

If you want to do it yourself, I would do an after_filter but externalize the update method to a thread. Like that you don't have a slow down while saving but still nice code and timely updated columns.

Triggers are not nice, the database should deliver data but not do logic.

Upvotes: 0

Related Questions