Skip to content

Commit

Permalink
allow GFT directly, and document
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Oct 5, 2024
1 parent f337184 commit 9b70068
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.2.1"
[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Expand Down
3 changes: 2 additions & 1 deletion src/GBIF2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ using Dates

import Base64
import JSON3
import GeoInterface as GI
import GeoFormatTypes as GFT
import HTTP
import PrettyTables
import Tables
import GeoInterface as GI
import WellKnownGeometry

export species, species_list, species_match, species_search
Expand Down
12 changes: 11 additions & 1 deletion src/occurrence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const OCCURRENCE_SEARCH_RETURNTYPE = (
institutionCode = "Search that returns matching institution codes. Table are ordered by relevance.",
)

const EXTENT_KEYWORD = """
- `extent`: Any object that GeoInterface.jl can calaculate or retrieve an `Extents.Extent`
for using `GeoInterface.extent(extent)`. This will be converted to regular
GBIF `decimalLatitude` and `decimalLongitude` keywords.
"""


"""
Occurrence
Expand Down Expand Up @@ -227,8 +233,9 @@ ocs = occurrence_search(sp; continent=:AFRICA, limit=1000)
$(_argdocs(OCCURRENCE_SEARCH_RETURNTYPE))
# Keywords
$EXTENT_KEYWORD
We use parameters exactly as in the [GBIF api](https://www.gbif.org/developer/species).
For all other keywords we use parameters exactly as in the [GBIF api](https://www.gbif.org/developer/species).
You can find keyword enum values with the `[GBIF2.enum](@ref)` function.
Expand Down Expand Up @@ -298,6 +305,8 @@ end

_maybe_geometry_kw(::Nothing) = (;)
_maybe_geometry_kw(geometry) = (; geometry=convert(String, GI.astext(geometry)))
_maybe_geometry_kw(geometry::Union{GFT.AbstractWellKnownText,AbstractString}) =
(; geometry=convert(String, geometry))

"""
occurrence_count(species::Species; kw...)
Expand Down Expand Up @@ -433,6 +442,7 @@ recent request.
# Keywords
$EXTENT_KEYWORD
- `username`: String username for a gbif.org account
- `password`: String password for a gbif.org account. The password
will be entered in the REPL if this keyword is not used.
Expand Down
21 changes: 18 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,38 @@ end
@test all(ext_tbl.geometry) do (x, y)
x >= 50 && x <= 51 && y >= 60 && y <= 61
end

# Use a squareg geometry matching the extent
geometry = GI.Polygon(GI.LinearRing([(50, 60), (50, 61), (51, 61), (51, 60), (50, 60)]))
GI.astext(GI.Polygon([GI.LinearRing([(50, 60), (50, 61), (51, 61), (51, 60), (50, 60)])]))
geom_tbl = @test_nowarn occurrence_search("Aves"; geometry)
@test all(geom_tbl.geometry) do (x, y)
x >= 50 && x <= 51 && y >= 60 && y <= 61
end
@test geom_tbl isa GBIF2.Table{GBIF2.Occurrence}
# Use a squareg geometry matching the extent
@test all(geom_tbl.geometry) do (x, y)
x >= 50 && x <= 51 && y >= 60 && y <= 61
end

# Using well known text directly
wkt = GI.astext(GI.Polygon([GI.LinearRing([(50, 60), (50, 61), (51, 61), (51, 60), (50, 60)])]))
wkt_tbl = @test_nowarn occurrence_search("Aves"; geometry=wkt)
@test wkt_tbl isa GBIF2.Table{GBIF2.Occurrence}
@test all(wkt_tbl.geometry) do (x, y)
x >= 50 && x <= 51 && y >= 60 && y <= 61
end

# Use the extent of the geometry
geom_ext_tbl = @test_nowarn occurrence_search("Aves"; extent=geometry)
@test geom_ext_tbl isa GBIF2.Table{GBIF2.Occurrence}
@test all(geom_ext_tbl.geometry) do (x, y)
x >= 50 && x <= 51 && y >= 60 && y <= 61
end

# Test that the geometry and extent give the same results
@test sort(ext_tbl.geometry) == sort(geom_ext_tbl.geometry) == sort(geom_tbl.geometry)
@test sort(ext_tbl.geometry) ==
sort(geom_ext_tbl.geometry) ==
sort(wkt_tbl.geometry) ==
sort(geom_tbl.geometry)
end

@testset "occurrence_count" begin
Expand Down

0 comments on commit 9b70068

Please sign in to comment.