Skip to content
Snippets Groups Projects
Commit 10d2c2aa authored by Marco Matthies's avatar Marco Matthies
Browse files

Don't implement Base.show for Matrix{Pixel}

Rename this function to `showlandscape()`.
parent 9585811e
Branches
No related tags found
No related merge requests found
......@@ -31,11 +31,14 @@ Pixel(landcover::LandCover, fieldid::Union{Missing,Int64}) =
Pixel(landcover, fieldid, Vector{Management}(), Vector{Int64}(), Vector{Int64}())
Pixel(landcover::LandCover) = Pixel(landcover, missing)
function Base.show(io::IO, ::MIME"text/plain", mat::T) where T <: AbstractMatrix{Pixel}
max_fieldid = maximum(skipmissing(map(x -> getfield(x, :fieldid), mat)); init=0)
showlandscape(mat::T) where T <: AbstractMatrix{Pixel} = showlandscape(stdout, mat)
function showlandscape(io::IO, mat::T) where T <: AbstractMatrix{Pixel}
println(io, "Matrix{Pixel}:")
println(io, " LandCover:")
nrow, ncol = size(mat)
max_fieldid = maximum(skipmissing(map(x -> getfield(x, :fieldid), mat)); init=0)
for i in axes(mat, 1)
print(io, " ")
for j in axes(mat, 2)
......@@ -49,12 +52,33 @@ function Base.show(io::IO, ::MIME"text/plain", mat::T) where T <: AbstractMatrix
end
println(io)
end
# print legend
legend = join(map(x -> "$(uppercase(first(string(x)))) = $x", instances(Persefone.LandCover)), ", ")
println(io, "Legend: ", legend)
soiltype_to_str(s::SoilType) = replace(string(s), r"^soiltype_" => "")
soiltype_to_char(s::SoilType) = uppercase(first(soiltype_to_str(s)))
println(io)
println(io, " Soil types:")
for i in axes(mat, 1)
print(io, " ")
for j in axes(mat, 2)
charid = soiltype_to_char(mat[i, j].soiltype)
fieldid = if ismissing(mat[i, j].fieldid)
repeat(" ", ndigits(max_fieldid))
else
@sprintf("%*s", ndigits(max_fieldid), mat[i, j].fieldid)
end
print(io, charid, fieldid, " ")
end
println(io)
end
# print legend
legend = join(map(x -> "$(soiltype_to_char(x)) = $(soiltype_to_str(x))", instances(Persefone.SoilType)), ", ")
println(io, "Legend: ", legend)
# print number of unique animals, events, territories
println(io)
nanimals = length(unique(reduce(vcat, map(x -> getfield(x, :animals), mat))))
nevents = length(unique(reduce(vcat, map(x -> getfield(x, :events), mat))))
nterritories = length(unique(reduce(vcat, map(x -> getfield(x, :territories), mat))))
......
......@@ -22,7 +22,7 @@ end
@testset "Utility functions" begin
model = inittestmodel()
iobuf = IOBuffer()
show(iobuf, MIME"text/plain"(), model.landscape)
Ps.showlandscape(iobuf, model.landscape)
@test length(take!(iobuf)) > 0
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment