Skip to content

Commit

Permalink
Fix of custom GDAL error handler management by deregistrating it at e…
Browse files Browse the repository at this point in the history
…xit (done in `__init__()`). Plus a few modifications on GDAL utilities' tests and (#245)

* For GDAL utilities tests, separate error tests from normal tests

* Added GDAL import in test_images.jl

* Suppressed `@time` macro in test_ospy_examples.jl

* Add `@test_logs` to two tests issuing a warning in test_rasterattrtable.jl

* CPLDestroyMutex Error narrowed on one test in test_gdalutilities_errors.jl

* `GDAL.gdalinfo` in a try...finally block

* fixed a typo in file name in "GDAL error" testset

* changed `options` local variable name in `AG.gdalinfo` in case it may interfere with `options` arg

* Tried to test errored  `AG.galinfo`  with `@test` instead of `@test_throws`

* cleanup after (unsuccessful) investigation on "CPLDestroyMutex: Error = 16" issue

* DriverManager modifications proposition

* deregister gdaljl_errorhandler at exit

* deregisterering gdaljl_errorhandler via an anonymous function to avoid test coverage decrease

* Cleanup keeping gdaljl_errorhandler deregistration at exit in __init()__

* Suppressed `GDAL.cplseterrorhandler` (handled in GDAL.jl `__init__()`) and added `GDAL.gdaldestroydrivermanager` in `atexit`

* Cleanup in `__init__()` with GDAL.jl PR #124 as prerequisite

* Suppressed comments in ArchGDAL.jl `__init__()`  function

* Dropped `gdalallregister()` in `__init__()` since it has been added to GDAL.jl in PR JuliaGeo/GDAL.jl#124
  • Loading branch information
mathieu17g authored Oct 11, 2021
1 parent 58e2073 commit 43e1408
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 122 deletions.
12 changes: 0 additions & 12 deletions src/ArchGDAL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,7 @@ include("tables.jl")
include("geointerface.jl")
include("convert.jl")

mutable struct DriverManager
function DriverManager()
drivermanager = new()
GDAL.gdalallregister()
finalizer((dm,) -> GDAL.gdaldestroydrivermanager(), drivermanager)
return drivermanager
end
end

const DRIVER_MANAGER = Ref{DriverManager}()

function __init__()
DRIVER_MANAGER[] = DriverManager()
return nothing
end

Expand Down
10 changes: 6 additions & 4 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ List various information about a GDAL supported raster dataset.
String corresponding to the information about the raster dataset.
"""
function gdalinfo(dataset::AbstractDataset, options = String[])::String
options = GDAL.gdalinfooptionsnew(options, C_NULL)
result = GDAL.gdalinfo(dataset.ptr, options)
GDAL.gdalinfooptionsfree(options)
return result
gdal_info_options = GDAL.gdalinfooptionsnew(options, C_NULL)
return try
GDAL.gdalinfo(dataset.ptr, gdal_info_options)
finally
GDAL.gdalinfooptionsfree(gdal_info_options)
end
end

"""
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ include("remotefiles.jl")
include("test_array.jl")
include("test_spatialref.jl")
include("test_gdalutilities.jl")
include("test_gdalutilities_errors.jl")
include("test_rasterattrtable.jl")
include("test_ospy_examples.jl")
include("test_geos_operations.jl")
Expand Down
53 changes: 0 additions & 53 deletions test/test_gdalutilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,6 @@ using Test

