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

Let the centroid (GDAL) function compute all centroids of a vector of GMTdataset. #1240

Merged
merged 4 commits into from
Aug 30, 2023
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
18 changes: 16 additions & 2 deletions src/gdal_extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ end
centroid(geom; gdataset=false)

### Parameters
* `geom`: the geometry. This can either be a GDAL AbstractGeometry or a GMTdataset (or vector of it), or a Matrix
* `geom`: the geometry. This can either be a GDAL AbstractGeometry or a GMTdataset (or vector of them), or a Matrix
* `gdataset`: Returns a GDAL IGeometry even when input is a GMTdataset or Matrix

Compute the geometry centroid.
Expand Down Expand Up @@ -544,9 +544,23 @@ function helper_geoms_run_fun(f::Function, D1, D2, retds::Bool=true; gdataset=fa
return (retds) ? gd2gmt(ig) : ig
end
function helper_geoms_run_fun(f::Function, D, retds::Bool=true; gdataset=false)
(gdataset) && (retds = false)
if (isa(D, Vector{<:GMTdataset}) && (f == centroid))
# Don't know if due to bad implementation or it's the way it is, the centroid function only Computes
# that of first polygon, so we have to loop over D[k]. Restrictred so far to the centroid function.
mat = Array{Float64,2}(undef, length(D), 2)
for k = 1:GMT.numel(D)
geom = helper_1geom(D[k])
ig = f(geom)
mat[k,1], mat[k,2] = Gdal.getx(ig, 0), Gdal.gety(ig, 0)
end
Dc = mat2ds(mat, geom=1, proj4=D[1].proj4, wkt=D[1].wkt, epsg=D[1].epsg)
!isempty(D[1].colnames) && (Dc.colnames = D[1].colnames[1:2])
Dc.comment = ["Centroids"]
return (retds) ? Dc : gmt2gd(Dc)
end
geom = helper_1geom(D)
ig = f(geom)
(gdataset) && (retds = false)
return (retds) ? gd2gmt(ig) : ig
end
function helper_geoms_run_fun(f::Function, D, ratio::Float64, holes::Bool=true; gdataset=false)
Expand Down
5 changes: 3 additions & 2 deletions src/gmtspatial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Parameters

Determine if one (or all, with +a) points of each feature in the input data are inside any of
the polygons given in the pfile.
- **Q** | **area** or **length** :: [Type => Str] `Arg = [[-|+]unit][+cmin[/max]][+h][+l][+p][+s[a|d]]`
- **Q** | **centroid** or **area** or **length** :: [Type => Str] `Arg = [[unit][+cmin[/max]][+h][+l][+p][+s[a|d]]`

Measure the area of all polygons or length of line segments.
- $(GMT._opt_R)
Expand Down Expand Up @@ -67,7 +67,7 @@ function gmtspatial(cmd0::String="", arg1=nothing, arg2 = nothing; kwargs...)

cmd, = parse_common_opts(d, "", [:R :V_params :b :d :e :f :g :h :i :o :yx])
cmd = parse_these_opts(cmd, d, [[:A :nn :nearest_neighbor], [:C :clip], [:E :handedness], [:F :force_polygons],
[:I :intersections], [:Q :area :length], [:W :extend]])
[:I :intersections], [:Q :centroid :area :length], [:W :extend]])
cmd = add_opt(d, cmd, "S", [:S :polygons :polyg_process], (buffer="b", holes="_h", dateline="_s"))

