Skip to content

Commit

Permalink
Merge pull request #1241 from GenericMappingTools/better-info
Browse files Browse the repository at this point in the history
Improve the info() function.
  • Loading branch information
joa-quim authored Aug 31, 2023
2 parents 1ae20a9 + 6d4847a commit 6ac2fd4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
46 changes: 35 additions & 11 deletions src/gmt_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,7 @@ function print_crs(GID, saysomething=false)
(GID.proj4 != "") && println("PROJ: ", GID.proj4)
(GID.wkt != "") && println("WKT: ", GID.wkt)
(GID.epsg != 0) && println("EPSG: ", GID.epsg)
return nothing
end

"""
Expand All @@ -1354,15 +1355,21 @@ Shows information about the `GI` grid or image that includes dimensional and, if
- `showdata`: Boolean that controls if a small array subset is printed or not.
- `crs`: Boolean that if `true` only prints the referencing information.
### `info(D::GDtype; crs::Bool=false, attribs=false)`
### `info(D::GDtype; crs::Bool=false, attribs=false, att="")`
Shows information about the `D` GMTdataset (or vector of them).
- `crs`: Boolean that if `true` only prints the referencing information.
- `attribs`: In case the dataset has attributes, like they do when resulting from reading a shape file, use
this parameter to print only the attribute table. A setting of `attribs=true` will print the entire attributes
table. Give a positive number, _e.g._ `attribs=5` to show only the first 5 attributes. A negative number prints
the last n attribs. A vector range, `attribs=5:9` is also accepted.
the last n attribs. A vector range, `attribs=5:9` is also accepted.
- `att`: Name of one attribute. Returns a string vector with the values of the attribute passed into this option.
Example, ``attn = info(D, att="NAME")`` returns all values of the attribute ``NAME``.
### `info(any)`
Runs ``show(stdout, "text/plain", any)`` which prints all elements of `any`
"""
function info(GI::GItype, showdata::Bool=true; crs::Bool=false)
crs && return print_crs(GI)
Expand All @@ -1378,43 +1385,60 @@ function info(GI::GItype, showdata::Bool=true; crs::Bool=false)
showdata && (isa(GI, GMTgrid) ? display(GI.z) : display(GI.image))
end

function info(D::GDtype; crs::Bool=false, attribs=false)
crs && return isa(D, Vector) ? print_crs(D[1]) : print_crs(D)
(attribs == false) && return isempty(D[1].attrib) ? show(D[1]) : show(D[1], attrib_table=make_atrtbl(D, false)[1])
function info(D::GDtype; crs::Bool=false, attribs=false, att::StrSymb="")
crs && return isa(D, Vector) ? print_crs(D[1]) : print_crs(D) # Report projection info and return

(attribs == false) && (_D = isa(D, Vector) ? D[1] : D)
(attribs == false && att == "") && return isempty(_D.attrib) ? show(_D) : show(_D, attrib_table=make_attrtbl(D, false)[1])

# OK, here we are dealing with printing the attribs, or returning a column with the values of one attrib.
(attribs == false && att != "") && (attribs = true)
n_att = (attribs == true) ? 0 : attribs
!isa(n_att, Int) && !isa(n_att, AbstractVector) && error("'attribs' can only be an integer or an AbstractVector.")

tit = "Attribute table (Dict{String, String})"
if (!isa(D, Vector))
pretty_table(reshape(vec(string.(values(D.attrib))), 1, length(D.attrib)), header=vec(string.(keys(D.attrib))), title=tit)
else
att_tbl, att_names = make_atrtbl(D, true)
# Do differently depending on: plot whole attrib table or return the values of one attribute.
if (att == "") att_tbl, att_names = make_attrtbl(D, true)
else att_tbl = make_attrtbl(D, att=att)
end
if (isa(n_att, Int))
tbl = (n_att > 0) ? att_tbl[1:n_att,:] : (n_att < 0) ? att_tbl[size(att_tbl,1)+n_att+1:end,:] : att_tbl
else
tbl = att_tbl[n_att, :]
end
(att != "") && return tbl # If only one attribute was requested we end here.
pretty_table(tbl; header=att_names, alignment=:l, show_row_number=true, title=tit, crop=:horizontal)
end
return nothing
end
info(any) = show(stdout, "text/plain", any) # Show the f all of whatever 'any' is

