Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3e477cc
For GDAL utilities tests, separate error tests from normal tests
mathieu17g Oct 9, 2021
969f8ba
Added GDAL import in test_images.jl
mathieu17g Oct 9, 2021
3ff398e
Suppressed `@time` macro in test_ospy_examples.jl
mathieu17g Oct 9, 2021
e307362
Add `@test_logs` to two tests issuing a warning in test_rasterattrtab…
mathieu17g Oct 9, 2021
8bef232
CPLDestroyMutex Error narrowed on one test in test_gdalutilities_erro…
mathieu17g Oct 9, 2021
a7e0780
`GDAL.gdalinfo` in a try...finally block
mathieu17g Oct 9, 2021
9d4de2c
fixed a typo in file name in "GDAL error" testset
mathieu17g Oct 9, 2021
a94bcd0
changed `options` local variable name in `AG.gdalinfo` in case it may…
mathieu17g Oct 9, 2021
1f96ef7
Tried to test errored `AG.galinfo` with `@test` instead of `@test_t…
mathieu17g Oct 9, 2021
dbd3df2
cleanup after (unsuccessful) investigation on "CPLDestroyMutex: Error…
mathieu17g Oct 9, 2021
efa08df
DriverManager modifications proposition
mathieu17g Oct 9, 2021
82b4781
deregister gdaljl_errorhandler at exit
mathieu17g Oct 9, 2021
5f10b6e
deregisterering gdaljl_errorhandler via an anonymous function to avoi…
mathieu17g Oct 9, 2021
5e4598b
Cleanup keeping gdaljl_errorhandler deregistration at exit in __init()__
mathieu17g Oct 9, 2021
6754ada
Suppressed `GDAL.cplseterrorhandler` (handled in GDAL.jl `__init__()`…
mathieu17g Oct 9, 2021
ae7d63f
Cleanup in `__init__()` with GDAL.jl PR #124 as prerequisite
mathieu17g Oct 9, 2021
a797de6
Suppressed comments in ArchGDAL.jl `__init__()` function
mathieu17g Oct 9, 2021
8911c49
Dropped `gdalallregister()` in `__init__()` since it has been added t…
mathieu17g Oct 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions src/ArchGDAL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,8 @@ 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()
GDAL.gdalallregister()
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