jeroenbourgois

jeroenbourgois

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!

Showing Posts 1 to 4

kokolegorille

kokolegorille

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

https://github.com/bryanjos/geo_postgis

It is for postgres, but might be helpful nevertheless.

jeroenbourgois

jeroenbourgois OP

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.

jeroenbourgois

jeroenbourgois OP

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews