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!

1 Like

You might find some information about PostGis Ecto integration in this github project.

It is for postgres, but might be helpful nevertheless.

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.

2 Likes

Just as a follow-up, we do understand the custom Ecto.Type a bit better. We still need the seperate cast like above because the actual Point was not submitted with the form, but for another component we did post the string variant of a Polygon. There inside the cast/1 function we constructed the correct Mariaex.Geometry.Polygon type, so we could just put that field the cast method of our changeset and everything worked!

defmodule MyApp.Ecto.Polygon do
  @behaviour Ecto.Type
  def type, do: Mariaex.Geometry.Polygon

  # Casting from input into point struct
  # The point string is in the form of "lat long, lat long, lat long"
  def cast(points_string) when is_binary(points_string) do
    coordinates =
      points_string
      |> String.split(",")
      |> Enum.map(fn raw_coord ->
        [lat, long] = raw_coord |> String.trim() |> String.split(" ")
        to_coord(lat, long)
      end)

    polygon = %Mariaex.Geometry.Polygon{
      coordinates: [coordinates]
    }

    {:ok, polygon}
  end

  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.Polygon{}), do: {:ok, value}
  def dump(_), do: :error

  defp to_coord(lat_s, long_s), do: {String.to_float(lat_s), String.to_float(long_s)}
end

You might want to read this one: Another cast vs dump question for a custom Ecto type