diff --git a/Project.toml b/Project.toml index 08e8adf..ae690cf 100644 --- a/Project.toml +++ b/Project.toml @@ -4,11 +4,18 @@ authors = ["Haakon Ludvig Langeland Ervik (@haakon-e), Anshul Singhvi splitdir(f.name)[2] == raster_name, zf.files)) + +function RasterDataSources.zippath(::Type{NaturalEarthRaster{Version, Scale, Name}}) where {Version, Scale, Name} + return joinpath(RasterDataSources.rasterpath(), "NaturalEarth", "zips", split(_zippath(NaturalEarthRaster{Version, Scale, Name}), "/")...) +end + +function RasterDataSources.zipurl(::Type{NaturalEarthRaster{Version, Scale, Name}}) where {Version, Scale, Name} + return RasterDataSources.URIs.URI(_NATURALEARTH_URI; path = "/" * _zippath(NaturalEarthRaster{Version, Scale, Name})) +end + +function RasterDataSources.zipname(::Type{NaturalEarthRaster{Version, Scale, Name}}) where {Version, Scale, Name} + return splitpath(_zippath(NaturalEarthRaster{Version, Scale, Name}))[end] +end + +function RasterDataSources.rasterpath(T::Type{NaturalEarthRaster{Version, Scale, Name}}) where {Version, Scale, Name} + return joinpath(RasterDataSources.rasterpath(), "NaturalEarth", split((_zippath(NaturalEarthRaster{Version, Scale, Name})), "/")[1:end-1]..., RasterDataSources.rastername(T)) +end + +function RasterDataSources.rastername(::Type{NaturalEarthRaster{Version, Scale, Name}}) where {Version, Scale, Name} + return string(Name) * ".tif" +end + +function RasterDataSources.getraster(T::Type{NaturalEarthRaster{Version, Scale, Name}}, layer = nothing) where {Version, Scale, Name} + raster_path = RasterDataSources.rasterpath(T) + if !isfile(raster_path) + zip_path = RasterDataSources.zippath(T) + RasterDataSources._maybe_download(RasterDataSources.zipurl(T), zip_path) + zf = RasterDataSources.ZipFile.Reader(zip_path) + mkpath(dirname(raster_path)) + raster_name = RasterDataSources.rastername(T) + @show zf + write(raster_path, read(_zipfile_to_read(raster_name, zf))) + close(zf) + end + return raster_path +end + +function __init__() + global download_cache = Scratch.get_scratch!(@__MODULE__, "rasters") +end + +# Raster API - hook into RasterDataSources + +function check_scale(scale::Int) + return scale in (110, 50, 10) +end + +function ne_file_name(scale::Int, type::String, category::String) + if type in ( + "countries", + "map_units", + "map_subunits", + "sovereignty", + "tiny_countries", + "boundary_lines_land", + "pacific_groupings", + "breakaway_disputed_areas", + "boundary_lines_disputed_areas", + "boundary_lines_maritime_indicator") + type = "admin_0_" * type + elseif type == "states" + type = "admin_1_" * type + end + + + if category == "raster" + return type + else + return "ne_$(scale)m_$(type)" + end +end + +end # module + +# This is the name checker implementation for the NaturalEarth R package. + +# function(scale = 110, +# type = "countries", +# category = c("cultural", "physical", "raster"), +# full_url = FALSE) { +# # check on permitted scales, convert names to numeric +# scale <- check_scale(scale) + +# # check permitted category +# category <- match_arg(category) + + +# # add admin_0 to known types +# if (type %in% c( +# "countries", +# "map_units", +# "map_subunits", +# "sovereignty", +# "tiny_countries", +# "boundary_lines_land", +# "pacific_groupings", +# "breakaway_disputed_areas", +# "boundary_lines_disputed_areas", +# "boundary_lines_maritime_indicator" +# )) { +# type <- paste0("admin_0_", type) +# } + + +# # add admin_1 to known types +# # this actually just expands 'states' to the name including lakes +# if (type == "states") { +# type <- "admin_1_states_provinces_lakes" +# } + + +# if (category == "raster") { +# # raster seems not to have so straightforward naming, so require that name +# # is passed in type +# file_name <- paste0(type) +# } else { +# file_name <- paste0("ne_", scale, "m_", type) +# } + + +# # https://naturalearth.s3.amazonaws.com/110m_cultural/ne_110m_admin_0_countries.zip +# if (full_url) { +# file_name <- paste0( +# "https://naturalearth.s3.amazonaws.com/", +# scale, "m_", category, "/", +# file_name, ".zip" +# ) +# } + +# return(file_name) +# } -end # end module +# end # end module