GeoPostGIS v3.3.1 Geo.PostGIS.Extension View Source

PostGIS extension for Postgrex. Supports Geometry and Geography data types

#Create a new Postgrex Types module:
Postgrex.Types.define(MyApp.PostgresTypes, [Geo.PostGIS.Extension], [])

#If using with Ecto, you may want something like thing instead
Postgrex.Types.define(MyApp.PostgresTypes,
              [Geo.PostGIS.Extension] ++ Ecto.Adapters.Postgres.extensions(),
              json: Poison)

opts = [hostname: "localhost", username: "postgres", database: "geo_postgrex_test",
types: MyApp.PostgresTypes ]

[hostname: "localhost", username: "postgres", database: "geo_postgrex_test",
  types: MyApp.PostgresTypes]

{:ok, pid} = Postgrex.Connection.start_link(opts)
{:ok, #PID<0.115.0>}

geo = %Geo.Point{coordinates: {30, -90}, srid: 4326}
%Geo.Point{coordinates: {30, -90}, srid: 4326}

{:ok, _} = Postgrex.Connection.query(pid, "CREATE TABLE point_test (id int, geom geometry(Point, 4326))")
{:ok, %Postgrex.Result{columns: nil, command: :create_table, num_rows: 0, rows: nil}}

{:ok, _} = Postgrex.Connection.query(pid, "INSERT INTO point_test VALUES ($1, $2)", [42, geo])
{:ok, %Postgrex.Result{columns: nil, command: :insert, num_rows: 1, rows: nil}}

Postgrex.Connection.query(pid, "SELECT * FROM point_test")
{:ok, %Postgrex.Result{columns: ["id", "geom"], command: :select, num_rows: 1,
rows: [{42, %Geo.Point{coordinates: {30.0, -90.0}, srid: 4326}}]}}

Link to this section Summary

Functions

Returns a quoted list of clauses that decode a binary to an Elixir value.

Returns a quoted list of clauses that encode an Elixir value to iodata.

Should perform any initialization of the extension. The function receives the user options. The state returned from this function will be passed to other callbacks.

Specifies the types the extension matches, see Postgrex.TypeInfo for specification of the fields.

Link to this section Functions

Returns a quoted list of clauses that decode a binary to an Elixir value.

The pattern must use binary syntax and decode a fixed length using the signed 32 bit big endian integer byte length header.

def decode(_) do
  quote do
    # length header is in bytes
    <<len :: signed-32, integer :: signed-size(len)-unit(8)>> ->
      integer
  end
end

Callback implementation for Postgrex.Extension.decode/1.

Returns a quoted list of clauses that encode an Elixir value to iodata.

It must use a signed 32 bit big endian integer byte length header.

def encode(_) do
  quote do
    integer ->
      <<8 :: signed-32, integer :: signed-64>>
  end
end

Callback implementation for Postgrex.Extension.encode/1.

Returns the format the type should be encoded as. See http://www.postgresql.org/docs/9.4/static/protocol-overview.html#PROTOCOL-FORMAT-CODES.

Callback implementation for Postgrex.Extension.format/1.

Should perform any initialization of the extension. The function receives the user options. The state returned from this function will be passed to other callbacks.

Callback implementation for Postgrex.Extension.init/1.

Specifies the types the extension matches, see Postgrex.TypeInfo for specification of the fields.

Callback implementation for Postgrex.Extension.matching/1.