Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Jul 29, 2024
1 parent d4375b0 commit 92e89da
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version = "1.1.2"
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
DataDeps = "124859b0-ceae-595e-8997-d05f6a7a8dfe"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
Expand Down
21 changes: 15 additions & 6 deletions src/GADM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module GADM
using DataDeps
using ArchGDAL
using Tables
using HTTP

import GeoInterface as GI

Expand Down Expand Up @@ -70,12 +71,20 @@ function download(country; version="4.1")
catch
# otherwise we register the data
# and download using DataDeps.jl
register(DataDep(ID,
"Geographic data for country $country provided by the https://gadm.org project.",
"$route/$filename",
post_fetch_method=postfetch
))
@datadep_str ID
try
register(DataDep(ID,
"Geographic data for country $country provided by the https://gadm.org project.",
"$route/$filename",
Any,
post_fetch_method=postfetch
))
@datadep_str ID
catch e
if e isa HTTP.StatusError && e.status == 404
throw(ArgumentError("country code \"$country\" not found, please provide a standard ISO 3 country code"))
end
rethrow()
end
end
end

Expand Down
1 change: 1 addition & 0 deletions src/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ GI.isfeaturecollection(::Type{<:Table}) = true
GI.trait(::Table) = GI.FeatureCollectionTrait()
GI.nfeature(::GI.FeatureCollectionTrait, table::Table) = length(table.rows)
GI.getfeature(::GI.FeatureCollectionTrait, table::Table, i) = table.rows[i]
GI.geometrycolumns(::Table) = (:geom,)
GI.crs(table::Table) = table.crs
44 changes: 25 additions & 19 deletions test/GADM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,20 @@ end
# valid country code
country = GADM.get("IND")
@test Tables.istable(country)
@test GeoInterface.geomtrait(country.geom[1]) isa MultiPolygonTrait
@test Tables.rowaccess(country)
row = Tables.rows(country) |> first
@test GeoInterface.geomtrait(Tables.getcolumn(row, :geom)) isa MultiPolygonTrait

# get country and states
country, states = GADM.get("IND", depth=0), GADM.get("IND", depth=1)
@test Tables.istable(country)
@test Tables.istable(states)
@test GeoInterface.geomtrait(country.geom[1]) isa MultiPolygonTrait
@test length(states) == 12 # number of fields in table
geometries = Tables.getcolumn(states, Symbol("geom"))
@test length(geometries) == 41 # number of rows
row = Tables.rows(country) |> first
@test GeoInterface.geomtrait(Tables.getcolumn(row, :geom)) isa MultiPolygonTrait
rows = Tables.rows(states)
row = rows |> first
@test length(rows) == 41 # number of rows
@test length(Tables.columnnames(row)) == 12 # number of fields in table

# throws error when query is invalid
@test_throws ArgumentError GADM.get("IND", "Rio de Janeiro")
Expand All @@ -85,38 +89,40 @@ end
level1 = GADM.get("IND", depth=1)
level2 = GADM.get("IND", depth=2)
level3 = GADM.get("IND", depth=3)
@test length(Tables.getcolumn(level0, :geom)) == 6
@test length(Tables.getcolumn(level1, :geom)) == 41
@test length(Tables.getcolumn(level2, :geom)) == 676
@test length(Tables.getcolumn(level3, :geom)) == 2347
@test length(Tables.rows(level0)) == 6
@test length(Tables.rows(level1)) == 41
@test length(Tables.rows(level2)) == 676
@test length(Tables.rows(level3)) == 2347

somecities = ["Mumbai City", "Bangalore", "Chennai"]
@test issubset(somecities, level2.NAME_2)
level2cols = Tables.columns(level2)
@test issubset(somecities, Tables.getcolumn(level2cols, :NAME_2))

# throws argument error when the level is deeper than available in dataset
@test_throws ArgumentError GADM.get("IND", depth=4)

# subregions tests
subregions = GADM.get("SUR", "Para", depth=1)
subregions = GADM.get("SUR", "Para", depth=1) |> Tables.columns
expected = ["Bigi Poika", "Carolina", "Noord", "Oost", "Zuid"]
@test issetequal(subregions.NAME_2, expected)
@test issetequal(Tables.getcolumn(subregions, :NAME_2), expected)

# subregions with same name
suriname = GADM.get("SUR", depth=2)
@test count(==("Welgelegen"), suriname.NAME_2) == 2
suriname = GADM.get("SUR", depth=2) |> Tables.columns
@test count(==("Welgelegen"), Tables.getcolumn(suriname, :NAME_2)) == 2

coronie = GADM.get("SUR", "Coronie", "Welgelegen")
paramaribo = GADM.get("SUR", "Paramaribo", "Welgelegen")
@test length(coronie.NAME_1) == length(paramaribo.NAME_1) == 1
@test coronie.NAME_1 paramaribo.NAME_1
coronie = GADM.get("SUR", "Coronie", "Welgelegen") |> Tables.columns
paramaribo = GADM.get("SUR", "Paramaribo", "Welgelegen") |> Tables.columns
@test length(Tables.getcolumn(coronie, :NAME_1)) == length(Tables.getcolumn(paramaribo, :NAME_1)) == 1
@test Tables.getcolumn(coronie, :NAME_1) Tables.getcolumn(paramaribo, :NAME_1)

# invalid subregion
@test_throws ArgumentError GADM.get("SUR", "a", "Welgelegen")
end

@testset "basic" begin
polygon = GADM.get("VAT")
bounds = ArchGDAL.envelope(polygon.geom[1])
row = Tables.rows(polygon) |> first
bounds = ArchGDAL.envelope(Tables.getcolumn(row, :geom))
bounds_arr = [bounds.MinX, bounds.MaxX, bounds.MinY, bounds.MaxY]

# Vatican City bounding boxes lat min 41.9002044 lat max 41.9073912 lon min 12.4457442 lon max 12.4583653
Expand Down

0 comments on commit 92e89da

Please sign in to comment.