Skip to content

Commit

Permalink
Let the centroid (GDAL) function compute all centroids of a vector of…
Browse files Browse the repository at this point in the history
… GMTdataset
  • Loading branch information
joa-quim committed Aug 30, 2023
1 parent 7030dbb commit 5f73513
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
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
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

0 comments on commit 5f73513

Please sign in to comment.