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

RFC: Allow to use interactive Datasets in unsafe_gdal functions in utilities.jl #167

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
68 changes: 34 additions & 34 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
gdalinfo(dataset::Dataset, options = String[])
gdalinfo(dataset::AbstractDataset, options = String[])

List various information about a GDAL supported raster dataset.

Expand All @@ -11,15 +11,15 @@ List various information about a GDAL supported raster dataset.
### Returns
String corresponding to the information about the raster dataset.
"""
function gdalinfo(dataset::Dataset, options = String[])
function gdalinfo(dataset::AbstractDataset, options = String[])
options = GDAL.gdalinfooptionsnew(options, C_NULL)
result = GDAL.gdalinfo(dataset.ptr, options)
GDAL.gdalinfooptionsfree(options)
return result
end

"""
unsafe_gdaltranslate(dataset::Dataset, options = String[]; dest = "/vsimem/tmp")
unsafe_gdaltranslate(dataset::AbstractDataset, options = String[]; dest = "/vsimem/tmp")

Convert raster data between different formats.

Expand All @@ -32,19 +32,19 @@ Convert raster data between different formats.
The output dataset.
"""
function unsafe_gdaltranslate(
dataset::Dataset,
dataset::T,
options = String[];
dest = "/vsimem/tmp"
)
) where {T<:AbstractDataset}
options = GDAL.gdaltranslateoptionsnew(options, C_NULL)
usage_error = Ref{Cint}()
result = GDAL.gdaltranslate(dest, dataset.ptr, options, usage_error)
GDAL.gdaltranslateoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end

"""
unsafe_gdalwarp(datasets::Vector{Dataset}, options = String[]; dest = "/vsimem/tmp")
unsafe_gdalwarp(datasets::Vector{<:AbstractDataset}, options = String[]; dest = "/vsimem/tmp")

Image reprojection and warping function.

Expand All @@ -57,20 +57,20 @@ Image reprojection and warping function.
The output dataset.
"""
function unsafe_gdalwarp(
datasets::Vector{Dataset},
datasets::Vector{T},
options = String[];
dest = "/vsimem/tmp"
)
) where T<:AbstractDataset
options = GDAL.gdalwarpappoptionsnew(options, C_NULL)
usage_error = Ref{Cint}()
result = GDAL.gdalwarp(dest, C_NULL,
length(datasets), [ds.ptr for ds in datasets], options, usage_error)
GDAL.gdalwarpappoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end

"""
unsafe_gdalvectortranslate(datasets::Vector{Dataset}, options = String[]; dest = "/vsimem/tmp")
unsafe_gdalvectortranslate(datasets::Vector{<:AbstractDataset}, options = String[]; dest = "/vsimem/tmp")

Convert vector data between file formats.

Expand All @@ -83,20 +83,20 @@ Convert vector data between file formats.
The output dataset.
"""
function unsafe_gdalvectortranslate(
datasets::Vector{Dataset},
datasets::Vector{T},
options = String[];
dest = "/vsimem/tmp"
)
) where T<:AbstractDataset
options = GDAL.gdalvectortranslateoptionsnew(options, C_NULL)
usage_error = Ref{Cint}()
result = GDAL.gdalvectortranslate(dest, C_NULL, length(datasets),
[ds.ptr for ds in datasets], options, usage_error)
GDAL.gdalvectortranslateoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end

"""
unsafe_gdaldem(dataset::Dataset, processing::String, options = String[]; dest = "/vsimem/tmp", colorfile)
unsafe_gdaldem(dataset::AbstractDataset, processing::String, options = String[]; dest = "/vsimem/tmp", colorfile)

Tools to analyze and visualize DEMs.

Expand All @@ -115,12 +115,12 @@ Tools to analyze and visualize DEMs.
The output dataset.
"""
function unsafe_gdaldem(
dataset::Dataset,
dataset::T,
processing::String,
options = String[];
dest = "/vsimem/tmp",
colorfile = C_NULL
)
) where T<:AbstractDataset
if processing == "color-relief"
@assert colorfile != C_NULL
end
Expand All @@ -129,11 +129,11 @@ function unsafe_gdaldem(
result = GDAL.gdaldemprocessing(dest, dataset.ptr, processing, colorfile,
options, usage_error)
GDAL.gdaldemprocessingoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end

"""
unsafe_gdalnearblack(dataset::Dataset, options = String[]; dest = "/vsimem/tmp")
unsafe_gdalnearblack(dataset::AbstractDataset, options = String[]; dest = "/vsimem/tmp")

Convert nearly black/white borders to exact value.

Expand All @@ -146,19 +146,19 @@ Convert nearly black/white borders to exact value.
The output dataset.
"""
function unsafe_gdalnearblack(
dataset::Dataset,
dataset::T,
options = String[];
dest = "/vsimem/tmp"
)
) where T<:AbstractDataset
options = GDAL.gdalnearblackoptionsnew(options, C_NULL)
usage_error = Ref{Cint}()
result = GDAL.gdalnearblack(dest, C_NULL, dataset.ptr, options, usage_error)
GDAL.gdalnearblackoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end

"""
unsafe_gdalgrid(dataset::Dataset, options = String[]; dest = "/vsimem/tmp")
unsafe_gdalgrid(dataset::AbstractDataset, options = String[]; dest = "/vsimem/tmp")

