Skip to content

Commit

Permalink
Merge pull request #1545 from GenericMappingTools/R-quadtre_names
Browse files Browse the repository at this point in the history
Let parse_R also accept (& parse) XYZ tiles names.
  • Loading branch information
joa-quim authored Sep 27, 2024
2 parents 95a6da2 + 23ca999 commit 4ff89cf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/GMT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const DEF_FIG_AXES3_BAK = " -Baf -Bza" # "" but for 3D views
const global DEF_FIG_AXES = [DEF_FIG_AXES_BAK] # This one may be be changed by theme()
const global DEF_FIG_AXES3 = [DEF_FIG_AXES3_BAK] # ""
const global CTRL = CTRLstruct(zeros(13), zeros(6), [true], [false],
[:arrows, :bubblechart, :basemap, :band, :clip, :coast, :colorbar, :hband, :hlines, :inset, :logo, :lines, :grdvector, :plot, :plot3, :quiver, :scatter, :scatter3, :stairs, :text, :vlines, :vband], fill(nothing, 6), ["","",""], ["","", "", " "], [""], ["",""], [false,true], [C_NULL], [Dict()])
[:arrows, :bubblechart, :basemap, :band, :clip, :coast, :colorbar, :hband, :hlines, :inset, :logo, :lines, :grdvector, :plot, :plot3, :quiver, :scatter, :scatter3, :stairs, :text, :vlines, :vband], fill(nothing, 6), ["","",""], ["","", "", " "], ["",""], ["",""], [false,true], [C_NULL], [Dict()])
const global CTRLshapes = CTRLstruct2([true], [true], [""]) # Used in sub-module Drawing
const prj4WGS84 = "+proj=longlat +datum=WGS84 +units=m +no_defs" # This is used in many places
const CPTaliases = [:C :color :cmap :colormap :colorscale]
Expand Down
16 changes: 10 additions & 6 deletions src/common_options.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,9 @@ function parse_R(d::Dict, cmd::String, O::Bool=false, del::Bool=true, RIr::Bool=

opt_R::String = ""
val, symb = find_in_dict(d, [:R :region :limits :region_llur :limits_llur :limits_diag :region_diag], del)
if (val !== nothing)
opt_R = build_opt_R(val, symb)
elseif (IamModern[1] && !RIr)
return cmd, ""
end

(val === nothing && IamModern[1] && !RIr) && return cmd, ""
opt_R = build_opt_R(val, symb)

opt_R = merge_R_and_xyzlims(d, opt_R) # Let a -R be partially changed by the use of optional xyzlim

Expand All @@ -178,7 +176,11 @@ function parse_R(d::Dict, cmd::String, O::Bool=false, del::Bool=true, RIr::Bool=
CTRL.limits[7:7+length(limits)-1] = limits # The plot limits
(opt_R != " -Rtight" && opt_R !== nothing && limits != zeros(4) && all(CTRL.limits[1:4] .== 0)) &&
(CTRL.limits[1:length(limits)] = limits) # And this makes data = plot limits, IF data is empty.
contains(opt_R, "+r") && (CTRL.limits[13] = 1.0)# To know that -R...+r was used.
if (count_chars(opt_R, ',') == 2) # A XYZ tile address (like "7829,6374,14")
opt_R = sprintf(" -R%.12g/%.12g/%.12g/%.12g", limits[1], limits[2], limits[3], limits[4])
elseif contains(opt_R, "+r")
CTRL.limits[13] = 1.0 # To know that -R...+r was used.
end
catch
CTRL.limits .= 0.0
IamModern[1] = bak
Expand Down Expand Up @@ -361,6 +363,8 @@ function opt_R2num(opt_R::String)::Vector{Float64}
for k = 2:lastindex(rs) limits[k] = parse(Float64, rs[k]) end
#if (isdiag) limits[2], limits[4] = limits[4], limits[2] end
# Don't know anymore how -R...+r limits should be stored in CTRL.limits
elseif (count_chars(opt_R, ',') == 2) # A XYZ tile address
limits = mosaic(scan_opt(opt_R, "-R"), mesh=true).bbox
elseif (opt_R != " -R" && opt_R != " -Rtight") # One of those complicated -R forms. Ask GMT the limits (but slow. It takes 0.2 s)

# If opt_R is not a grid's name, we are f.
Expand Down
5 changes: 2 additions & 3 deletions src/gmt_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,9 @@ function gmt(cmd::String, args...)
# is a Ref, so I'm forced to use 'pointer', which goes against the documents recommendation.
pLL = (LL != NULL) ? Ref([LL], 1) : pointer([NULL])

zero = 0
n_itemsP = pointer([zero])
n_itemsP = Ref{UInt32}(0)
XX = GMT_Encode_Options(G_API[1], g_module, n_argin, pLL, n_itemsP) # This call also changes LL
@GC.preserve zero n_items = unsafe_load(n_itemsP)
n_items = n_itemsP[]
if (XX == NULL && n_items > 65000) # Just got usage/synopsis option (if (n_items == UINT_MAX)) in C
(n_items > 65000) ? n_items = 0 : error("Failure to encode Julia command options")
end
Expand Down
31 changes: 16 additions & 15 deletions src/imgtiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ or XYZ encoding. An example of these codes is provided by the attributes of when
An important difference between the `address` option and the `lon & lat` option is that the `address` option also
set the zoom level, so here the ``zoom`` option means the extra zoom level added to that implied by ``address``.
A number highr than 3 is suspiciously large.
A number higher than 3 is suspiciously large.
# Example
```jldoctest
Expand All @@ -215,35 +215,35 @@ function mosaic(address::String; pt_radius=6378137.0, provider="", zoom::Int=0,
(length(s) != 3 && length(s) != 1) && throw(error("Wrong type of tile address: $address"))

# Functions for parsing the tiles XYZ code when given as ranges. E.g. "317-9" means 317 to 319 or 317+2 -> 315 to 319
function parse_LL(s_ind) # This version is for the form: 317-9 or 319-21
base, add = s_ind[1:ind[1]-1], s_ind[ind[1]+1:end]
function parse_LL(s_ind, ind) # This version is for the form: 317-9 or 319-21
base, add = s_ind[1:ind-1], s_ind[ind+1:end]
first = parse(Int, base)
t = (length(add) == 1) ? base[1:end-1] * add : base[1:end-2] * add
last = parse(Int, t)
return first, last
end
function parse_CC(s_ind) # This version is for the form: 317+2
base, add = parse(Int, s_ind[1:ind[1]-1]), parse(Int, s_ind[ind[1]+1:end])
function parse_CC(s_ind, ind) # This version is for the form: 317+2
base, add = parse(Int, s_ind[1:ind-1]), parse(Int, s_ind[ind+1:end])
return base-add, base+add
end

if (length(s) == 3)
if ((ind = findfirst("-", s[1])) !== nothing) xf, xl = parse_LL(s[1])
elseif ((ind = findfirst("+", s[1])) !== nothing) xf, xl = parse_CC(s[1])
if ((ind = findfirst('-', s[1])) !== nothing) xf, xl = parse_LL(s[1], ind)
elseif ((ind = findfirst('+', s[1])) !== nothing) xf, xl = parse_CC(s[1], ind)
else xf = parse(Int, s[1]); xl = xf
end

if ((ind = findfirst("-", s[2])) !== nothing) yf, yl = parse_LL(s[2]) .+ 1
elseif ((ind = findfirst("+", s[2])) !== nothing) yf, yl = parse_CC(s[2]) .+ 1
if ((ind = findfirst('-', s[2])) !== nothing) yf, yl = parse_LL(s[2], ind) .+ 1
elseif ((ind = findfirst('+', s[2])) !== nothing) yf, yl = parse_CC(s[2], ind) .+ 1
else yf = parse(Int, s[2]) + 1; yl = yf
end
zoomL = parse(Int, s[3])
if (xf != xl || yf != yl)
limsLL = quadkey([xf, yf, zoomL]) # Each comes as 2x2 with [xmin ymin; xmax ymax]
limsUR = quadkey([xl, yl, zoomL])
lims = [min(limsLL[1], limsUR[1]), max(limsLL[2], limsUR[2]), min(limsLL[3], limsUR[3]), max(limsLL[4], limsUR[4])]
lims::Vector{Float64} = [min(limsLL[1], limsUR[1]), max(limsLL[2], limsUR[2]), min(limsLL[3], limsUR[3]), max(limsLL[4], limsUR[4])]
else
lims = quadkey([xf, yf, zoomL])
lims = vec(quadkey([xf, yf, zoomL]))
end

#xyz = parse.(Int, s)
Expand Down Expand Up @@ -428,15 +428,16 @@ function mosaic(lon::Vector{<:Float64}, lat::Vector{<:Float64}; pt_radius=637813

xx = collect(linspace(xm[1], xm[2], size(img,1)+1))
yy = collect(linspace(ym[1], ym[2], size(img,2)+1))
I = mat2img(img, x=xx, y=yy, proj4="+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=$pt_radius +b=$pt_radius +units=m +no_defs", layout="TRBa", is_transposed=true)
I::GMTimage = mat2img(img, x=xx, y=yy, proj4="+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=$pt_radius +b=$pt_radius +units=m +no_defs", layout="TRBa", is_transposed=true)
@assert typeof(I) === GMTimage{UInt8, 3} # Fck compiler. Only this convinced it to make I type stable

if (inMerc && isExact) # Cut to the exact required limits
mat::Matrix{Float64} = mapproject([lon[1] lat_orig[1]; lon[2] lat_orig[2]], J=I.proj4).data
I = grdcut(I, R=(mat[1,1], mat[2,1], mat[1,2], mat[2,2]))
I = grdcut(I, R=(mat[1,1], mat[2,1], mat[1,2], mat[2,2]))::GMTimage
elseif (!inMerc) # That is, if project to Geogs
gdwopts = ["-t_srs","+proj=latlong +datum=WGS84", "-r","cubic"]
isExact && append!(gdwopts, ["-te"], ["$(lon[1])"], ["$(lat_orig[1])"], ["$(lon[2])"], ["$(lat_orig[2])"])
I = gdalwarp(I, gdwopts)
I = gdalwarp(I, gdwopts)::GMTimage
end

return I
Expand Down Expand Up @@ -594,7 +595,7 @@ function quadbounds(quadtree::Matrix{String}, quadkey=['0' '1'; '2' '3']; geog=t
flatness = 0.0

if isa(quadtree, AbstractString) # A single quadtree
lims, zoomL = getQuadLims(quadtree, quadkey, "")
lims, zoomL = getQuadLims(quadtree, quaEdkey, "")
tiles_bb = lims[[1, 2, 4, 3]] # In case idiot choice of 2 argouts
else # Several in a cell array
tiles_bb = zeros(length(quadtree), 4)
Expand Down
4 changes: 2 additions & 2 deletions src/libgmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ function GMT_Report(API, vlevel::Integer, txt)
ccall((:GMT_Report, libgmt), Cvoid, (Cstring, Cint, Ptr{UInt8}), API, vlevel, txt)
end =#

function GMT_Encode_Options(V_API::Ptr{Cvoid}, _module, n_argin::Int, head::Ref{Ptr{GMT_OPTION}}, n::Ptr{Int})
function GMT_Encode_Options(V_API::Ptr{Cvoid}, _module, n_argin::Int, head::Ref{Ptr{GMT_OPTION}}, n)
ccall((:GMT_Encode_Options, libgmt), Ptr{GMT_RESOURCE}, (Cstring, Ptr{UInt8}, Int32, Ref{Ptr{GMT_OPTION}},
Ptr{UInt32}), V_API, _module, n_argin, head, n)
end
function GMT_Encode_Options(V_API::Ptr{Cvoid}, _module, n_argin::Int, head, n::Ptr{Int})
function GMT_Encode_Options(V_API::Ptr{Cvoid}, _module, n_argin::Int, head, n)
ccall((:GMT_Encode_Options, libgmt), Ptr{GMT_RESOURCE}, (Cstring, Ptr{UInt8}, Int32, Ptr{Ptr{Cvoid}}, Ptr{UInt32}),
V_API, _module, n_argin, head, n)
end
Expand Down

0 comments on commit 4ff89cf

Please sign in to comment.