cmd, args, n, = add_opt(d, cmd, "D", [:D :duplicates], :data, Array{Any,1}([arg1, arg2]), (amax="+a", dmax="+d", cmax="+c", Cmax="+c", fact="+s", ortho="_+p"))
Expand All @@ -86,6 +86,7 @@ function gmtspatial(cmd0::String="", arg1=nothing, arg2 = nothing; kwargs...)
D = common_grd(d, cmd0, cmd, "gmtspatial ", arg1..., arg2, arg3, arg4) # Finish build cmd and run it
arg = arg1[end]
else
isa(arg1, GDtype) && isgeog(arg1) && !contains(cmd, " -f") && (cmd *= " -fg")
D = common_grd(d, cmd0, cmd, "gmtspatial ", arg1, arg2, arg3, arg4)
arg = arg2
end
Expand Down
13 changes: 7 additions & 6 deletions src/show_pretty_datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function _show(io::IO,
(D.proj4 != "") && println("PROJ: ", D.proj4)
(D.wkt != "") && println("WKT: ", D.wkt)
(D.header != "") && println("Header:\t", D.header)
(!isempty(D.comment)) && println("Comment:\t", D.comment)
println("")
end

Expand Down Expand Up @@ -167,7 +168,7 @@ function _show(io::IO,
alignment_anchor_regex = alignment_anchor_regex,
compact_printing = compact_printing,
crop = crop,
crop_num_lines_at_beginning = 2,
reserved_display_lines = 2,
ellipsis_line_skip = 3,
formatters = (_pretty_tables_general_formatter,),
header = (names_str, types_str),
Expand All @@ -176,11 +177,11 @@ function _show(io::IO,
highlighters = (_PRETTY_TABLES_HIGHLIGHTER,),
maximum_columns_width = maximum_columns_width,
newline_at_end = false,
nosubheader = !eltypes,
row_name_alignment = :r,
row_name_crayon = Crayon(),
row_name_column_title = string(rowlabel),
row_names = row_names,
show_subheader = !eltypes,
row_label_alignment = :r,
row_label_crayon = Crayon(),
row_label_column_title = string(rowlabel),
row_labels = row_names,
row_number_alignment = :r,
row_number_column_title = string(rowlabel),
show_row_number = show_row_number,
Expand Down
10 changes: 10 additions & 0 deletions src/utils_project.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,13 @@ function sideplot(; plane=:xz, vsize=8, depth=NaN, kw...)
_W, _H = (p == 'x') ? (H, zsize) : (p == 'y') ? (W, zsize) : (W, H)
@sprintf(" -Dg%.12g/%.12g+w%.12g/%.12g -p%c%.0f/%.0f %s %s", lims[1], lims[3], _W, _H, p, azim, elev, opt_X, opt_Y)
end

"""
isgeog(in)::Bool

Find if the input (a GMTgrid, GMTimage, GMTdadaset or string), if referenced, is in geographical coordinates.
"""
function isgeog(in)::Bool
prj = getproj(in, proj4=true)
(prj != "" && contains(prj, "=long") || contains(prj, "=lat"))
end
1 change: 1 addition & 0 deletions test/test_common_opts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
mat2ds(rand(3,3), segnan=true)
@test mat2ds([1. NaN 3; NaN 3 4; 5 6 NaN]).ds_bbox == [1.0, 5.0, 3.0, 6.0, 3.0, 4.0]
D[1].attrib = Dict("nome" => "a", "nome2" => "b");
info(D, attribs=true);
GMT.polygonlevels(D, ["a", "b"], [1,2], att="nome");
GMT.polygonlevels(D, ["a", "b"], [1,2], att="nome", nocase=1);
GMT.polygonlevels(D, ["a" "aa"; "b" "bb"], [1,2], att=["nome", "nome2"]);
Expand Down
2 changes: 2 additions & 0 deletions test/test_gd_ext.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
bf = buffer([0 0], 1)
@test bf.geom == Gdal.wkbPolygon
c = centroid(bf)
D = [mat2ds([0. 0; 1 1; 2 0; 0 0], geom=UInt32(3)), mat2ds([0. 0; 1 1; 2 0; 0 0], geom=UInt32(3))];
centroid(D); # Test multipolygons
#@test c.data ≈ [0. 0.]
bf2 = buffer([0.5 0], 1);
polyunion(bf, bf2);
Expand Down
2 changes: 1 addition & 1 deletion test/test_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
grdcontour(G, axis="a", color=cpt, pen="+c", fmt=:png, savefig="lixo")
D = grdcontour(G, cont=[-2,0,5], dump=true);

info(makecpt(C=:rainbow))
info(makecpt(C=:rainbow));

add2PSfile("Bla")
add2PSfile(["Bla", "Bla"])
Expand Down
Loading