Skip to content

Commit

Permalink
Add function to calculate bounding box (#149)
Browse files Browse the repository at this point in the history
* add bounding box function

* add test for bounding box
  • Loading branch information
zerefwayne authored Nov 11, 2020
1 parent 0bdcb00 commit 9cc8713
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/ogr/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ function envelope3d(geom::AbstractGeometry)
return envelope[]
end

"""
boundingbox(geom::AbstractGeometry)
Returns a bounding box polygon (CW) built from envelope coordinates
"""
function boundingbox(geom::AbstractGeometry)
coordinates = envelope(geom)
MinX, MaxX, MinY, MaxY = coordinates.MinX, coordinates.MaxX, coordinates.MinY, coordinates.MaxY
# creates a CW closed ring polygon
return createpolygon([[MaxY, MinX], [MaxY, MaxX], [MinY, MaxX], [MinY, MinX], [MaxY, MinX]])
end

"""
toWKB(geom::AbstractGeometry, order::OGRwkbByteOrder = GDAL.wkbNDR)
Expand Down
9 changes: 9 additions & 0 deletions test/test_cookbook_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,15 @@ end
end
end

@testset "Calculate Bounding Box of a Geometry" begin
wkt = "POLYGON ((1162440.5712740074 672081.4332727483, 1162440.5712740074 647105.5431482664, 1195279.2416228633 647105.5431482664, 1195279.2416228633 672081.4332727483, 1162440.5712740074 672081.4332727483))"
expected = AG.createpolygon([[672081.4332727483, 1162440.5712740074], [672081.4332727483, 1195279.2416228633], [647105.5431482664, 1195279.2416228633], [647105.5431482664, 1162440.5712740074], [672081.4332727483, 1162440.5712740074]])
AG.fromWKT(wkt) do polygon
calculated = AG.boundingbox(polygon)
@test AG.equals(expected, calculated)
end
end

@testset "Calculate the Area of a Geometry" begin
wkt = "POLYGON ((1162440.5712740074 672081.4332727483, 1162440.5712740074 647105.5431482664, 1195279.2416228633 647105.5431482664, 1195279.2416228633 672081.4332727483, 1162440.5712740074 672081.4332727483))"
AG.fromWKT(wkt) do poly
Expand Down

0 comments on commit 9cc8713

Please sign in to comment.