Geo.Turf.Classification (geo_turf v0.5.0)

Copy Markdown View Source

A collection of classification and boolean spatial functions.

Summary

Functions

Takes a reference Point and a list of Points and returns the point closest to the reference. Distance is calculated geodesically (Haversine).

Takes a Point and a Polygon (or MultiPolygon) and determines whether the Point lies inside the shape. Points on the boundary are considered inside unless :ignore_boundary is set to true. Empty polygons return false.

Filters a list of Points to those falling inside the given Polygon or MultiPolygon.

Functions

nearest_point(target, points, opts \\ [])

@spec nearest_point(Geo.Point.t(), [Geo.Point.t()], keyword()) :: Geo.Point.t() | nil

Takes a reference Point and a list of Points and returns the point closest to the reference. Distance is calculated geodesically (Haversine).

Returns nil if the list is empty.

Options

Examples

iex> target = %Geo.Point{coordinates: {28.965797, 41.010086}}
...> points = [
...>   %Geo.Point{coordinates: {28.973865, 41.011122}},
...>   %Geo.Point{coordinates: {28.948459, 41.024204}},
...>   %Geo.Point{coordinates: {28.938674, 41.013324}}
...> ]
...> Geo.Turf.Classification.nearest_point(target, points)
%Geo.Point{coordinates: {28.973865, 41.011122}}

iex> Geo.Turf.Classification.nearest_point(%Geo.Point{coordinates: {0, 0}}, [])
nil

point_in_polygon?(point, polygon, opts \\ [])

@spec point_in_polygon?(
  Geo.Point.t(),
  Geo.Polygon.t() | Geo.MultiPolygon.t(),
  keyword()
) :: boolean()

Takes a Point and a Polygon (or MultiPolygon) and determines whether the Point lies inside the shape. Points on the boundary are considered inside unless :ignore_boundary is set to true. Empty polygons return false.

Options

  • :ignore_boundary - when true, points on the polygon boundary return false. Defaults to false.

Examples

iex> poly = %Geo.Polygon{coordinates: [[{0, 0}, {0, 4}, {4, 4}, {4, 0}, {0, 0}]]}
...> Geo.Turf.Classification.point_in_polygon?(%Geo.Point{coordinates: {2, 2}}, poly)
true

iex> poly = %Geo.Polygon{coordinates: [[{0, 0}, {0, 4}, {4, 4}, {4, 0}, {0, 0}]]}
...> Geo.Turf.Classification.point_in_polygon?(%Geo.Point{coordinates: {5, 5}}, poly)
false

points_within_polygon(points, polygon)

@spec points_within_polygon(
  [Geo.Point.t()],
  Geo.Polygon.t() | Geo.MultiPolygon.t()
) :: [Geo.Point.t()]

Filters a list of Points to those falling inside the given Polygon or MultiPolygon.

Examples

iex> poly = %Geo.Polygon{coordinates: [[{0, 0}, {0, 4}, {4, 4}, {4, 0}, {0, 0}]]}
...> points = [%Geo.Point{coordinates: {2, 2}}, %Geo.Point{coordinates: {5, 5}}]
...> Geo.Turf.Classification.points_within_polygon(points, poly)
[%Geo.Point{coordinates: {2, 2}}]