Skip to content

A set of geometry types for WKT/WKB and GeoJson.

License

Notifications You must be signed in to change notification settings

hrzndhrn/geometry

Repository files navigation

Geometry

Hex.pm: version GitHub: CI status Coveralls: coverage License: MIT

A set of geometry types for WKT/WKB, EWKT/EWKB and GeoJson.

Installation

The package can be installed by adding geometry to your list of dependencies in mix.exs:

def deps do
  [
    {:geometry, "~> 0.3"}
  ]
end

Examples

iex> wkb = "00000000013FF0000000000000400199999999999A"
"00000000013FF0000000000000400199999999999A"
iex> wkb |> Base.decode16!() |> Geometry.from_wkb()
{:ok, %Geometry.Point{coordinate: [1.0, 2.2]}}
iex> point = wkb |> Base.decode16!() |> Geometry.from_wkb!()
%Geometry.Point{coordinate: [1.0, 2.2]}
iex> Geometry.to_wkt(point)
"Point (1.0 2.2)"

iex> line_string = Geometry.from_wkt!("LineString Z (1 2 3, 4 5 6, 9 9 9)")
%Geometry.LineStringZ{points: [[1, 2, 3], [4, 5, 6], [9, 9, 9]]}
iex> Geometry.to_wkb(line_string) |> Base.encode16()
"010200008003000000000000000000F03F00000000000000400000000000000840000000000000104000000000000014400000000000001840000000000000224000000000000022400000000000002240"
iex> Geometry.to_wkb(line_string, :xdr) |> Base.encode16()
"0080000002000000033FF000000000000040000000000000004008000000000000401000000000000040140000000000004018000000000000402200000000000040220000000000004022000000000000"

iex> polygon = Geometry.from_wkt!("POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))")
%Geometry.Polygon{
  rings: [
    [[35, 10], [45, 45], [15, 40], [10, 20], [35, 10]],
    [[20, 30], [35, 35], [30, 20], [20, 30]]
  ]
}
iex> polygon |> Geometry.to_geo_json() |> Jason.encode!() |> IO.puts()
{"coordinates":[[[35,10],[45,45],[15,40],[10,20],[35,10]],[[20,30],[35,35],[30,20],[20,30]]],"type":"Polygon"}
:ok