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

Add Julian progress functions. #353

Merged
merged 11 commits into from
Jan 2, 2023
16 changes: 8 additions & 8 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ function copywholeraster!(
source::AbstractDataset,
dest::D;
options = StringList(C_NULL),
progressfunc::Function = GDAL.gdaldummyprogress,
progressdata::Any = C_NULL,
progressfunc::Function = progressfunc_wrapper,
progressdata::Any = dummyprogress,
)::D where {D<:AbstractDataset}
result = GDAL.gdaldatasetcopywholeraster(
source,
Expand Down Expand Up @@ -93,8 +93,8 @@ function unsafe_copy(
driver::Driver = getdriver(dataset),
strict::Bool = false,
options = StringList(C_NULL),
progressfunc::Function = GDAL.gdaldummyprogress,
progressdata = C_NULL,
progressfunc::Function = progressfunc_wrapper,
progressdata = dummyprogress,
)::Dataset
return Dataset(
GDAL.gdalcreatecopy(
Expand Down Expand Up @@ -152,8 +152,8 @@ function copy(
driver::Driver = getdriver(dataset),
strict::Bool = false,
options = StringList(C_NULL),
progressfunc::Function = GDAL.gdaldummyprogress,
progressdata = C_NULL,
progressfunc::Function = progressfunc_wrapper,
progressdata = dummyprogress,
)::IDataset
return IDataset(
GDAL.gdalcreatecopy(
Expand Down Expand Up @@ -954,8 +954,8 @@ function buildoverviews!(
overviewlist::Vector{Cint};
bandlist::Vector{Cint} = Cint[],
resampling::AbstractString = "NEAREST",
progressfunc::Function = GDAL.gdaldummyprogress,
progressdata = C_NULL,
progressfunc::Function = progressfunc_wrapper,
progressdata = dummyprogress,
)::T where {T<:AbstractDataset}
result = GDAL.gdalbuildoverviews(
dataset,
Expand Down
12 changes: 5 additions & 7 deletions src/raster/rasterband.jl
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ function copywholeraster!(
source::T,
dest::AbstractRasterBand;
options = StringList(C_NULL),
progressdata = C_NULL,
progressfunc::Function = GDAL.gdaldummyprogress,
progressfunc::Function = progressfunc_wrapper,
progressdata = dummyprogress,
yeesian marked this conversation as resolved.
Show resolved Hide resolved
)::T where {T<:AbstractRasterBand}
result = GDAL.gdalrasterbandcopywholeraster(
source,
Expand Down Expand Up @@ -429,17 +429,15 @@ function regenerateoverviews!(
band::T,
overviewbands::Vector{<:AbstractRasterBand},
resampling::AbstractString = "NEAREST",
# progressfunc::Function = GDAL.gdaldummyprogress,
progressdata = C_NULL,
progressfunc::Function = progressfunc_wrapper,
progressdata = dummyprogress,
)::T where {T<:AbstractRasterBand}
cfunc =
@cfunction(GDAL.gdaldummyprogress, Cint, (Cdouble, Cstring, Ptr{Cvoid}))
result = GDAL.gdalregenerateoverviews(
band,
length(overviewbands),
overviewbands,
resampling,
cfunc,
@cplprogress(progressfunc),
progressdata,
)
@cplerr result "Failed to regenerate overviews"
Expand Down
45 changes: 35 additions & 10 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,45 @@ macro cplwarn(code, message)
end
end

function dummyprogress(progress, message = "")
print("$(round(Int, progress)*100)..$message")
return true
end

function dummyprogress(progress, message = "")
yeesian marked this conversation as resolved.
Show resolved Hide resolved
return true
end

function progressfunc_wrapper(
dfComplete::Cdouble,
pszMessage::Cstring,
pProgressArg::Ptr{Cvoid},
)::Cint
yeesian marked this conversation as resolved.
Show resolved Hide resolved
pProgressArg == C_NULL && return true
f = unsafe_pointer_to_objref(pProgressArg)
isa(f, Function) || return true
pszMessage == C_NULL && return f(dfComplete)
return f(dfComplete, unsafe_string(pszMessage))
end

macro cplprogress(progressfunc)
@static if Sys.ARCH == :aarch64
@warn "User provided progress functions are unsupported on this architecture."
return @cfunction(
GDAL.gdaldummyprogress,
Cint,
(Cdouble, Cstring, Ptr{Cvoid})
)
quote
@cfunction(
$(Expr(:$, esc(progressfunc_wrapper))),
Cint,
(Cdouble, Cstring, Ptr{Cvoid})
)
end
else
return @cfunction(
$(esc(progressfunc)),
Cint,
(Cdouble, Cstring, Ptr{Cvoid})
)
quote
@cfunction(
$(Expr(:$, esc(progressfunc))),
Cint,
(Cdouble, Cstring, Ptr{Cvoid})
)
end
end
end

Expand Down