View Source Geometry.Polygon (Geometry v0.3.1)

A polygon struct, representing a 2D polygon.

A none empty line-string requires at least one ring with four points.

Link to this section Summary

Functions

Returns true if the given Polygon is empty.

Creates a Polygon from the given coordinates.

Returns an :ok tuple with the Polygon from the given GeoJSON term. Otherwise returns an :error tuple.

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

Returns an :ok tuple with the Polygon from the given WKB string. Otherwise returns an :error tuple.

The same as from_wkb/2, but raises a Geometry.Error exception if it fails.

Returns an :ok tuple with the Polygon from the given WKT string. Otherwise returns an :error tuple.

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

Creates an empty Polygon.

Creates a Polygon from the given rings.

Returns the GeoJSON term of a Polygon.

Returns the WKB representation for a Polygon.

Returns the WKT representation for a Polygon. With option :srid an EWKT representation with the SRID is returned.

Link to this section Types

Specs

t() :: %Geometry.Polygon{rings: [Geometry.coordinates()]}

Link to this section Functions

Specs

empty?(t()) :: boolean()

Returns true if the given Polygon is empty.

Examples

iex> Polygon.empty?(Polygon.new())
true

iex> Polygon.empty?(
...>   Polygon.new([
...>     LineString.new([
...>       Point.new(35, 10),
...>       Point.new(45, 45),
...>       Point.new(10, 20),
...>       Point.new(35, 10)
...>     ])
...>   ])
...> )
false

Specs

from_coordinates([Geometry.coordinate()]) :: t()

Creates a Polygon from the given coordinates.

Examples

iex> Polygon.from_coordinates([
...>   [[1, 1], [2, 1], [2, 2], [1, 1]]
...> ])
%Polygon{
  rings: [
    [[1, 1], [2, 1], [2, 2], [1, 1]]
  ]
}

Specs

from_geo_json(Geometry.geo_json_term()) ::
  {:ok, t()} | Geometry.geo_json_error()

Returns an :ok tuple with the Polygon from the given GeoJSON term. Otherwise returns an :error tuple.

Examples

iex> ~s(
...>   {
...>     "type": "Polygon",
...>     "coordinates": [
...>       [[35, 10],
...>        [45, 45],
...>        [15, 40],
...>        [10, 20],
...>        [35, 10]]
...>     ]
...>   }
...> )
iex> |> Jason.decode!()
iex> |> Polygon.from_geo_json()
{:ok, %Polygon{
  rings: [
    [
      [35, 10],
      [45, 45],
      [15, 40],
      [10, 20],
      [35, 10]
    ]
  ]
}}

iex> ~s(
...>   {
...>     "type": "Polygon",
...>     "coordinates": [
...>       [[35, 10],
...>        [45, 45],
...>        [15, 40],
...>        [10, 20],
...>        [35, 10]],
...>       [[20, 30],
...>        [35, 35],
...>        [30, 20],
...>        [20, 30]]
...>     ]
...>   }
...> )
iex> |> Jason.decode!()
iex> |> Polygon.from_geo_json()
{:ok, %Polygon{
  rings: [[
    [35, 10],
    [45, 45],
    [15, 40],
    [10, 20],
    [35, 10]
  ], [
    [20, 30],
    [35, 35],
    [30, 20],
    [20, 30]
  ]]
}}

Specs

from_geo_json!(Geometry.geo_json_term()) :: t()

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

Link to this function

from_wkb(wkb, mode \\ :binary)

View Source

Specs

from_wkb(Geometry.wkb(), Geometry.mode()) ::
  {:ok, t() | {t(), Geometry.srid()}} | Geometry.wkb_error()

Returns an :ok tuple with the Polygon from the given WKB string. Otherwise returns an :error tuple.

If the geometry contains a SRID the id is added to the tuple.

The optional second argument determines if a :hex-string or a :binary input is expected. The default is :binary.

An example of a simpler geometry can be found in the description for the Geometry.Point.from_wkb/2 function.

Link to this function

from_wkb!(wkb, mode \\ :binary)

View Source

Specs

from_wkb!(Geometry.wkb(), Geometry.mode()) :: t() | {t(), Geometry.srid()}

The same as from_wkb/2, but raises a Geometry.Error exception if it fails.

Specs

from_wkt(Geometry.wkt()) ::
  {:ok, t() | {t(), Geometry.srid()}} | Geometry.wkt_error()

Returns an :ok tuple with the Polygon from the given WKT string. Otherwise returns an :error tuple.

If the geometry contains a SRID the id is added to the tuple.

