Geometry.MultiPolygon (Geometry v0.1.0) View Source

A set of polygons from type Geometry.Polygon

MultiPoint implements the protocols Enumerable and Collectable.

Examples

iex> Enum.map(
...>   MultiPolygon.new([
...>     Polygon.new([
...>       LineString.new([
...>         Point.new(11, 12),
...>         Point.new(11, 22),
...>         Point.new(31, 22),
...>         Point.new(11, 12)
...>       ]),
...>     ]),
...>     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)
...>       ])
...>     ])
...>   ]),
...>   fn polygon -> length(polygon) == 1 end
...> )
[true, false]

iex> Enum.into(
...>   [
...>     Polygon.new([
...>       LineString.new([
...>         Point.new(11, 12),
...>         Point.new(11, 22),
...>         Point.new(31, 22),
...>         Point.new(11, 12)
...>       ])
...>     ])
...>   ],
...>   MultiPolygon.new())
%MultiPolygon{
  polygons:
    MapSet.new([
      [
        [
          [11, 12],
          [11, 22],
          [31, 22],
          [11, 12]
        ]
      ]
    ])
}

Link to this section Summary

Functions

Returns true if the given MultiPolygon is empty.

Creates a MultiPolygon from the given coordinates.

Returns an :ok tuple with the MultiPolygon 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 MultiPolygon from the given WKB string. Otherwise returns an :error tuple.

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

Returns an :ok tuple with the MultiPolygon 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.

Checks if MultiPolygon contains point.

Creates an empty MultiPolygon.

Creates a MultiPolygon from the given Geometry.MultiPolygons.

Returns the number of elements in MultiPolygon.

Returns the GeoJSON term of a MultiPolygon.

Converts MultiPolygon to a list.

Returns the WKB representation for a MultiPolygon.

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

Link to this section Types

Specs

t() :: %Geometry.MultiPolygon{polygons: MapSet.t([Geometry.coordinates()])}

Link to this section Functions

Specs

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

Returns true if the given MultiPolygon is empty.

Examples

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

iex> MultiPolygon.empty?(
...>   MultiPolygon.new([
...>     Polygon.new([
...>         LineString.new([
...>           Point.new(1, 1),
...>           Point.new(1, 5),
...>           Point.new(5, 4),
...>           Point.new(1, 1)
...>        ])
...>     ])
...>   ])
...> )
false
Link to this function

from_coordinates(coordinates)

View Source

Specs

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

Creates a MultiPolygon from the given coordinates.

Examples

iex> MultiPolygon.from_coordinates([
...>   [
...>     [[6, 2], [8, 2], [8, 4], [6, 2]]
...>   ], [
...>     [[1, 1], [9, 1], [9, 8], [1, 1]],
...>     [[6, 2], [7, 2], [7, 3], [6, 2]]
...>   ]
...> ])
%MultiPolygon{
  polygons:
    MapSet.new([
      [
        [[6, 2], [8, 2], [8, 4], [6, 2]],
      ], [
        [[1, 1], [9, 1], [9, 8], [1, 1]],
        [[6, 2], [7, 2], [7, 3], [6, 2]]
      ]
    ])
}

Specs

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

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

Examples

iex> ~s(
...>   {
...>     "type": "MultiPolygon",
...>     "coordinates": [
...>       [
...>         [[6, 2], [8, 2], [8, 4], [6, 2]]
...>       ], [
...>         [[1, 1], [9, 1], [9, 8], [1, 1]],
...>         [[6, 2], [7, 2], [7, 3], [6, 2]]
...>       ]
...>     ]
...>   }
...> )
...> |> Jason.decode!()
...> |> MultiPolygon.from_geo_json()
{:ok,
 %MultiPolygon{
   polygons:
     MapSet.new([
       [
         [[1, 1], [9, 1], [9, 8], [1, 1]],
         [[6, 2], [7, 2], [7, 3], [6, 2]]
       ], [
         [[6, 2], [8, 2], [8, 4], [6, 2]]
       ]
     ])
 }}

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.

Specs

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

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

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

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

Specs

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

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

Specs

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

