Skip to content

Commit

Permalink
add geointerface :geometry column (#8)
Browse files Browse the repository at this point in the history
* add geointerface :geometry column

* add base deps compat

* fix tests
  • Loading branch information
rafaqz committed Nov 30, 2023
1 parent 548bb0f commit 75eaa54
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
Aqua = "0.8"
Base64 = "1"
CSV = "0.10"
DataFrames = "1"
Dates = "1"
HTTP = "0.8, 0.9, 1"
JSON3 = "1"
PrettyTables = "2"
Tables = "1"
Test = "1"
julia = "1.6"

[extras]
Expand Down
23 changes: 18 additions & 5 deletions src/occurrence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,28 @@ Occurrence(raw::Union{AbstractString,AbstractVector{UInt8}}) = Occurrence(JSON3.

Base.propertynames(sp::Occurrence) = keys(occurrence_properties())
function Base.getproperty(oc::Occurrence, k::Symbol)
if k in keys(object(oc))
prop = getproperty(object(oc), k)
obj = object(oc)
if k == :geometry
if hasproperty(obj, :decimalLongitude) && hasproperty(obj, :decimalLatitude)
x = getproperty(obj, :decimalLongitude)
y = getproperty(obj, :decimalLatitude)
if ismissing(x) || ismissing(y)
return missing
else
return (x, y) # Return a tuple point
end
else
return missing
end
elseif k in keys(obj)
prop = getproperty(obj, k)
# Convert JSON objects to json strings
if prop isa JSON3.Object
JSON3.write(prop)
return JSON3.write(prop)
elseif prop isa JSON3.Array{<:JSON3.Object}
JSON3.write.(prop)
return JSON3.write.(prop)
else
convert(occurrence_properties()[k], prop)
return convert(occurrence_properties()[k], prop)
end
else
k in keys(occurrence_properties()) || error("Occurrence has no field $k")
Expand Down
6 changes: 4 additions & 2 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ const OCCURRENCE_KEY_DESC = (
)

occurrence_properties() = (
# Added column for GeoInterface.jl compatibility
geometry = Union{Missing,Tuple{Float64,Float64}},
# Moved these up from the order returned in json
decimalLongitude = Union{Missing,Float64},
decimalLatitude = Union{Missing,Float64},
year = Union{Missing,Int64},
month = Union{Missing,Int64},
day = Union{Missing,Int64},
Expand All @@ -113,6 +113,8 @@ occurrence_properties() = (
stateProvince = Union{Missing,String},
eventDate = Union{Missing,String},
# Original json order starts here
decimalLongitude = Union{Missing,Float64},
decimalLatitude = Union{Missing,Float64},
key = Union{Missing,Int64},
datasetKey = Union{Missing,String},
publishingOrgKey = Union{Missing,String},
Expand Down
27 changes: 15 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ using DataFrames
using Test
using CSV

# Aqua.test_ambiguities([GBIF2, Base, Core])
Aqua.test_unbound_args(GBIF2)
Aqua.test_stale_deps(GBIF2)
Aqua.test_undefined_exports(GBIF2)
Aqua.test_project_extras(GBIF2)
Aqua.test_deps_compat(GBIF2)
Aqua.test_project_toml_formatting(GBIF2)
@testset "Aqua.jl" begin
# Aqua.test_ambiguities([GBIF2, Base, Core])
Aqua.test_unbound_args(GBIF2)
Aqua.test_stale_deps(GBIF2)
Aqua.test_undefined_exports(GBIF2)
Aqua.test_project_extras(GBIF2)
Aqua.test_deps_compat(GBIF2)
end

sp = species_match("Lalage newtoni"; class="Aves", verbose=true)

Expand All @@ -26,7 +27,7 @@ end
sp1 = species(sp)
@test sp1 isa GBIF2.Species
@test sp1.species == "Coracina newtoni"
@test sp1.synonym == false
@test sp1.synonym isa Union{Bool,Missing}
@test sp1.vernacularName == "Reunion Cuckooshrike"
class = species(sp.classKey)
@test ismissing(class.species)
Expand Down Expand Up @@ -94,15 +95,17 @@ end
CSV.write("occurence_test.csv", results)
df = CSV.read("occurence_test.csv", DataFrame)
foreach(enumerate(Tables.columns(df)), Tables.columns(DataFrame(results))) do (i, written), orig
if i == 72
if i == 73
@test all(parse.(Int64, orig) .== written)
elseif i in [20, 45, 48, 49, 50, 51, 60, 61]
nothing
elseif i in [1, 19, 46, 49, 50, 51, 52, 61, 62]
# skip
# 20 => DateTime, 45-6: Vector{String} => String
else
@test map(written, orig) do w, o
match = map(written, orig) do w, o
ismissing(w) && ismissing(o) || w == o
end |> all
end |> all || @show i written orig

end
end
end
Expand Down

0 comments on commit 75eaa54

Please sign in to comment.