thousandsofthem

thousandsofthem

Custom ecto/postgrex types for Point data type?

At this moment postgrex package have built-in point data type (geometric, not a PostGIS one), according to the source code:
https://github.com/elixir-ecto/postgrex/blob/master/lib/postgrex/builtins.ex#L74
https://github.com/elixir-ecto/postgrex/blob/master/lib/postgrex/extensions/point.ex

I’m trying to use it with Ecto, no luck so far. Is there a way to use it in ecto schema? Regular way (Postgrex.Types.define) doesn’t seem to work for me

Marked As Solved

thousandsofthem

thousandsofthem

UPD: created custom ecto wrapper, seems to be working correctly. No idea why no one did that before

defmodule App.EctoPoint do
  @behaviour Ecto.Type
  def type, do: Postgrex.Point

  # Casting from input into point struct
  def cast(value = %Postgrex.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 = %Postgrex.Point{}), do: {:ok, value}
  def dump(_), do: :error
end

Also Liked

thousandsofthem

thousandsofthem

Sure. Besides, geo_postgis is for PostGIS stuff (separate package, not installed by default), not a regular point .
e.g. PostGIS’ Geo.Point includes coordinates and srid projection, different data type.
PostGIS makes sense if someone goes full-blown geographic approach, for more humble use-cases (e.g. sort stuff by distance to a person, sped up by an index) point is more than enough

josevalim

josevalim

Creator of Elixir

That looks great. Also see the geo_postgis library: GitHub - felt/geo_postgis: Postgrex Extension for PostGIS · GitHub :slight_smile:

josevalim

josevalim

Creator of Elixir

I see. Thanks for clarifying!

Where Next?

Popular in Questions Top

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
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement