Skip to content

Commit e2f64b6

Browse files
Merge pull request #26 from felixcremer/fc/lazyprinting
Add pretty printing for OrderedDictWrapper and check for Catalog type in construction
2 parents ef5976c + 1d6519d commit e2f64b6

7 files changed

Lines changed: 59 additions & 5 deletions

File tree

src/STAC.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module STAC
22

3-
import Base: keys, values, getindex, show, length, iterate
3+
import Base: keys, values, getindex, show, length, iterate, ==
44
import CFTime
55
import Dates: DateTime
66
import Dates

src/asset.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Get the $($name) of a STAC `asset` (or `default` if it is not specified).
1717
end
1818
end
1919

20+
(==)(a1::Asset, a2::Asset) = a1.data == a2.data
21+
2022
function Base.show(io::IO,asset::Asset)
2123
_printstyled(io, "title: ",title(asset), "\n", bold=true, color=title_color[])
2224
_printstyled(io, description(asset), "\n")

src/catalog.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ function Catalog(url::String; parent = nothing, limit = 200)
118118
if !haskey(data,:id)
119119
throw(ArgumentError("The mandatory STAC id element is missing in $url"))
120120
end
121+
if haskey(data, :type)
122+
if data[:type] !== "Catalog"
123+
throw(ArgumentError("The provided STAC url is of type $(data[:type]). Expected a STAC url of type Catalog."))
124+
end
125+
end
121126

122127
assets = _assets(data)
123128

@@ -207,9 +212,12 @@ end
207212

208213
children_ids(catalog::Catalog) = _rel_ids(Catalog,catalog,:child)
209214
child(catalog::Catalog,id::AbstractString) = _rel(Catalog,catalog,:child,id)
215+
odwtype(::typeof(STAC.child)) = "Children of "
210216

211217
items_ids(catalog::Catalog) = _rel_ids(Item,catalog,:item)
212218
item(catalog::Catalog,id::AbstractString) = _rel(Item,catalog,:item,id)
219+
odwtype(::typeof(STAC.item)) = "Items of "
220+
213221

214222
@inline function Base.getproperty(catalog::Catalog,name::Symbol)
215223
if (name == :children)

src/item.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ struct Item
77
parent
88
end
99

10+
function (==)(item1::Item, item2::Item)
11+
comps = [getproperty(item1,k)==getproperty(item2,k) for k in fieldnames(Item)]
12+
all(comps)
13+
end
1014
# https://github.com/radiantearth/stac-spec/blob/master/item-spec/item-spec.md
1115

1216
for (prop,name) in ((:id, "identifier"),

src/search.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function FeatureCollection(url,query; method=:get, _enctype = :form_urlencoded)
3232
r = HTTP.get(url)
3333
end
3434
data = JSON3.read(String(r.body))
35-
3635
for d in data[:features]
3736
geojson = GeoJSON.read(JSON3.write(d))
3837
put!(c,STAC.Item("",d,geojson,STAC._assets(d),nothing))
@@ -148,11 +147,21 @@ function search(cat::Catalog, collections, lon_range, lat_range, time_range;
148147
end
149148

150149
@debug "full query:" full_query
150+
searchurl = getsearchurl(cat.url)
151151
return FeatureCollection(
152-
cat.url * "/search",full_query,
152+
searchurl,full_query,
153153
method = :post,
154154
_enctype = :json
155155
)
156156
end
157157

158+
function getsearchurl(url)
159+
r = HTTP.get(url)
160+
data = JSON3.read(String(r.body))
161+
searchlinks = filter(d -> get(d,"rel",nothing) == "search",data[:links])
162+
searchgeojson = filter(d -> get(d,"type",nothing) == "application/geo+json",searchlinks)
163+
isnothing(searchgeojson) && ArgumentError("Search not available for $url")
164+
searchgeojson[1]["href"]
165+
end
166+
158167
export search

src/utils.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ function Base.keys(ld::LazyOrderedDict)
2222
end
2323

2424
function Base.getindex(ld::LazyOrderedDict{K,V},id::K) where {K,V}
25-
if ld.getindex_guess != nothing
25+
if ld.getindex_guess !== nothing
2626
guess = ld.getindex_guess(ld.list,ld.fun,id)
27-
if guess != nothing
27+
if guess !== nothing
2828
return guess
2929
end
3030
end
@@ -59,8 +59,29 @@ end
5959

6060
Base.keys(odw::OrderedDictWrapper) = odw.keys(odw.data)
6161
Base.getindex(odw::OrderedDictWrapper,key) = odw.getindex(odw.data,key)
62+
function Base.getindex(odw::OrderedDictWrapper, intkey::Integer)
63+
odwkeys = keys(odw)
64+
outkey = eltype(odwkeys)[]
65+
for (i, key) in enumerate(odwkeys)
66+
if i == intkey
67+
push!(outkey, key)
68+
break
69+
end
70+
end
71+
if isempty(outkey)
72+
throw(BoundsError(odw, intkey))
73+
end
74+
odw.getindex(odw.data, outkey[1])
75+
end
6276

77+
function Base.show(io::IO, m::MIME"text/plain", odw::OrderedDictWrapper)
78+
_printstyled(io, typeof(odw),"\n")
79+
_printstyled(io, odwtype(odw.getindex))
80+
_printstyled(io, "Parent Catalog: ", title(odw.data))
81+
end
6382

83+
Base.show(io::IO, odw::OrderedDictWrapper) = show(io, MIME("text/plain"), odw)
84+
odwtype(x) = string(x) * " of "
6485
Base.length(odw::OrderedDictWrapper) = length(collect(keys(odw)))
6586

6687
# https://github.com/JuliaLang/julia/blob/95c643a689293eb91a47cc83c41533a94c3677cc/base/channels.jl

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ end
3838

3939
@test subcat1 isa STAC.Catalog
4040
testshow(subcat1,"Items")
41+
subitems = subcat1.items
42+
testshow(subitems, "Parent Catalog")
4143

4244
item = subcat1.items["LC08_L1TP_152038_20200611_20200611_01_RT"]
45+
itemint = subcat1.items[1]
46+
@test item == itemint
47+
@test_throws BoundsError subcat1.items[2]
4348
testshow(item,"box")
49+
testshow(item, "LC08_L1TP_152038_20200611_20200611_01_RT")
4450

4551
@test geometry(item) isa STAC.GeoJSON.Polygon
4652
@test bbox(item) isa AbstractVector
@@ -84,6 +90,10 @@ end
8490
@test length(STAC.CACHE) == 0
8591
end
8692

93+
@testset "Malformed URL" begin
94+
@test_throws ArgumentError STAC.Catalog("https://geoservice.dlr.de/eoc/ogc/stac/v1/collections/TDM_FNF_50")
95+
@test_throws ArgumentError STAC.Catalog("https://stac.core.eopf.eodc.eu/collections")
96+
end
8797

8898

8999
@testset "search" begin

0 commit comments

Comments
 (0)