Returns an :ok tuple with the MultiPolygon 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> MultiPolygon.from_wkt("
...>   SRID=1234;MULTIPOLYGON (
...>     (
...>        (40 40, 20 45, 45 30, 40 40)
...>     ), (
...>        (20 35, 10 30, 10 10, 30 5, 45 20, 20 35),
...>        (30 20, 20 15, 20 25, 30 20)
...>     )
...>   )
...> ")
{:ok,
 %MultiPolygon{
   polygons:
     MapSet.new([
       [
         [
           [20, 35],
           [10, 30],
           [10, 10],
           [30, 5],
           [45, 20],
           [20, 35]
         ],
         [
           [30, 20],
           [20, 15],
           [20, 25],
           [30, 20]
         ]
       ],
       [
         [
           [40, 40],
           [20, 45],
           [45, 30],
           [40, 40]
         ]
       ]
     ])
 }, 1234}

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

Specs

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

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

Link to this function

member?(multi_polygon, polygon)

View Source

Specs

member?(t(), Geometry.Polygon.t()) :: boolean()

Checks if MultiPolygon contains point.

Examples

iex> MultiPolygon.member?(
...>   MultiPolygon.new([
...>     Polygon.new([
...>       LineString.new([
...>         Point.new(11, 12),
...>         Point.new(11, 22),
...>         Point.new(31, 22),
...>         Point.new(11, 12)
...>       ])
...>     ])
...>   ]),
...>   Polygon.new([
...>     LineString.new([
...>       Point.new(11, 12),
...>       Point.new(11, 22),
...>       Point.new(31, 22),
...>       Point.new(11, 12)
...>     ])
...>   ])
...> )
true

iex> MultiPolygon.member?(
...>   MultiPolygon.new([
...>     Polygon.new([
...>       LineString.new([
...>         Point.new(11, 12),
...>         Point.new(11, 22),
...>         Point.new(31, 22),
...>         Point.new(11, 12)
...>       ])
...>     ])
...>   ]),
...>   Polygon.new([
...>     LineString.new([
...>       Point.new(11, 12),
...>       Point.new(11, 22),
...>       Point.new(33, 22),
...>       Point.new(11, 12)
...>     ])
...>   ])
...> )
false

Specs

new() :: t()

Creates an empty MultiPolygon.

Examples

iex> MultiPolygon.new()
%MultiPolygon{polygons: MapSet.new()}

Specs

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

Creates a MultiPolygon from the given Geometry.MultiPolygons.

Examples

iex> MultiPolygon.new([
...>   Polygon.new([
...>     LineString.new([
...>       Point.new(6, 2),
...>       Point.new(8, 2),
...>       Point.new(8, 4),
...>       Point.new(6, 2)
...>     ]),
...>   ]),
...>   Polygon.new([
...>     LineString.new([
...>       Point.new(1, 1),
...>       Point.new(9, 1),
...>       Point.new(9, 8),
...>       Point.new(1, 1)
...>     ]),
...>     LineString.new([
...>       Point.new(6, 2),
...>       Point.new(7, 2),
...>       Point.new(7, 3),
...>       Point.new(6, 2)
...>     ])
...>   ])
...> ])
%MultiPolygon{
  polygons:
    MapSet.new([
      [
        [[1, 1], [9, 1], [9, 8], [1, 1]],
        [[6, 2], [7, 2], [7, 3], [6, 2]]
      ],
      [[[6, 2], [8, 2], [8, 4], [6, 2]]]
    ])
}

iex> MultiPolygon.new([])
%MultiPolygon{}

Specs

size(t()) :: non_neg_integer()

Returns the number of elements in MultiPolygon.

Examples

iex> MultiPolygon.size(
...>   MultiPolygon.new([
...>     Polygon.new([
...>       LineString.new([
...>         Point.new(11, 12),
...>         Point.new(11, 22),
...>         Point.new(31, 22),
...>         Point.new(11, 12)
...>       ])
...>     ])
...>   ])
...> )
1
Link to this function

to_geo_json(multi_polygon)

View Source

Specs

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

Returns the GeoJSON term of a MultiPolygon.

There are no guarantees about the order of polygons in the returned coordinates.

Examples

MultiPolygon.to_list(
  MultiPolygon.new([
    Polygon.new([
      LineString.new([
        Point.new(111, 112),
        Point.new(111, 122),
        Point.new(131, 122),
        Point.new(111, 112)
      ])
    ]),
    Polygon.new([
      LineString.new([
        Point.new(211, 212),
        Point.new(211, 222),
        Point.new(231, 222),
        Point.new(211, 212)
      ])
    ])
  ])
)
# =>
# %{
#   "type" => "MultiPolygon",
#   "coordinates" => [
#     [
#       [
#         [11, 12],
#         [11, 22],
#         [31, 22],
#         [11, 12]
#       ]
#     ], [
#       [
#         [21, 22],
#         [21, 22],
#         [21, 22],
#         [21, 22]
#       ]
#     ]
#   ]
# }

Specs

to_list(t()) :: [Geometry.Polygon.t()]

Converts MultiPolygon to a list.

Examples

iex> MultiPolygon.to_list(
...>   MultiPolygon.new([
...>     Polygon.new([
...>       LineString.new([
...>         Point.new(11, 12),
...>         Point.new(11, 22),
...>         Point.new(31, 22),
...>         Point.new(11, 12)
...>       ])
...>     ])
...>   ])
...> )
[
  [
    [
      [11, 12],
      [11, 22],
      [31, 22],
      [11, 12]
    ]
  ]
]
Link to this function

to_wkb(multi_polygon, opts \\ [])

View Source

Specs

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

Returns the WKB representation for a MultiPolygon.

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.

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(multi_polygon, opts \\ [])

View Source

Specs

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

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

There are no guarantees about the order of polygons in the returned WKT-string.

Examples

MultiPolygon.to_wkt(
  MultiPolygon.new([
    Polygon.new([
      LineStrinZM.new([
        Point.new(20, 35),
        Point.new(10, 30),
        Point.new(10, 10),
        Point.new(30, 5),
        Point.new(45, 20),
        Point.new(20, 35)
      ]),
      LineString.new([
        Point.new(30, 20),
        Point.new(20, 15),
        Point.new(20, 25),
        Point.new(30, 20)
      ])
    ]),
    Polygon.new([
      LineString.new([
        Point.new(40, 40),
        Point.new(20, 45),
        Point.new(45, 30),
        Point.new(40, 40)
      ])
    ])
  ])
)
# Returns a string without any \n or extra spaces (formatted just for readability):
# SRID=478;MultiPolygon (
#   (
#     (20 35, 10 30, 10 10, 30 5, 45 20, 20 35),
#     (30 20, 20 15, 20 25, 30 20)
#   ), (
#     (40 40, 20 45, 45 30, 40 40)
#   )
# )