View Source Geometry (Geometry v0.4.0)

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

Geometry provide the decoding and encoding for geometires of type WKT/EWKT, WKB/EWKB and GeoJon.

The following gemetries are supported:

For GeoJson also Geometry.Feature and Geometry.FeatureCollection are supported.

Link to this section Summary

Types

An n-dimensional coordinate.

A list of n-dimensional coordinates.

Byte order.

The Spatial Reference System Identifier to identify projected, unprojected, and local spatial coordinate system definitions.

t()

The supported geometries.

Well-Known Binary (WKB) is the binary representation of WKT.

Well-Known Text (WKT) is a text markup language for representing vector geometry objects.

Functions

Returns true if a geometry is empty.

Returns an :ok tuple with the geometry from the given EWKB binary.

The same as from_ewkb/1, but raises a Geometry.DecodeError exception if it fails.

Returns an :ok tuple with the geometry from the given EWKT string.

The same as from_ewkt/1, but raises a Geometry.DecodeError exception if it fails.

Returns an :ok tuple with the geometry from the given GeoJSON term.

The same as from_geo_json/1, but raises a Geometry.DecodeError exception if it fails.

Returns an :ok tuple with the geometry from the given WKB binary.

The same as from_wkb/1, but raises a Geometry.DecodeError exception if it fails.

Returns an :ok tuple with the geometry from the given WKT string.

The same as from_wkt/1, but raises a Geometry.DecodeError exception if it fails.

Returns the EWKB representation of a geometry.

Returns the EWKT representation of the given geometry.

Returns the GeoJSON term representation of a geometry.

Returns the WKB representation of a geometry.

Returns the WKT representation of the given geometry.

Link to this section Types

@type coordinate() :: [number(), ...]

An n-dimensional coordinate.

@type coordinates() :: [coordinate()]

A list of n-dimensional coordinates.

@type endian() :: :ndr | :xdr

Byte order.

  • :ndr: Little endian byte order encoding
  • :xdr: Big endian byte order encoding
@type geo_json_term() :: map()

A GeoJson term.

@type srid() :: non_neg_integer()

The Spatial Reference System Identifier to identify projected, unprojected, and local spatial coordinate system definitions.

The supported geometries.

@type wkb() :: binary()

Well-Known Binary (WKB) is the binary representation of WKT.

@type wkt() :: String.t()

Well-Known Text (WKT) is a text markup language for representing vector geometry objects.

Link to this section Functions

@spec empty?(t()) :: boolean()

Returns true if a geometry is empty.

examples

Examples

iex> Geometry.empty?(Point.new(1, 2))
false
iex> Geometry.empty?(Point.new())
true
iex> Geometry.empty?(LineString.new([]))
true
@spec from_ewkb(wkb()) ::
  {:ok, {t(), srid() | nil}} | {:error, Geometry.DecodeError.t()}

Returns an :ok tuple with the geometry from the given EWKB binary.

Otherwise returns an :error tuple.

If the given binary not an extended wkb/0 a nil for the SRID is returned.

examples

Examples

iex> "0020000001000012673FF00000000000004000000000000000"
...> |> Base.decode16!()
...> |> Geometry.from_ewkb()
{:ok, {%Point{coordinate: [1.0, 2.0]}, 4711}}

iex> "0101000080000000000000F03F00000000000000400000000000000840"
...> |> Base.decode16!()
...> |> Geometry.from_ewkb()
{:ok, {%PointZ{coordinate: [1.0, 2.0, 3.0]}, nil}}
@spec from_ewkb!(wkb()) :: {t(), srid() | nil}

The same as from_ewkb/1, but raises a Geometry.DecodeError exception if it fails.

@spec from_ewkt(wkt()) ::
  {:ok, {t(), srid() | nil}} | {:error, Geometry.DecodeError.t()}

Returns an :ok tuple with the geometry from the given EWKT string.

Otherwise returns an :error tuple.

If the given string not an extended wkt/0 a nil for the SRID is returned.

examples

Examples

iex> Geometry.from_ewkt("SRID=42;Point (1.1 2.2)")
{:ok, {%Point{coordinate: [1.1, 2.2]}, 42}}

iex> Geometry.from_ewkt("Point ZM (1 2 3 4)")
{:ok, {%PointZM{coordinate: [1, 2, 3, 4]}, nil}}

iex> Geometry.from_ewkt("Point XY (1 2 3 4)")
{:error, "expected Point data", "XY (1 2 3 4)", {1, 0}, 6}
{
  :error,
  %Geometry.DecodeError{
    from: :wkt,
    line: {1, 0},
    message: "expected Point data",
    offset: 6,
    rest: "XY (1 2 3 4)"
  }
}
@spec from_ewkt!(wkt()) :: {t(), srid() | nil}

The same as from_ewkt/1, but raises a Geometry.DecodeError exception if it fails.

Link to this function

from_geo_json(json, dim \\ :xy)

View Source
@spec from_geo_json(geo_json_term(), :xy | :xyz | :xym | :xyzm) ::
  {:ok, t() | Geometry.Feature.t() | Geometry.FeatureCollection.t()}
  | {:error, Geometry.DecodeError.t()}

Returns an :ok tuple with the geometry from the given GeoJSON term.

Otherwise returns an :error tuple.

The optional second argument specifies which type is expected. The possible values are :xy, :xym, xyz, and :xyzm. Defaults to :xy.

examples

Examples

iex> ~s({"type": "Point", "coordinates": [1, 2]})
iex> |> Jason.decode!()
iex> |> Geometry.from_geo_json()
{:ok, %Point{coordinate: [1, 2]}}

iex> ~s({"type": "Point", "coordinates": [1, 2, 3, 4]})
iex> |> Jason.decode!()
iex> |> Geometry.from_geo_json(:xyzm)
{:ok, %PointZM{coordinate: [1, 2, 3, 4]}}

iex> ~s({"type": "Dot", "coordinates": [1, 2]})
iex> |> Jason.decode!()
iex> |> Geometry.from_geo_json()
{
  :error,
  %Geometry.DecodeError{
    from: :geo_json,
    reason: [unknown_type: "Dot"]
  }
}
Link to this function

from_geo_json!(json, dim \\ :xy)

View Source
@spec from_geo_json!(geo_json_term(), type :: :xy | :xyz | :xym | :xyzm) ::
  t() | Geometry.Feature.t() | Geometry.FeatureCollection.t()

The same as from_geo_json/1, but raises a Geometry.DecodeError exception if it fails.

@spec from_wkb(wkb()) :: {:ok, t()} | {:error, Geometry.DecodeError.t()}

Returns an :ok tuple with the geometry from the given WKB binary.

Otherwise returns an :error tuple.

If the given binary is an extended wkb/0 the SRID is ignored.

examples

Examples

iex> "0020000001000012673FF00000000000004000000000000000"
...> |> Base.decode16!()
...> |> Geometry.from_wkb()
{:ok, %Point{coordinate: [1.0, 2.0]}}

iex> "0101000080000000000000F03F00000000000000400000000000000840"
...> |> Base.decode16!()
...> |> Geometry.from_wkb()
{:ok, %PointZ{coordinate: [1.0, 2.0, 3.0]}}

iex> "FF"
...> |> Base.decode16!()
...> |> Geometry.from_wkb()
{
  :error,
  %Geometry.DecodeError{
    from: :wkb,
    offset: 0,
    reason: [expected_endian: :flag],
    rest: <<255>>
  }
}
@spec from_wkb!(wkb()) :: t()

The same as from_wkb/1, but raises a Geometry.DecodeError exception if it fails.

@spec from_wkt(wkt()) :: {:ok, t()} | {:error, Geometry.DecodeError.t()}

Returns an :ok tuple with the geometry from the given WKT string.

Otherwise returns an :error tuple.

If the given string is an extended wkt/0 the SRID is ignored.

