Skip to content

Commit d45f90d

Browse files
authored
Add a gmt_centroid_area() fun to directly ccall the GMT C lib function. (#1785)
1 parent 98bec96 commit d45f90d

5 files changed

Lines changed: 31 additions & 8 deletions

File tree

src/libgmt.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,28 @@ function gmt_free_mem(API::Ptr{Cvoid}, mem)
427427
ccall((:gmt_free_func, libgmt), Cvoid, (Cstring, Ptr{Cvoid}, Bool, Cstring), GMT_, mem, true, "Julia")
428428
end
429429

430+
function gmt_centroid_area(API::Ptr{Cvoid}, D, geo::Int=0)::Matrix{Float64}
431+
if (isa(D, Vector))
432+
mat = Matrix{Float64}(undef, length(D), 3)
433+
for k in eachindex(D)
434+
t = gmt_centroid_area(API::Ptr{Cvoid}, D[k].data[:,1], D[k].data[:,2], geo)
435+
mat[k, 1], mat[k, 2], mat[k, 3] = t[1], t[2], t[3]
436+
end
437+
mat
438+
else
439+
gmt_centroid_area(API::Ptr{Cvoid}, D.data[:,1], D.data[:,2], geo)
440+
end
441+
end
442+
443+
function gmt_centroid_area(API::Ptr{Cvoid}, x::Vector{Float64}, y::Vector{Float64}, geo::Int=0)::Matrix{Float64}
444+
# geo = 0 for Cartesian, !=0 for geographic
445+
pos = [0.0 0.0 0.0]
446+
area = ccall((:gmt_centroid_area, libgmt), Cdouble, (Cstring, Ptr{Cdouble}, Ptr{Cdouble}, UInt64, Int32, Ptr{Cdouble}),
447+
GMT_Get_Ctrl(API), x, y, length(x), geo, pos)
448+
pos[3] = abs(area)
449+
return pos
450+
end
451+
430452
#=
431453
function get_common_R(API::Ptr{Cvoid})
432454
R = COMMON_R((false,false,false,false), false, 0, 0, 0, (0., 0., 0., 0., 0., 0.), (0., 0., 0., 0.), (0., 0.), map(UInt8, (string(repeat(" ",256))...,)))

src/psxy.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,19 @@ function plt_txt_attrib!(D::Vector{<:GMTdataset{T,N}}, d::Dict{Symbol, Any}, _cm
418418
((_ind = findfirst('=', s_val)) === nothing) && (_ind = findfirst(':', s_val))
419419
ind::Int = _ind # Because it fck insists _ind is a Any
420420
ts::String = s_val[ind+1:end]
421-
ct::GMTdataset = gmtspatial(D, centroid=true) # Texts will be plotted at the polygons centroids
421+
t = vec(make_attrtbl(D, att=ts)[1])
422422
if ((fnt = add_opt(d, "", "", [:font], (angle="+a", font=("+f", font)); del=false)) != "")
423423
(fnt[1] != '+') && (fnt = "+f" * fnt)
424424
delete!(d, :font)
425-
ct.text = vec(make_attrtbl(D, att=ts)[1])
426425
else
427426
nc::Int = round(Int, sqrt(length(D))) # A crude guess of the number of columns
428427
fnt = (nc < 5) ? "7p" : (nc < 9 ? "5p" : "4p") # A simple heuristic
429428
outline = fnt * ",black=~0.75p,white " # Apply the outline trick
430429
fnt = "+f"
431-
ct.text = outline .* vec(make_attrtbl(D, att=ts)[1])
430+
t = outline .* t
432431
end
432+
ct::GMTdataset{Float64,2} = mat2ds(gmt_centroid_area(G_API[1], D, Int(isgeog(D))))
433+
ct.text = t # Texts will be plotted at the polygons centroids
433434
(CTRL.pocket_call[1] === nothing) ? (CTRL.pocket_call[1] = ct) : (CTRL.pocket_call[2] = ct)
434435
append!(_cmd, ["pstext -R -J -F" * fnt * "+jMC"])
435436
return nothing

src/spatial_funs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function getbyattrib(D::Vector{<:GMTdataset}, ind_::Bool; kw...)::Vector{Int}
263263

264264
(ind = findfirst(atts .== "_region")) !== nothing && (lims = parse.(Float64, split(vals[ind][2:end-1], ", ")))
265265
(ind = findfirst(atts .== "_nps")) !== nothing && (nps = parse.(Float64, vals[ind]))
266-
(ind = findfirst(atts .== "_area")) !== nothing && (area = parse.(Float64, vals[ind]); areas::Vector{Float64} = gmtspatial(D, area=true).data[:,3])
266+
(ind = findfirst(atts .== "_area")) !== nothing && (area = parse.(Float64, vals[ind]); areas = gmt_centroid_area(G_API[1], D, Int(isgeog(D)))[:,3])
267267

268268
indices::Vector{Int} = Int[]
269269
ky = keys(D[1].attrib)

src/utils_project.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,11 @@ Project a geographical grid/image in the Lee Oblated Stereographic projection ce
171171
172172
173173
### Returns
174-
A grid or an image and optionally the coastlines ... or errors. Not many projections support the procedure
175-
implemented in this function.
176-
The working or not is controlled by PROJ's `+over` option https://proj.org/usage/projections.html#longitude-wrapping
174+
A grid or an image and optionally the coastlines.
177175
178176
### Example:
179-
G,cl = leepacific("@earth_relief_10m_g")
177+
G,cl = leepacific("@earth_relief_10m_g");
178+
grid = worldrectgrid(G);
180179
grdimage(G, shade=true, plot=(data=cl,), cmap=:geo, B=:none)
181180
plotgrid!(G, grid, show=true)
182181
"""

test/test_new_projs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
G = worldrectangular("@earth_relief_10m_g", pm=80);
99
G = worldrectangular("@earth_relief_10m_g", pm=-100);
1010
G,cl = worldrectangular("@earth_relief_10m_g", latlim=90, coast=true);
11+
G,cl = leepacific("@earth_relief_10m_g");
1112

1213
grid = worldrectgrid(G, annot_x=[-180,-150,0,150,180])
1314
plot([0 0]) # Just to have a PS for the next have where to append

0 commit comments

Comments
 (0)