Examples

iex> Polygon.from_wkt("
...>   POLYGON (
...>     (35 10, 45 45, 15 40, 10 20, 35 10),
...>     (20 30, 35 35, 30 20, 20 30)
...>   )
...> ")
{:ok,
 %Polygon{
   rings: [
     [
       [35, 10],
       [45, 45],
       [15, 40],
       [10, 20],
       [35, 10]
     ], [
       [20, 30],
       [35, 35],
       [30, 20],
       [20, 30]
     ]
   ]
}}

iex> "
...>   SRID=789;
...>   POLYGON (
...>     (35 10, 45 45, 15 40, 10 20, 35 10),
...>     (20 30, 35 35, 30 20, 20 30)
...>   )
...> "
iex> |> Polygon.from_wkt()
{:ok, {
  %Polygon{
    rings: [
      [
        [35, 10],
        [45, 45],
        [15, 40],
        [10, 20],
        [35, 10]
      ], [
        [20, 30],
        [35, 35],
        [30, 20],
        [20, 30]
      ]
    ]
  },
  789
}}

iex> Polygon.from_wkt("Polygon EMPTY")
{:ok, %Polygon{}}

Specs

from_wkt!(Geometry.wkt()) :: t() | {t(), Geometry.srid()}

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

Specs

new() :: t()

Creates an empty Polygon.

Examples

iex> Polygon.new()
%Polygon{rings: []}

Specs

new([Geometry.LineString.t()]) :: t()

Creates a Polygon from the given rings.

Examples

iex> Polygon.new([
...>   LineString.new([
...>     Point.new(35, 10),
...>     Point.new(45, 45),
...>     Point.new(10, 20),
...>     Point.new(35, 10)
...>   ]),
...>   LineString.new([
...>     Point.new(20, 30),
...>     Point.new(35, 35),
...>     Point.new(30, 20),
...>     Point.new(20, 30)
...>   ])
...> ])
%Polygon{
  rings: [
    [[35, 10], [45, 45], [10, 20], [35, 10]],
    [[20, 30], [35, 35], [30, 20], [20, 30]]
  ]
}

iex> Polygon.new()
%Polygon{}

Specs

to_geo_json(t()) :: Geometry.geo_json_term()

Returns the GeoJSON term of a Polygon.

Examples

iex> Polygon.to_geo_json(
...>   Polygon.new([
...>     LineString.new([
...>       Point.new(35, 10),
...>       Point.new(45, 45),
...>       Point.new(10, 20),
...>       Point.new(35, 10)
...>     ]),
...>     LineString.new([
...>       Point.new(20, 30),
...>       Point.new(35, 35),
...>       Point.new(30, 20),
...>       Point.new(20, 30)
...>     ])
...>   ])
...> )
%{
  "type" => "Polygon",
  "coordinates" => [
    [
      [35, 10],
      [45, 45],
      [10, 20],
      [35, 10]
    ], [
      [20, 30],
      [35, 35],
      [30, 20],
      [20, 30]
    ]
  ]
}
Link to this function

to_wkb(polygon, opts \\ [])

View Source

Specs

to_wkb(t(), opts) :: Geometry.wkb()
when opts: [
       endian: Geometry.endian(),
       srid: Geometry.srid(),
       mode: Geometry.mode()
     ]

Returns the WKB representation for a Polygon.

With option :srid an EWKB representation with the SRID is returned.

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

The :mode determines whether a hex-string or binary is returned. The default is :binary.

An example of a simpler geometry can be found in the description for the Geometry.Point.to_wkb/1 function.

Link to this function

to_wkt(polygon, opts \\ [])

View Source

Specs

to_wkt(t(), opts) :: Geometry.wkt() when opts: [{:srid, Geometry.srid()}]

Returns the WKT representation for a Polygon. With option :srid an EWKT representation with the SRID is returned.

Examples

iex> Polygon.to_wkt(Polygon.new())
"Polygon EMPTY"

iex> Polygon.to_wkt(Polygon.new(), srid: 1123)
"SRID=1123;Polygon EMPTY"

iex> Polygon.to_wkt(
...>   Polygon.new([
...>     LineString.new([
...>       Point.new(35, 10),
...>       Point.new(45, 45),
...>       Point.new(10, 20),
...>       Point.new(35, 10)
...>     ]),
...>     LineString.new([
...>       Point.new(20, 30),
...>       Point.new(35, 35),
...>       Point.new(30, 20),
...>       Point.new(20, 30)
...>     ])
...>   ])
...> )
"Polygon ((35 10, 45 45, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))"