# ---------------------------------------------------------------------------------------------------
Base.:show(io::IO, G::GMTgrid) = info(G, false)
Base.:display(G::GMTgrid) = show(G) # Otherwise by default it only displays the numbers
Base.:show(io::IO, I::GMTimage) = info(I, false)
Base.:display(I::GMTimage) = show(I)

function make_atrtbl(D::Vector{<:GMTdataset}, names::Bool=false)
# Create a string matrix with the dataset attributes. 'names', if true, returns also a string
# vector with attribute names.
"""
att_tbl, att_names = make_attrtbl(D::GDtype, names::Bool=false; att::StrSymb="")
Create a string matrix with the dataset attributes. 'names', if true, returns also a string
vector with attribute names. 'att', if == to one atribute, returns only that column of the att table.
"""
function make_attrtbl(D::GDtype, names::Bool=false; att::StrSymb="")
!isa(D, Vector) && (att_tbl = reshape(vec(string.(values(D.attrib))),1,length(D.attrib)))
!isa(D, Vector) && return att_tbl

len_D, len_att = length(D), length(D[1].attrib)
att_tbl = Matrix{String}(undef, len_D, len_att)
for k = 1:len_D
att_tbl[k, :] = reshape(vec(string.(values(D[k].attrib))), 1, len_att)
end
att_names = names ? vec(string.(keys(D[1].attrib))) : String[]
att_names = (names || att != "") ? vec(string.(keys(D[1].attrib))) : String[]
(att != "") && ((ind = findfirst(string(att) .== att_names)) !== nothing) && return att_tbl[:,ind]
att_tbl, att_names
end

Expand All @@ -1424,7 +1448,7 @@ function Base.:show(io::IO, ::MIME"text/plain", D::Vector{<:GMTdataset})
(length(D) == 0) && return

println("Showing first segment. To see other segments just type its element number. e.g. D[2]\n")
isempty(D[1].attrib) ? show(D[1]) : show(D[1], attrib_table=make_atrtbl(D, false)[1])
isempty(D[1].attrib) ? show(D[1]) : show(D[1], attrib_table=make_attrtbl(D, false)[1])
end
Base.:show(io::IO, ::MIME"text/plain", D::GMTdataset) = show(D)
Base.:display(D::GMTdataset) = show(D) # Otherwise the default prints nothing when text only (data == [])
Expand Down
2 changes: 1 addition & 1 deletion src/gmtreadwrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function gmtread(_fname::String; kwargs...)
(opt_T == "" && opt_bi != "") && (opt_T = " -Td") # If asked to read binary, must be a 'data' file.

if (opt_T == "")
((opt_T = guess_T_from_ext(fname)) == "") && error("Must select one input data type (grid, image, dataset, cmap or ps)")
((opt_T = guess_T_from_ext(fname)) == "") && error("Must select the input data type (grid, image, dataset, ogr, cmap or ps)")
(opt_T == " -Tg" && haskey(d, :ignore_grd)) && return nothing # contourf uses this
if (opt_T == " -To" && fname[1] == '@') # Because GMT ogrread does not know the '@' mechanism.
fname = joinpath(readlines(`gmt --show-userdir`)[1], "cache", fname[2:end])
Expand Down
1 change: 1 addition & 0 deletions src/show_pretty_datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function _show(io::IO,
end
end
(~isempty(D.bbox)) && println("BoundingBox: ", D.bbox)
(~isempty(D.bbox) && D.bbox != D.ds_bbox) && println("Global BoundingBox: ", D.ds_bbox)
(D.proj4 != "") && println("PROJ: ", D.proj4)
(D.wkt != "") && println("WKT: ", D.wkt)
(D.header != "") && println("Header:\t", D.header)
Expand Down
2 changes: 1 addition & 1 deletion src/utils_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1961,7 +1961,7 @@ function getbyattrib(D::Vector{<:GMTdataset}, ind_::Bool; kw...)::Vector{Int}
indices::Vector{Int} = Int[]
ky = keys(D[1].attrib)
for n = 1:numel(atts)
((ind = findfirst(ky .== atts[n])) === nothing) && continue#return Int[]
((ind = findfirst(ky .== atts[n])) === nothing) && continue
tf = fill(false, length(D))
for k = 1:length(D)
(!isempty(D[k].attrib) && (D[k].attrib[atts[n]] == vals[n])) && (tf[k] = true)
Expand Down

0 comments on commit 6ac2fd4

Please sign in to comment.