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.
关于mysql空间数据类型的问题已经有了不少,但是我更关注的是如何在rails MVC架构中最好地处理它们。
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?
因此,我的问题是如何在rails中的MVC架构中最好地处理这个问题?
Here are some ideas I had, but nothing really seems clean:
这是我的一些想法,但没有什么看起来干净:
-
Call :after_filter method that takes the new instance of the object and does a raw SQL update that handles the "GeomFromText('POINT(lat long)' ))" goodness. The issue with this is that "lat/long" would be text fields in my create form, although this disrupts the clean form_for :object architecture that rails provides since lat/long aren't really attributes, they're just there to let a human input values that aren't mysql spatials.
调用:after_filter方法,它接受对象的新实例,并执行处理“GeomFromText('POINT(lat long)'))”良好的原始SQL更新。这个问题是“lat / long”将是我的创建表单中的文本字段,虽然这会破坏rails提供的干净的form_for:object体系结构,因为lat / long不是真正的属性,它们只是让它们人类输入值不是mysql空间。
-
Maybe creating a trigger in the db to run after a row insert that updates that row? I have no idea and it doesn't seem like these triggers would have access to the lat/long, unless I stored the lat/long as well as the spatial point, and then created the row in the db with the lat/long decimals, and then ran the trigger after creation to update the spatial. I guess i could also do that with an after_filter if I added the lat/long columns to the model.
也许在db中创建一个触发器,在更新该行的行插入之后运行?我不知道,似乎这些触发器不会访问lat / long,除非我存储lat / long以及空间点,然后使用lat / long小数在db中创建行,然后在创建后运行触发器以更新空间。如果我将lat / long列添加到模型中,我想我也可以使用after_filter。
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.
还有其他想法吗?我认为存储lat / long是多余的,因为我真的会使用空间点进行距离计算等等......但是如果我允许进行人工编辑则可能是必要的。
2 个解决方案
#1
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.
查看Rails的geokit-rails插件,它使用普通的lat / lng列作为浮点数进行距离计算(并使用geokit gem)。但是,如果您想使用数据库的地理空间能力,GeoRuby支持Point或LineString等基本空间功能作为列类型。我希望这些帮助。
#2
I agree with hopeless, geokit is nice, I use it too.
我同意绝望,geokit很好,我也使用它。
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.
如果你想自己做,我会做一个after_filter但是将update方法外部化到一个线程。就像你在保存时没有减速但仍然很好的代码和及时更新的列。
Triggers are not nice, the database should deliver data but not do logic.
触发器不好,数据库应该提供数据但不能做逻辑。
#1
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.
查看Rails的geokit-rails插件,它使用普通的lat / lng列作为浮点数进行距离计算(并使用geokit gem)。但是,如果您想使用数据库的地理空间能力,GeoRuby支持Point或LineString等基本空间功能作为列类型。我希望这些帮助。
#2
I agree with hopeless, geokit is nice, I use it too.
我同意绝望,geokit很好,我也使用它。
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.
如果你想自己做,我会做一个after_filter但是将update方法外部化到一个线程。就像你在保存时没有减速但仍然很好的代码和及时更新的列。
Triggers are not nice, the database should deliver data but not do logic.
触发器不好,数据库应该提供数据但不能做逻辑。