trilogy-libraries / activerecord-trilogy-adapter

Active Record adapter for the Trilogy database client for Rails v6.0 - v7.0.
https://github.com/trilogy-libraries/trilogy
MIT License
171 stars 17 forks source link

Can this adapter support spatial column types? #70

Closed maxKimoby closed 4 months ago

maxKimoby commented 8 months ago

Currently, there are no real solution for implementing spatial columns in Rails + MySQL. There are independent projects that do so, but nothing official. Given that this project would seemingly be built-in in Rails 7.1, is it something that could be implemented?

I've read on the mysql2 issues page that there are no standard ruby objects to instantiate those spatial colums into. What is this project's contributors opinion on the subject? I would expect a better support from Rails + MySQL for spatial columns.

Thanks!

lorint commented 7 months ago

Trilogy supports spatial data with no issues -- to the adapter it simply looks like binary.

Here's one common approach to storing latitude and longitude in MySQL -- use its own "Internal Geometry Format". I had implemented this for another gem I maintain -- The Brick. The spatial data gets stored internally in very much the same way as Well-Known Binary (WKB), just with an initial 4 bytes to indicate the SRID. (So perhaps really it's almost an implementation of extended WKB.)

To make use of WKB data in Ruby, you can use the RGeo gem. Here's how it was implemented in Brick: https://github.com/lorint/brick/blob/6a75e5d9c2f45da55c13db06535c38aae4f10e89/lib/brick/frameworks/rails/engine.rb#L14-L59