jeroenbourgois

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

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.

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

Other popular topics Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement