diff --git a/ext/RastersArchGDALExt/RastersArchGDALExt.jl b/ext/RastersArchGDALExt/RastersArchGDALExt.jl index 293013406..e0f0c4ece 100644 --- a/ext/RastersArchGDALExt/RastersArchGDALExt.jl +++ b/ext/RastersArchGDALExt/RastersArchGDALExt.jl @@ -31,6 +31,8 @@ const DA = DiskArrays const GI = GeoInterface const LA = Lookups +Rasters.is_loaded(::Rasters.ArchGDALExt) = true + include("gdal_source.jl") include("resample.jl") include("warp.jl") diff --git a/ext/RastersCoordinateTransformationsExt/RastersCoordinateTransformationsExt.jl b/ext/RastersCoordinateTransformationsExt/RastersCoordinateTransformationsExt.jl index 1643f7a1e..2406d91fe 100644 --- a/ext/RastersCoordinateTransformationsExt/RastersCoordinateTransformationsExt.jl +++ b/ext/RastersCoordinateTransformationsExt/RastersCoordinateTransformationsExt.jl @@ -12,6 +12,8 @@ const RA = Rasters const DD = DimensionalData const LA = Lookups +Rasters.is_loaded(::Rasters.CoordinateTransformationsExt) = true + include("affineprojected.jl") end # module diff --git a/ext/RastersGRIBDatasetsExt/RastersGRIBDatasetsExt.jl b/ext/RastersGRIBDatasetsExt/RastersGRIBDatasetsExt.jl index 21260b4ab..593bd3784 100644 --- a/ext/RastersGRIBDatasetsExt/RastersGRIBDatasetsExt.jl +++ b/ext/RastersGRIBDatasetsExt/RastersGRIBDatasetsExt.jl @@ -10,6 +10,8 @@ const RA = Rasters const DA = DiskArrays const GDS = GRIBDatasets +Rasters.is_loaded(::Rasters.GRIBDatasetsExt) = true + include("gribdatasets_source.jl") end diff --git a/ext/RastersMakieExt/RastersMakieExt.jl b/ext/RastersMakieExt/RastersMakieExt.jl index 4ddae96c3..702204cde 100644 --- a/ext/RastersMakieExt/RastersMakieExt.jl +++ b/ext/RastersMakieExt/RastersMakieExt.jl @@ -6,6 +6,8 @@ using Rasters using Rasters.DimensionalData using Rasters.Dimensions +Rasters.is_loaded(::Rasters.MakieExt) = true + include("plotrecipes.jl") end diff --git a/ext/RastersNCDatasetsExt/RastersNCDatasetsExt.jl b/ext/RastersNCDatasetsExt/RastersNCDatasetsExt.jl index 8513b5c27..702a3d241 100644 --- a/ext/RastersNCDatasetsExt/RastersNCDatasetsExt.jl +++ b/ext/RastersNCDatasetsExt/RastersNCDatasetsExt.jl @@ -20,6 +20,8 @@ const RA = Rasters const DD = DimensionalData const LA = Lookups +Rasters.is_loaded(::Rasters.NCDatasetsExt) = true + include("ncdatasets_source.jl") end diff --git a/ext/RastersProjExt/RastersProjExt.jl b/ext/RastersProjExt/RastersProjExt.jl index bb9892946..6bfd22efc 100644 --- a/ext/RastersProjExt/RastersProjExt.jl +++ b/ext/RastersProjExt/RastersProjExt.jl @@ -28,6 +28,8 @@ const DA = DiskArrays const GI = GeoInterface const LA = Lookups +Rasters.is_loaded(::Rasters.ProjExt) = true + include("cellarea.jl") include("reproject.jl") end \ No newline at end of file diff --git a/ext/RastersRasterDataSourcesExt/RastersRasterDataSourcesExt.jl b/ext/RastersRasterDataSourcesExt/RastersRasterDataSourcesExt.jl index 20beabb45..b6979681c 100644 --- a/ext/RastersRasterDataSourcesExt/RastersRasterDataSourcesExt.jl +++ b/ext/RastersRasterDataSourcesExt/RastersRasterDataSourcesExt.jl @@ -8,6 +8,8 @@ using Rasters.Dimensions const RA = Rasters const RDS = RasterDataSources +Rasters.is_loaded(::Rasters.RasterDataSourcesExt) = true + include("constructors.jl") end # Module diff --git a/ext/RastersStatsBaseExt/RastersStatsBaseExt.jl b/ext/RastersStatsBaseExt/RastersStatsBaseExt.jl index fd6f1e918..b13add76f 100644 --- a/ext/RastersStatsBaseExt/RastersStatsBaseExt.jl +++ b/ext/RastersStatsBaseExt/RastersStatsBaseExt.jl @@ -8,6 +8,8 @@ const RA = Rasters import Rasters: _True, _False, _booltype, istrue import Rasters.DimensionalData as DD +Rasters.is_loaded(::Rasters.StatsBaseExt) = true + include("sample.jl") end # Module diff --git a/ext/RastersZarrDatasetsExt/RastersZarrDatasetsExt.jl b/ext/RastersZarrDatasetsExt/RastersZarrDatasetsExt.jl index bb44066aa..05cb2dfc3 100644 --- a/ext/RastersZarrDatasetsExt/RastersZarrDatasetsExt.jl +++ b/ext/RastersZarrDatasetsExt/RastersZarrDatasetsExt.jl @@ -8,6 +8,8 @@ using Rasters: Zarrsource const RA = Rasters +Rasters.is_loaded(::Rasters.ZarrDatasetsExt) = true + include("zarrdatasets_source.jl") end diff --git a/src/extensions.jl b/src/extensions.jl index 3620752ef..69970fe2d 100644 --- a/src/extensions.jl +++ b/src/extensions.jl @@ -1,12 +1,31 @@ # extensions -function throw_extension_error(f::Function, package::String, extension::Symbol, args) - if isnothing(Base.get_extension(Rasters, extension)) - throw(BackendException(package)) - else +abstract type Extension end + +# Concrete extension markers. These exist so callers can test availability +# with `is_loaded(::Type{...})` instead of scattered `Base.get_extension` calls. +struct ArchGDALExt <: Extension end +struct CoordinateTransformationsExt <: Extension end +struct GRIBDatasetsExt <: Extension end +struct MakieExt <: Extension end +struct NCDatasetsExt <: Extension end +struct ProjExt <: Extension end +struct RasterDataSourcesExt <: Extension end +struct StatsBaseExt <: Extension end +struct ZarrDatasetsExt <: Extension end + +# defaults to false +is_loaded(::Extension) = false + +function throw_extension_error(f::Function, extension::Extension, args) + if is_loaded(extension) throw(MethodError(f, args)) + else + throw(BackendException(extension)) end end +BackendException(e::E) where E <: Extension = + BackendException(replace(string(nameof(E)), r"Ext$" => "")) # stubs that need ArchGDAL @@ -90,7 +109,7 @@ savefig(b, "build/resample_example_after.png"); nothing $EXPERIMENTAL """ -resample(args...; kw...) = throw_extension_error(resample, "ArchGDAL", :RastersArchGDALExt, args) +resample(args...; kw...) = throw_extension_error(resample, ArchGDALExt(), args) """ warp(A::AbstractRaster, flags::Dict; kw...) @@ -154,7 +173,7 @@ In practise, prefer [`resample`](@ref) for this. But `warp` may be more flexible $EXPERIMENTAL """ -warp(args...; kw...) = throw_extension_error(warp, "ArchGDAL", :RastersArchGDALExt, args) +warp(args...; kw...) = throw_extension_error(warp, ArchGDALExt(), args) # stubs that need Proj @@ -218,7 +237,7 @@ function cellarea(method::GeometryOpsCore.Spherical, dims::Tuple{<:XDim, <:YDim} return Raster(areas; dims) end -_spherical_cellarea(args...; kw...) = throw_extension_error(_spherical_cellarea, "Proj", :RastersProjExt, args) +_spherical_cellarea(args...; kw...) = throw_extension_error(_spherical_cellarea, ProjExt(), args) function _planar_cellarea(dims::Tuple{<:XDim, <:YDim}) xbnds, ybnds = DD.intervalbounds(dims) @@ -273,7 +292,7 @@ Rasters.sample(myraster, 5; weights=cellarea(myraster)) @NamedTuple{geometry::Tuple{Float64, Float64}, ::Union{Missing, Float64}}(((110.0, 10.0), 0.5291143028176258)) ``` """ -sample(args...; kw...) = throw_extension_error(sample, "StatsBase", :RastersStatsBaseExt, args) +sample(args...; kw...) = throw_extension_error(sample, StatsBaseExt, args) diff --git a/src/methods/reproject.jl b/src/methods/reproject.jl index 28b095309..c297d5e2c 100644 --- a/src/methods/reproject.jl +++ b/src/methods/reproject.jl @@ -72,7 +72,7 @@ function _reproject(source::GeoFormat, target::GeoFormat, dim::Union{XDim,YDim}, reshape(_reproject(source, target, dim, vec(vals)), size(vals)) end function _reproject(source::GeoFormat, target::GeoFormat, dim::Union{XDim,YDim}, vals::AbstractVector) - throw_extension_error(reproject, "Proj", :RastersProjExt, (source, target, dim, vals)) + throw_extension_error(reproject, ProjExt(), (source, target, dim, vals)) end # Guess the step for arrays