GeometryBasics.jl

Basic geometry types.

This package aims to offer a standard set of geometry types that easily work with metadata, query frameworks on geometries and different memory layouts. The aim is to create a solid basis for graphics/plotting, finite element analysis, geo applications, and general geometry manipulations - while offering a Julian API that still allows performant C-interop.

This package is a replacement for the discontinued GeometryTypes.

Quick start

Create some points:

julia> using GeometryBasics
julia> p1 = Point(3, 1)2-element Point2{Int64} with indices SOneTo(2): 3 1
julia> p2 = Point(1, 3);
julia> p3 = Point(4, 4);

Geometries can carry metadata:

julia> poi = meta(p1, city="Abuja", rainfall=1221.2)2-element PointMeta{2, Int64, Point2{Int64}, (:city, :rainfall), Tuple{String, Float64}} with indices SOneTo(2):
 3
 1

Metadata is stored in a NamedTuple and can be retrieved as such:

julia> meta(poi)(city = "Abuja", rainfall = 1221.2)

Specific metadata attributes can be directly retrieved:

julia> poi.rainfall1221.2

To remove the metadata and keep only the geometry, use metafree:

julia> metafree(poi)2-element Point2{Int64} with indices SOneTo(2):
 3
 1

Geometries have predefined metatypes:

julia> multipoi = MultiPointMeta([p1], city="Abuja", rainfall=1221.2)1-element MultiPointMeta{Point2{Int64}, MultiPoint{2, Int64, Point2{Int64}, Vector{Point2{Int64}}}, (:city, :rainfall), Tuple{String, Float64}}:
 [3, 1]

Connect the points with lines:

julia> l1 = Line(p1, p2)Line([3, 1] => [1, 3])
julia> l2 = Line(p2, p3);

Connect the lines in a linestring:

julia> LineString([l1, l2])2-element LineString{2, Int64, Point2{Int64}, Vector{Line{2, Int64}}}:
 Line([3, 1] => [1, 3])
 Line([1, 3] => [4, 4])

Linestrings can also be constructed directly from points:

julia> LineString([p1, p2, p3])2-element LineString{2, Int64, Point2{Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point2{Int64}, Point2{Int64}}, TupleView{Tuple{Point2{Int64}, Point2{Int64}}, 2, 1, Vector{Point2{Int64}}}, false}}:
 Line([3, 1] => [1, 3])
 Line([1, 3] => [4, 4])

The same goes for polygons:

julia> Polygon(Point{2, Int}[(3, 1), (4, 4), (2, 4), (1, 2), (3, 1)])Polygon{2, Int64, Point2{Int64}, LineString{2, Int64, Point2{Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point2{Int64}, Point2{Int64}}, TupleView{Tuple{Point2{Int64}, Point2{Int64}}, 2, 1, Vector{Point2{Int64}}}, false}}, Vector{LineString{2, Int64, Point2{Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point2{Int64}, Point2{Int64}}, TupleView{Tuple{Point2{Int64}, Point2{Int64}}, 2, 1, Vector{Point2{Int64}}}, false}}}}(Line{2, Int64}[Line([3, 1] => [4, 4]), Line([4, 4] => [2, 4]), Line([2, 4] => [1, 2]), Line([1, 2] => [3, 1])], LineString{2, Int64, Point2{Int64}, Base.ReinterpretArray{Line{2, Int64}, 1, Tuple{Point2{Int64}, Point2{Int64}}, TupleView{Tuple{Point2{Int64}, Point2{Int64}}, 2, 1, Vector{Point2{Int64}}}, false}}[])

Create a rectangle placed at the origin with unit width and height:

julia> rect = Rect(Vec(0.0, 0.0), Vec(1.0, 1.0))Rect2{Float64}([0.0, 0.0], [1.0, 1.0])

Decompose the rectangle into two triangular faces:

julia> rect_faces = decompose(TriangleFace{Int}, rect)2-element Vector{TriangleFace{Int64}}:
 TriangleFace(1, 2, 4)
 TriangleFace(1, 4, 3)

Decompose the rectangle into four vertices:

julia> rect_vertices = decompose(Point{2, Float64}, rect)4-element Vector{Point2{Float64}}:
 [0.0, 0.0]
 [1.0, 0.0]
 [0.0, 1.0]
 [1.0, 1.0]

Combine the vertices and faces into a triangle mesh:

julia> mesh = Mesh(rect_vertices, rect_faces)Mesh{2, Float64, Triangle}:
 Triangle([0.0, 0.0], [1.0, 0.0], [1.0, 1.0])
 Triangle([0.0, 0.0], [1.0, 1.0], [0.0, 1.0])

Use GeometryBasics.mesh to get a mesh directly from a geometry:

julia> mesh = GeometryBasics.mesh(rect)Mesh{2, Float64, Triangle}:
 Triangle([0.0, 0.0], [1.0, 0.0], [1.0, 1.0])
 Triangle([0.0, 0.0], [1.0, 1.0], [0.0, 1.0])