Skip to content

Commit 1361fc5

Browse files
authored
Shelter helper_run_GDAL_fun() againste multiple compiles (#1939)
1 parent 8bb3326 commit 1361fc5

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

src/gdal/gdal_tools.jl

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ And it may also contain a `options=...` argument that is equivalent to the `opti
2626
A GMT grid or Image, or a GDAL dataset (or nothing if file was writen on disk).
2727
"""
2828
function gdaltranslate(indata, opts=String[]; dest="/vsimem/tmp", kwargs...)
29-
helper_run_GDAL_fun(gdaltranslate, indata, dest, opts, "", kwargs...)
29+
helper_run_GDAL_fun(gdaltranslate, indata, dest, opts, "", KW(kwargs))
3030
end
3131
# ---------------------------------------------------------------------------------------------------
3232

@@ -50,7 +50,7 @@ Image/Grid reprojection and warping function.
5050
A GMT grid or Image, or a GDAL dataset (or nothing if file was writen on disk).
5151
"""
5252
function gdalwarp(indata, opts=String[]; dest="/vsimem/tmp", kwargs...)
53-
helper_run_GDAL_fun(gdalwarp, indata, dest, opts, "", kwargs...)
53+
helper_run_GDAL_fun(gdalwarp, indata, dest, opts, "", KW(kwargs))
5454
end
5555

5656
# ---------------------------------------------------------------------------------------------------
@@ -75,7 +75,7 @@ The modified input `data`
7575
function fillnodata!(indata::GItype; nodata=nothing, kwargs...)
7676
d = KW(kwargs)
7777
d[:nodata] = (nodata !== nothing) ? nodata : isa(indata, GItype) ? indata.nodata : NaN
78-
helper_run_GDAL_fun(gdalfillnodata!, indata, "", String[], "", d...)
78+
helper_run_GDAL_fun(gdalfillnodata!, indata, "", String[], "", d)
7979
end
8080

8181
# ---------------------------------------------------------------------------------------------------
@@ -130,6 +130,10 @@ Its natural use is to digitize masks images.
130130
function polygonize(data::GItype; gdataset=nothing, kwargs...)
131131
d = KW(kwargs)
132132
(gdataset === nothing) && (d[:gdataset] = true)
133+
is_gdataset = (gdataset === nothing)
134+
_polygonize(data, is_gdataset, d)
135+
end
136+
function _polygonize(data::GItype, is_gdataset::Bool, d::Dict{Symbol, Any})
133137
m_per_deg = 2pi * 6371000 / 360; m_per_deg_2 = m_per_deg^2
134138
_isgeog = isgeog(data)
135139
min_area::Float64 = ((val = find_in_dict(d, [:min_area :minarea])[1]) !== nothing) ? Float64(val) : 0.0
@@ -151,9 +155,9 @@ function polygonize(data::GItype; gdataset=nothing, kwargs...)
151155
_isgeog && (POSTMAN[]["polygon_isgeog"] = "1")
152156
end
153157

154-
o = helper_run_GDAL_fun(gdalpolygonize, data, "", String[], "", d...)
158+
o = helper_run_GDAL_fun(gdalpolygonize, data, "", String[], "", d)
155159

156-
if (gdataset === nothing) # Should be doing this for GDAL objects too but need to learn how to.
160+
if (is_gdataset) # Should be doing this for GDAL objects too but need to learn how to.
157161
POSTMAN[]["polygonize"] = "y" # To inform gd2gmt() that it should check if last Di is the whole area.
158162
(find_in_dict(d, [:sort])[1] !== nothing) && (POSTMAN[]["sort_polygons"] = "y")
159163
if ((val = find_in_dict(d, [:simplify])[1]) !== nothing)
@@ -207,7 +211,7 @@ function bwareaopen(I::Union{GMTimage{UInt8,2}, GMTimage{Bool,2}}; keepwhites::B
207211
d = KW(kwargs)
208212
# When the memory layout is column major, the gmt2gd step has to make a data copy, so we better forget the
209213
# possibility an insitu gdalsievefilter operation and do a copy right away when layout is row major.
210-
Ic::GMTimage{UInt8,2} = helper_run_GDAL_fun(gdalsievefilter, I.layout[2] == 'C' ? I : deepcopy(I), "", String[], "", d...)
214+
Ic::GMTimage{UInt8,2} = helper_run_GDAL_fun(gdalsievefilter, I.layout[2] == 'C' ? I : deepcopy(I), "", String[], "", d)
211215
(!keepwhites && !keepblacks) && return Ic
212216
white = (I.range[6] == 1) ? UInt8(1) : UInt8(255)
213217
black = UInt8(0)
@@ -246,9 +250,12 @@ Tools to analyze and visualize DEMs.
246250
A GMT grid or Image, or a GDAL dataset (or nothing if file was writen on disk).
247251
"""
248252
function gdaldem(indata, method::String, opts::Vector{String}=String[]; dest="/vsimem/tmp", kwargs...)
253+
d = KW(kwargs)
254+
_gdaldem(indata, method, opts, dest::String, d)
255+
end
256+
function _gdaldem(indata, method::String, opts::Vector{String}, dest::String, d::Dict{Symbol, Any})
249257
opts = gdal_opts2vec(opts) # Guarantied to return a Vector{String}
250258
if (method == "hillshade") # So far the only method that accept kwarg options
251-
d = KW(kwargs)
252259
band = ((val = find_in_dict(d, [:band])[1]) !== nothing) ? string(val)::String : "1"
253260
append!(opts, ["-compute_edges", "-b", band])
254261
if ((val = find_in_dict(d, [:scale])[1]) === nothing)
@@ -268,9 +275,9 @@ function gdaldem(indata, method::String, opts::Vector{String}=String[]; dest="/v
268275
((val = find_in_dict(d, [:alg])[1]) !== nothing) && append!(opts, ["-alg", string(val)])
269276
((val = find_in_dict(d, [:Horn])[1]) !== nothing) && append!(opts, ["-alg", "Horn"])
270277
((val = find_in_dict(d, [:Zeven :Zevenbergen])[1]) !== nothing) && append!(opts, ["-alg", "ZevenbergenThorne"])
271-
helper_run_GDAL_fun(gdaldem, indata, dest, opts, method, d...)
278+
helper_run_GDAL_fun(gdaldem, indata, dest, opts, method, d)
272279
else
273-
helper_run_GDAL_fun(gdaldem, indata, dest, opts, method, kwargs...)
280+
helper_run_GDAL_fun(gdaldem, indata, dest, opts, method, d)
274281
end
275282
end
276283

@@ -291,18 +298,21 @@ A GMTgrid or a GDAL dataset (or nothing if file was writen on disk). To force th
291298
dataset use the option `gdataset=true`.
292299
"""
293300
function gdalgrid(indata, opts::Union{String, Vector{String}}=""; dest="/vsimem/tmp", method::Union{AbstractString, Symbol}="", kwargs...)
301+
_gdalgrid(indata, opts, dest, string(method), KW(kwargs))
302+
end
303+
function _gdalgrid(indata, opts::Union{String, Vector{String}}, dest::String, method::String, d::Dict{Symbol, Any})
294304
if (method == "")
295305
_mtd = "-a invdist:nodata=NaN"
296306
else
297-
_mtd = isa(method, Symbol) ? string(method) : method
307+
_mtd = method
298308
(!startswith(_mtd, "invdist") && !startswith(_mtd, "invdistnn") && !startswith(_mtd, "average") && !startswith(_mtd, "nearest") && !startswith(_mtd, "linear") && !startswith(_mtd, "minimum") && !startswith(_mtd, "maximum") && !startswith(_mtd, "range") && !startswith(_mtd, "count") && !startswith(_mtd, "average_distance") && !startswith(_mtd, "average_distance_pts")) && error("Bad interpolation algorithm $_mtd")
299309
_mtd = "-a " * _mtd
300310
!occursin("nodata", _mtd) && (_mtd *= ":nodata=NaN")
301311
end
302312
_opts = isa(opts, Vector) ? join(opts, " ") : opts # Let's make a string to reduce confusion
303313
!occursin("-ot", _opts) && (_opts *= " -ot Float32")
304314
_mtd *= " " * _opts
305-
helper_run_GDAL_fun(gdalgrid, indata, dest, _mtd, "", kwargs...)
315+
helper_run_GDAL_fun(gdalgrid, indata, dest, _mtd, "", d)
306316
end
307317

308318
# ---------------------------------------------------------------------------------------------------
@@ -321,19 +331,19 @@ end
321331
A GMT dataset, or a GDAL dataset (or nothing if file was writen on disk).
322332
"""
323333
function gdalvectortranslate(indata, opts=String[]; dest="/vsimem/tmp", kwargs...)
324-
helper_run_GDAL_fun(gdalvectortranslate, indata, dest, opts, "", kwargs...)
334+
helper_run_GDAL_fun(gdalvectortranslate, indata, dest, opts, "", KW(kwargs))
325335
end
326336

327-
function helper_run_GDAL_fun(f::Function, indata, dest::String, opts, method::String="", kwargs...)::Union{GItype, GDtype, Gdal.AbstractDataset, Nothing}
337+
function helper_run_GDAL_fun(f::Function, indata, dest::String, opts, method::String, d::Dict{Symbol, Any})::Union{GItype, GDtype, Gdal.AbstractDataset, Nothing}
328338
ressurectGDAL() # Another black-hole plug attempt.
329339
opts = gdal_opts2vec(opts) # Guarantied to return a Vector{String}
330-
d = KW(kwargs)
340+
#d = KW(kwargs)
331341
opts_vs, got_GMT_opts = GMT_opts_to_GDAL(f, opts, d)
332342
_helper_run_GDAL_fun(f, indata, dest, opts_vs, method, got_GMT_opts, d)
333343
end
334344

335345
# ---------------------------------------------------------------------------------------------------
336-
function _helper_run_GDAL_fun(f::Function, indata, dest::String, opts::Vector{String}, method, got_GMT_opts::Bool, d::Dict{Symbol, Any})::Union{GItype, GDtype, Gdal.AbstractDataset, Nothing}
346+
function _helper_run_GDAL_fun(f::Function, indata, dest::String, opts::Union{String, Vector{String}}, method, got_GMT_opts::Bool, d::Dict{Symbol, Any})::Union{GItype, GDtype, Gdal.AbstractDataset, Nothing}
337347
# This second level helper function reduces the number of multiple compiles. Here, only 'indata' may have different types.
338348

339349
Vd::Int = ((val = find_in_dict(d, [:Vd])[1]) !== nothing) ? val : 0 # More gymns to avoid Anys

0 commit comments

Comments
 (0)