Create a raster from the scattered data.

Expand All @@ -171,19 +171,19 @@ Create a raster from the scattered data.
The output dataset.
"""
function unsafe_gdalgrid(
dataset::Dataset,
dataset::T,
options = String[];
dest = "/vsimem/tmp"
)
) where T<:AbstractDataset
options = GDAL.gdalgridoptionsnew(options, C_NULL)
usage_error = Ref{Cint}()
result = GDAL.gdalgrid(dest, dataset.ptr, options, usage_error)
GDAL.gdalgridoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end

"""
unsafe_gdalrasterize(dataset::Dataset, options = String[]; dest = "/vsimem/tmp")
unsafe_gdalrasterize(dataset::AbstractDataset, options = String[]; dest = "/vsimem/tmp")

Burn vector geometries into a raster.

Expand All @@ -196,19 +196,19 @@ Burn vector geometries into a raster.
The output dataset.
"""
function unsafe_gdalrasterize(
dataset::Dataset,
dataset::T,
options = String[];
dest = "/vsimem/tmp"
)
) where T<:AbstractDataset
options = GDAL.gdalrasterizeoptionsnew(options, C_NULL)
usage_error = Ref{Cint}()
result = GDAL.gdalrasterize(dest, C_NULL, dataset.ptr, options, usage_error)
GDAL.gdalrasterizeoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end

"""
unsafe_gdalbuildvrt(datasets::Vector{Dataset}, options = String[]; dest = "/vsimem/tmp")
unsafe_gdalbuildvrt(datasets::Vector{<:AbstractDataset}, options = String[]; dest = "/vsimem/tmp")

Build a VRT from a list of datasets.

Expand All @@ -221,14 +221,14 @@ Build a VRT from a list of datasets.
The output dataset.
"""
function unsafe_gdalbuildvrt(
datasets::Vector{Dataset},
datasets::Vector{T},
options = String[];
dest = "/vsimem/tmp"
)
) where T <:AbstractDataset
options = GDAL.gdalbuildvrtoptionsnew(options, C_NULL)
usage_error = Ref{Cint}()
result = GDAL.gdalbuildvrt(dest, length(datasets),
[ds.ptr for ds in datasets], C_NULL, options, usage_error)
GDAL.gdalbuildvrtoptionsfree(options)
return Dataset(result)
return T(result)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be Dataset(result) rather than T(result) (see #167 (comment) for context).

end
81 changes: 81 additions & 0 deletions test/test_gdalutilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,84 @@ AG.read("data/point.geojson") do ds_point
rm("data/point.csv")
end
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)
info_default = AG.gdalinfo(ds_small)
@test occursin("Driver: GTiff/GeoTIFF", info_default)
end

ds_tiny = AG.unsafe_gdaltranslate(ds_small, # resample to a 5×5 ascii grid
["-of","AAIGrid","-r","cubic","-tr","1200","1200"])
@test typeof(ds_tiny) == AG.IDataset
@testset "GDAL Translate" begin
@test AG.read(ds_tiny, 1) == [128 171 127 93 83;
126 164 148 114 101;
161 175 177 164 140;
185 206 205 172 128;
193 205 209 181 122]
end

@testset "GDAL Build VRT" begin
ds_vrt = AG.unsafe_gdalbuildvrt([ds_tiny])
@test AG.read(ds_vrt, 1) == [128 171 127 93 83;
126 164 148 114 101;
161 175 177 164 140;
185 206 205 172 128;
193 205 209 181 122]
end

@testset "GDAL DEM Processing" begin
ds_dempr = AG.unsafe_gdaldem(ds_tiny, "hillshade", ["-of","AAIGrid"])
@test AG.read(ds_dempr, 1) == [ 0 0 0 0 0;
0 183 180 181 0;
0 184 182 181 0;
0 183 181 177 0;
0 0 0 0 0]
end

@testset "GDAL Near Black" begin
ds_nearblack = AG.unsafe_gdalnearblack(ds_tiny, ["-of","GTiff","-color","0"])
@test AG.read(ds_nearblack, 1) == [ 0 0 0 0 0;
0 0 0 0 0;
0 0 177 0 0;
0 0 0 0 0;
0 0 0 0 0]
end

# cannot reproject file on AppVeyor yet
# GDALError (CE_Failure, code 4):
# Unable to open EPSG support file gcs.csv. Try setting the
# GDAL_DATA environment variable to point to the directory
# containing EPSG csv files.
# @testset "GDAL Warp" begin
# AG.gdalwarp([ds_small], ["-of","MEM","-t_srs","EPSG:4326"]) do ds_warped
# @test AG.width(ds_small) == 100
# @test AG.height(ds_small) == 100
# @test AG.width(ds_warped) == 109
# @test AG.height(ds_warped) == 91
# end
# end
@testset "GDAL Warp" begin
ds_warped = AG.unsafe_gdalwarp([ds_small], ["-of","MEM"])
@test AG.width(ds_small) == 100
@test AG.height(ds_small) == 100
@test AG.shortname(AG.getdriver(ds_small)) == "GTiff"
@test AG.width(ds_warped) == 100
@test AG.height(ds_warped) == 100
@test AG.shortname(AG.getdriver(ds_warped)) == "MEM"
end
end