Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to calculate bounding box #149

Merged
merged 2 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is this comment necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's good to know what orientation of a ring I'll recieve if someone's confused.

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