@testset "test_gdalutilities.jl" begin
AG.read("data/utmsmall.tif") do ds_small
@testset "GDAL Error" begin
@test_throws GDAL.GDALError AG.gdalinfo(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdaltranslate(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalbuildvrt(
[ds_small],
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdaldem(
ds_small,
"hillshade",
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalnearblack(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalwarp(
[ds_small],
["-novalidoption"],
)
end

@testset "GDAL Info" begin
infostr = AG.gdalinfo(ds_small, ["-checksum"])
@test occursin("Checksum=50054", infostr)
Expand Down Expand Up @@ -184,31 +156,6 @@ end

@testset "Interactive data/utmsmall.tif" begin
ds_small = AG.read("data/utmsmall.tif")
@testset "GDAL Error" begin
@test_throws GDAL.GDALError AG.gdalinfo(ds_small, ["-novalidoption"])
@test_throws GDAL.GDALError AG.unsafe_gdaltranslate(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalbuildvrt(
[ds_small],
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdaldem(
ds_small,
"hillshade",
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalnearblack(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalwarp(
[ds_small],
["-novalidoption"],
)
end

@testset "GDAL Info" begin
infostr = AG.gdalinfo(ds_small, ["-checksum"])
@test occursin("Checksum=50054", infostr)
Expand Down
61 changes: 61 additions & 0 deletions test/test_gdalutilities_errors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import ArchGDAL, GDAL;
const AG = ArchGDAL
using Test

@testset "test_gdalutilities_errors.jl" begin
AG.read("data/utmsmall.tif") do ds_small
@testset "GDAL Error" begin
@test_throws GDAL.GDALError AG.gdalinfo(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdaltranslate(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalbuildvrt(
[ds_small],
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdaldem(
ds_small,
"hillshade",
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalnearblack(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalwarp(
[ds_small],
["-novalidoption"],
)
end
end

@testset "Interactive data/utmsmall.tif" begin
ds_small = AG.read("data/utmsmall.tif")
@test_throws GDAL.GDALError AG.gdalinfo(ds_small, ["-novalidoption"])
@test_throws GDAL.GDALError AG.unsafe_gdaltranslate(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalbuildvrt(
[ds_small],
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdaldem(
ds_small,
"hillshade",
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalnearblack(
ds_small,
["-novalidoption"],
)
@test_throws GDAL.GDALError AG.unsafe_gdalwarp(
[ds_small],
["-novalidoption"],
)
end
end
1 change: 1 addition & 0 deletions test/test_images.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ArchGDAL;
const AG = ArchGDAL;
import ImageCore
import ColorTypes
import GDAL

@testset "test_images.jl" begin
@testset "Test Gray colors" begin
Expand Down
100 changes: 49 additions & 51 deletions test/test_ospy_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,69 +336,67 @@ const AG = ArchGDAL;

#reference: http://www.gis.usu.edu/~chrisg/python/2009/lectures/ospy_hw5a.py
@testset "Homework 5a" begin
@time begin
rows = AG.height(ds)
cols = AG.width(ds)
bands = AG.nraster(ds)

# get the band and block sizes
inband2 = AG.getband(ds, 2)
inband3 = AG.getband(ds, 3)
(xbsize, ybsize) = AG.blocksize(inband2)

buffer2 = Array{Float32}(undef, ybsize, xbsize)
buffer3 = Array{Float32}(undef, ybsize, xbsize)
ndvi = Array{Float32}(undef, ybsize, xbsize)
AG.create(
AG.getdriver("MEM"),
width = cols,
height = rows,
nbands = 1,
dtype = Float32,
) do outDS
for ((i, j), (nrows, ncols)) in AG.blocks(inband2)
AG.rasterio!(inband2, buffer2, j, i, ncols, nrows)
AG.rasterio!(inband3, buffer3, j, i, ncols, nrows)
data2 = buffer2[1:nrows, 1:ncols]
data3 = buffer3[1:nrows, 1:ncols]
for row in 1:nrows, col in 1:ncols
denominator = data2[row, col] + data3[row, col]
if denominator > 0
numerator = data3[row, col] - data2[row, col]
ndvi[row, col] = numerator / denominator
else
ndvi[row, col] = -99
end
rows = AG.height(ds)
cols = AG.width(ds)
bands = AG.nraster(ds)

# get the band and block sizes
inband2 = AG.getband(ds, 2)
inband3 = AG.getband(ds, 3)
(xbsize, ybsize) = AG.blocksize(inband2)

buffer2 = Array{Float32}(undef, ybsize, xbsize)
buffer3 = Array{Float32}(undef, ybsize, xbsize)
ndvi = Array{Float32}(undef, ybsize, xbsize)
AG.create(
AG.getdriver("MEM"),
width = cols,
height = rows,
nbands = 1,
dtype = Float32,
) do outDS
for ((i, j), (nrows, ncols)) in AG.blocks(inband2)
AG.rasterio!(inband2, buffer2, j, i, ncols, nrows)
AG.rasterio!(inband3, buffer3, j, i, ncols, nrows)
data2 = buffer2[1:nrows, 1:ncols]
data3 = buffer3[1:nrows, 1:ncols]
for row in 1:nrows, col in 1:ncols
denominator = data2[row, col] + data3[row, col]
if denominator > 0
numerator = data3[row, col] - data2[row, col]
ndvi[row, col] = numerator / denominator
else
ndvi[row, col] = -99
end
# write the data
AG.write!(outDS, ndvi, 1, j, i, ncols, nrows)
end
@test sprint(print, outDS) == """
# write the data
AG.write!(outDS, ndvi, 1, j, i, ncols, nrows)
end
@test sprint(print, outDS) == """
GDAL Dataset (Driver: MEM/In Memory Raster)
File(s):
Dataset (width x height): 5665 x 5033 (pixels)
Number of raster bands: 1
[GA_Update] Band 1 (Undefined): 5665 x 5033 (Float32)
"""
# flush data to disk, set the NoData value and calculate stats
outband = AG.getband(outDS, 1)
@test sprint(print, outband) == """
# flush data to disk, set the NoData value and calculate stats
outband = AG.getband(outDS, 1)
@test sprint(print, outband) == """
[GA_Update] Band 1 (Undefined): 5665 x 5033 (Float32)
blocksize: 5665×1, nodata: nothing, units: 1.0px + 0.0
overviews: """
AG.setnodatavalue!(outband, -99)
# georeference the image and set the projection
AG.setgeotransform!(outDS, AG.getgeotransform(ds))
return AG.setproj!(outDS, AG.getproj(ds))

# build pyramids
# gdal.SetConfigOption('HFA_USE_RRD', 'YES')
# AG.buildoverviews!(outDS,
# Cint[2,4,8,16,32,64,128], # overview list
# # bandlist (omit to include all bands)
# resampling="NEAREST") # resampling method
end
AG.setnodatavalue!(outband, -99)
# georeference the image and set the projection
AG.setgeotransform!(outDS, AG.getgeotransform(ds))
return AG.setproj!(outDS, AG.getproj(ds))

# build pyramids
# gdal.SetConfigOption('HFA_USE_RRD', 'YES')
# AG.buildoverviews!(outDS,
# Cint[2,4,8,16,32,64,128], # overview list
# # bandlist (omit to include all bands)
# resampling="NEAREST") # resampling method
end
end
end
Expand Down
10 changes: 8 additions & 2 deletions test/test_rasterattrtable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ const AG = ArchGDAL;
end

AG.setlinearbinning!(rat, 0, 10)
@test AG.getlinearbinning(rat) == (0, 10)
@test (@test_logs (
:warn,
"There is no linear binning information.",
) AG.getlinearbinning(rat)) == (0, 10)
AG.setlinearbinning!(rat, -1.5, 12.0)
@test AG.getlinearbinning(rat) == (-1.5, 12.0)
@test (@test_logs (
:warn,
"There is no linear binning information.",
) AG.getlinearbinning(rat)) == (-1.5, 12.0)

@test AG.findrowindex(rat, 0) == 0
@test AG.findrowindex(rat, -1) == 0
Expand Down

0 comments on commit 43e1408

Please sign in to comment.