examples

Examples

iex> Geometry.from_wkt("SRID=42;Point (1.1 2.2)")
{:ok, %Point{coordinate: [1.1, 2.2]}}

iex> Geometry.from_wkt("Point ZM (1 2 3 4)")
{:ok, %PointZM{coordinate: [1, 2, 3, 4]}}

iex> Geometry.from_wkt("Point XY (1 2 3 4)")
{:error,  %Geometry.DecodeError{
  from: :wkt,
  line: {1, 0},
  message: "expected Point data",
  offset: 6,
  rest: "XY (1 2 3 4)"}
}
@spec from_wkt!(wkt()) :: t()

The same as from_wkt/1, but raises a Geometry.DecodeError exception if it fails.

Link to this function

to_ewkb(geometry, srid, endian \\ :ndr)

View Source
@spec to_ewkb(t(), srid() | nil, endian()) :: wkb()

Returns the EWKB representation of a geometry.

If the srid is nil a WKB is returned.

The optional :endian argument indicates whether :xdr big endian or :ndr little endian is returned. The default is :ndr.

examples

Examples

iex> PointZ.new(1, 2, 3)
...> |> Geometry.to_ewkb(3825)
...> |> Base.encode16()
"01010000A0F10E0000000000000000F03F00000000000000400000000000000840"

iex> PointZ.new(1, 2, 3)
...> |> Geometry.to_ewkb(nil)
...> |> Base.encode16()
"0101000080000000000000F03F00000000000000400000000000000840"

iex> Point.new(1, 2)
...> |> Geometry.to_ewkb(3825, :xdr)
...> |> Base.encode16()
"002000000100000EF13FF00000000000004000000000000000"
@spec to_ewkt(t(), srid() | nil) :: wkt()

Returns the EWKT representation of the given geometry.

If the srid is nil a WKT is returned.

examples

Examples

iex> Geometry.to_ewkt(PointZ.new(1.1, 2.2, 3.3), 4211)
"SRID=4211;Point Z (1.1 2.2 3.3)"

iex> Geometry.to_ewkt(LineString.new([Point.new(1, 2), Point.new(3, 4)]), 3825)
"SRID=3825;LineString (1 2, 3 4)"

iex> Geometry.to_ewkt(Point.new(1, 2), nil)
"Point (1 2)"
@spec to_geo_json(t()) :: geo_json_term()

Returns the GeoJSON term representation of a geometry.

examples

Examples

iex> Geometry.to_geo_json(PointZ.new(1.2, 3.4, 5.6))
%{"type" => "Point", "coordinates" => [1.2, 3.4, 5.6]}

iex> Geometry.to_geo_json(LineString.new([Point.new(1, 2), Point.new(3, 4)]))
%{"type" => "LineString", "coordinates" => [[1, 2], [3, 4]]}
Link to this function

to_wkb(geometry, endian \\ :ndr)

View Source
@spec to_wkb(t(), endian()) :: wkb()

Returns the WKB representation of a geometry.

The optional :endian argument indicates whether :xdr big endian or :ndr little endian is returned. The default is :ndr.

examples

Examples

iex> PointZ.new(1, 2, 3)
...> |> Geometry.to_wkb()
...> |> Base.encode16()
"0101000080000000000000F03F00000000000000400000000000000840"

iex> PointZ.new(1, 2, 3)
...> |> Geometry.to_wkb(:xdr)
...> |> Base.encode16()
"00800000013FF000000000000040000000000000004008000000000000"
@spec to_wkt(t()) :: wkt()

Returns the WKT representation of the given geometry.

examples

Examples

iex> Geometry.to_wkt(PointZ.new(1.1, 2.2, 3.3))
"Point Z (1.1 2.2 3.3)"

iex> Geometry.to_wkt(LineString.new([Point.new(1, 2), Point.new(3, 4)]))
"LineString (1 2, 3 4)"

iex> Geometry.to_wkt(Point.new(1, 2))
"Point (1 2)"