jeroenbourgois
Geometry with MySQL (mariaex) and Ecto
I am saving a model that has a latitude and a longitude. Alongside I would like to save a POINT into my database from those two values. But I cannot find a way to use the POINT type in Ecto. I believe I will need to make a custom Ecto Type, but I am far too inexperienced to do it, and the online tutorials I find seem to tackle simpler field types.
Does anyone have any experience with Geo, Ecto and MySQL? I saw that Mariaex actually supports the Geo types, but I am clueless how to use it with Ecto.
Thanks!
Marked As Solved
jeroenbourgois
We just got it working here! It might have been obvious but we are really new to all of this. So, turns out, mariaex already had support for Geometry. But to get it to work with Ecto, we needed to define a custom type.
defmodule MyApp.EctoPoint do
@behaviour Ecto.Type
def type, do: Mariaex.Geometry.Point
# Casting from input into point struct
def cast(value = %Mariaex.Geometry.Point{}), do: {:ok, value}
def cast(_), do: :error
# loading data from the database
def load(data) do
{:ok, data}
end
# dumping data to the database
def dump(value = %Mariaex.Geometry.Point{}), do: {:ok, value}
def dump(_), do: :error
end
Then, in your schema you can use that type. And in our case, we were always creating the point from user input that went into lat/long input fields, so in the changeset we cast those two to a proper Point.
defmodule MyApp.Restaurant do
use Ecto.Schema
import Ecto
import Ecto.Changeset
schema "pois" do
# ... fields
field(:lat, :float)
field(:long, :float)
field(:point, MyApp.EctoPoint) # <-- custom type here
timestamps()
end
def changeset(%MyApp.Restaurant{} = restaurant, attrs \\ %{}) do
poi
|> cast(attrs, [:lat, :long])
|> validate_required([:name])
|> cast_geo()
end
def cast_geo(%Ecto.Changeset{changes: %{lat: lat, long: long}} = changeset) do
point = %Mariaex.Geometry.Point{coordinates: {lat, long}}
put_change(changeset, :geometry, point)
end
def cast_geo(changeset), do: changeset
end
Works like a charm! Only need to check what type to use in the migration.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








