felt / geo

A collection of GIS functions for Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Postgrex expected %Postgrex.Point{}, got %Geo.Point

mattcree opened this issue · comments

I'm a bit new to Phoenix so bear with me. At the moment I'm just trying to manually enter some numbers into latitude and longitude form fields and have it add the location to the database.

I'm getting this error:

Postgrex expected %Postgrex.Point{}, got %Geo.Point{coordinates: {4343, 2342342}, srid: 4326}. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.

I've got a simple table with one field

defmodule Test.Repo.Migrations.CreateLocations do
  use Ecto.Migration

  def change do
    create table(:locations) do
      add :location, :point

      timestamps()
    end

  end
end

and the schema is as follows

  schema "locations" do
    field :location, Geo.Point
    field :lat, :float, virtual: true
    field :long, :float, virtual: true

    timestamps()
  end

And my 'create' method in the controller is:

def create(conn, %{"location" => location_params}) do
    IO.inspect location_params
    lat_string = Map.get(location_params, "lat")
    long_string = Map.get(location_params, "long")
    point = "SRID=4326;POINT(#{lat_string} #{long_string})"
    geopoint = Geo.WKT.decode(point)
    IO.inspect geopoint

    case MapView.create_location(%{"location" => geopoint}) do
      {:ok, location} ->
        conn
        |> put_flash(:info, "Location created successfully.")
        |> redirect(to: location_path(conn, :show, location))
      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

It's obviously quite naive but I'm still not really sure the best way to add a point to the database -- the first step is this I suppose. Is there something obviously wrong here?

Okay, after enabling/configuring the GeoPostGIS extension changing my migration type to :geometry and the schema type to Geo.Geometry it seems to allow me to add things to the database. Unfortunately I can't display them in a list because of this:

protocol Phoenix.HTML.Safe not implemented for %Geo.Point{coordinates: {4343.0, 2342342.0}, srid: 4326}. This protocol is implemented for: Atom, BitString, Date, DateTime, Decimal, Ecto.Date, Ecto.DateTime, Ecto.Time, Float, Integer, List, NaiveDateTime, Time, Tuple

I'm assuming none of this is an error with geo though, so feel free to close this (though I'd appreciate anything helpful you might have to say).

Okay, I'm using 'inspect' just for the sake of seeing my location in a table view etc.

Sorry for making you read all that -- but is there any way I might use the :point type and is there anything wrong with using the :geometry type?

@mattcree looks like you found the answer to the geo-specific question. For the phoenix one, you will either probably want to format the Geo.Point struct into a string in order to see in html