Skip to content

Commit

Permalink
Add convert, affine! and broadcast methods. (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion authored Feb 2, 2023
1 parent f36425f commit 6e0271c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 7 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.7.13] - 2023-01-12
- Added convert, affine!
- Added broadcast for GeoArray, so `ga .+ 1` isa `GeoArray`

## [0.7.12] - 2023-01-12
- Fix interpolation, update to GeoStatsSolvers
- Fix indexing bug in non-singleton sized GeoArrays
Expand Down Expand Up @@ -35,7 +39,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed iterate specification so `sum` on a GeoArray is correct

[unreleased]: https://github.com/evetion/GeoArrays.jl/compare/v0.7.9...HEAD
[unreleased]: https://github.com/evetion/GeoArrays.jl/compare/v0.7.13...HEAD
[0.7.13]: https://github.com/evetion/GeoArrays.jl/compare/v0.7.12...v0.7.13
[0.7.12]: https://github.com/evetion/GeoArrays.jl/compare/v0.7.11...v0.7.12
[0.7.11]: https://github.com/evetion/GeoArrays.jl/compare/v0.7.10...v0.7.11
[0.7.10]: https://github.com/evetion/GeoArrays.jl/compare/v0.7.9...v0.7.10
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GeoArrays"
uuid = "2fb1d81b-e6a0-5fc5-82e6-8e06903437ab"
authors = ["Maarten Pronk <[email protected]>"]
version = "0.7.12"
version = "0.7.13"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand All @@ -18,7 +18,7 @@ ArchGDAL = "0.7 - 0.9, 0.10"
CoordinateTransformations = "0.5 - 0.6"
GeoFormatTypes = "0.4"
GeoInterface = "1"
GeoStatsBase = "0.21 - 0.29"
GeoStatsBase = "0.21 - 0.30"
IterTools = "1"
RecipesBase = "0.7, 0.8, 1.0"
StaticArrays = "0.12, 1.0"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ julia> ga = GeoArray(rand(100,200))
julia> bbox!(ga, (min_x=2., min_y=51., max_x=5., max_y=54.)) # roughly the Netherlands
julia> epsg!(ga, 4326) # in WGS84
julia> GeoArrays.write("test.tif", ga)
# Or write it with compression and tiling
julia> GeoArrays.write("test_compressed.tif", ga; options=Dict("TILED"=>"YES", "COMPRESS"=>"ZSTD"))
```
### Streaming support
Expand Down
2 changes: 1 addition & 1 deletion src/GeoArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export compose!
export bbox_overlap
export crop

export interpolate!, fill!
export interpolate!

export epsg!
export crs!
Expand Down
15 changes: 15 additions & 0 deletions src/geoarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ Base.parent(ga::GeoArray) = ga.A
Base.eltype(::Type{GeoArray{T}}) where {T} = T
Base.show(io::IO, ::MIME"text/plain", ga::GeoArray) = show(io, ga)

Base.convert(::Type{GeoArray{T}}, ga::GeoArray) where {T} = GeoArray(convert(Array{T}, ga.A), ga.f, ga.crs)

Base.BroadcastStyle(::Type{<:GeoArray}) = Broadcast.ArrayStyle{GeoArray}()
function Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{GeoArray}}, ::Type{ElType}) where {ElType}
ga = find_ga(bc)
GeoArray(similar(Array{ElType}, axes(bc)), ga.f, ga.crs)
end

find_ga(bc::Base.Broadcast.Broadcasted) = find_ga(bc.args)
find_ga(args::Tuple) = find_ga(find_ga(args[1]), Base.tail(args))
find_ga(x) = x
find_ga(::Tuple{}) = nothing
find_ga(a::GeoArray, rest) = a
find_ga(::Any, rest) = find_ga(rest)

function Base.show(io::IO, ga::GeoArray)
crs = GFT.val(ga.crs)
wkt = length(crs) == 0 ? "undefined CRS" : "CRS $crs"
Expand Down
6 changes: 6 additions & 0 deletions src/geoutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ function bboxes(ga::GeoArray)
cellbounds
end

function affine!(ga::GeoArray, f::AffineMap)
(length(f.translation) == 2 && size(f.linear) == (2, 2)) || error("AffineMap should be two dimensional.")
ga.f = f
ga
end

# Extend CoordinateTransformations
CoordinateTransformations.compose(ga::GeoArray, t2::AffineMap) = CoordinateTransformations.compose(ga.f, t2)
CoordinateTransformations.compose(ga::GeoArray, t2::LinearMap) = CoordinateTransformations.compose(ga.f, t2)
Expand Down
17 changes: 17 additions & 0 deletions test/test_geoarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ end
GeoArrays.write!("test.tif", gg)
end

@testset "Conversion" begin
ga = GeoArrays.GeoArray(rand(Int16, 10, 10))
gc = convert(GeoArrays.GeoArray{Float32}, ga)
@test gc isa GeoArray
@test eltype(gc) == Float32
@test all(ga .== gc)
end

@testset "Broadcast" begin
ga = GeoArrays.GeoArray(rand(Int16, 10, 10))
gc = clamp.(ga, 0, 1)
@test gc isa GeoArray
@test sum(gc) < length(gc)
gd = gc .+ 1
@test sum(gd) > length(gc)
end

@testset "Indexing" begin
ga = GeoArray(rand(10, 10))
@inferred ga[Float32(1.0), Float32(2.0)]
Expand Down
6 changes: 3 additions & 3 deletions test/test_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ end
end
@testset "COG" begin
ga = GeoArray(Array{Union{Missing,Int32}}(rand(1:10, 2048, 2048, 3)))
fn = GeoArrays.write(joinpath(testdatadir, "test_cog.tif"), ga; nodata=-1, shortname="COG", options=Dict("COMPRESSION" => "ZSTD"))
fn = GeoArrays.write(joinpath(testdatadir, "test_cog.tif"), ga; nodata=-1, shortname="COG", options=Dict("compress" => "ZSTD"))
GeoArrays.read(fn)
ga = GeoArray(Array{Union{Missing,Float32}}(rand(1:10, 2048, 2048, 3)))
fn = GeoArrays.write(joinpath(testdatadir, "test_cogf.tif"), ga; nodata=Inf, shortname="COG", options=Dict("COMPRESSION" => "ZSTD"))
fn = GeoArrays.write(joinpath(testdatadir, "test_cogf.tif"), ga; nodata=Inf, shortname="COG", options=Dict("compress" => "ZSTD"))
GeoArrays.read(fn)
end
@testset "Kwargs" begin
ga = GeoArray(rand(100, 200, 3))
fn = GeoArrays.write(joinpath(tempdir(), "test.tif"), ga; shortname="COG", nodata=1.0, options=Dict("compression" => "deflate"))
fn = GeoArrays.write(joinpath(tempdir(), "test.tif"), ga; shortname="COG", nodata=1.0, options=Dict("compress" => "deflate"))
GeoArrays.read(fn)
end
@testset "NetCDF" begin
Expand Down

2 comments on commit 6e0271c

@evetion
Copy link
Owner Author

@evetion evetion commented on 6e0271c Feb 2, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/76892

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.13 -m "<description of version>" 6e0271ccd8f3797defd2b363a6cdcf40b05a3484
git push origin v0.7.13

Please sign in to comment.