处理 Rails 中的纬度/经度和 mysql 空间点

发布于 2024-07-29 06:15:39 字数 772 浏览 6 评论 0原文

关于 mysql 空间数据类型存在大量问题,但我的问题更具体的是如何在 Rails MVC 架构中最好处理它们。

我有一个输入表单,管理员用户可以在其中创建新的兴趣点(例如餐厅)并输入一些信息。 他们还可以以十进制格式输入人类可读的纬度和经度。

但是,对于距离计算等......我将位置数据存储为数据库中的空间点。

因此,我的问题是,如何在 Rails 的 MVC 架构中最好地处理这个问题?

以下是我的一些想法,但似乎没有什么真正干净的:

  • Call :after_filter 方法,该方法采用对象的新实例并执行处理“GeomFromText('POINT(lat long)' ))”的原始 SQL 更新善良。 问题是“纬度/经度”将是我创建表单中的文本字段,尽管这破坏了 Rails 提供的干净的 form_for :object 架构,因为纬度/经度并不是真正的属性,它们只是为了让不是 mysql 空间的人工输入值。

  • 也许在数据库中创建一个触发器以在更新该行的行插入后运行? 我不知道,而且这些触发器似乎无法访问纬度/经度,除非我存储纬度/经度以及空间点,然后使用纬度/经度小数在数据库中创建行,然后在创建后运行触发器来更新空间。 我想如果我将纬度/经度列添加到模型中,我也可以使用 after_filter 来做到这一点。

还有其他想法吗? 我认为存储纬度/经度是多余的,因为我真的会使用空间点进行距离计算等......但如果我允许人工编辑,这可能是必要的。

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:

  • 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.

  • 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝戈者 2024-08-05 06:15:39

查看 Rails 的 geokit-rails 插件,它使用普通的 lat/lng 列进行距离计算浮动(并使用 geokit gem)。 但是,如果您想使用数据库的地理空间功能,GeoRuby 支持基本的空间功能,例如Point 或 LineString 作为列类型。 我希望这些有所帮助。

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.

天暗了我发光 2024-08-05 06:15:39

我同意绝望,geokit很好,我也用它。

如果你想自己做,我会做一个 after_filter 但将更新方法外部化到一个线程。 就像这样,您在保存时不会减慢速度,但仍然有很好的代码和及时更新的列。

触发器不好,数据库应该传递数据而不是执行逻辑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文