Skip to content

Commit cf02f94

Browse files
authored
Merge pull request #2010 from GenericMappingTools/async-tiles-download
Let img tiles be downloaded async.
2 parents d1fd810 + f3a1da8 commit cf02f94

1 file changed

Lines changed: 30 additions & 5 deletions

File tree

src/imgtiles.jl

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,13 @@ function mosaic(lon::Vector{<:Float64}, lat::Vector{<:Float64}; pt_radius=637813
504504
img = getImgTile(quadkey, quad_[1], tile_url, cache, cache_supp, ext, isZXY, verbose)
505505
else
506506
img = zeros(UInt8, (256 * nMo, 256 * mMo, 3))
507-
for row = 1:mMo # Rows
508-
for col = 1:nMo # Cols
509-
img[(col-1)*256+1:col*256, (row-1)*256+1:row*256, :] =
507+
tiledir = string(cache, cache_supp) # Pre-create cache dir once to avoid a concurrent mkpath race
508+
(!isempty(cache) && !isdir(tiledir)) && mkpath(tiledir)
509+
# Cooperative @async (NOT @threads): GDAL is not thread-safe, but Downloads.download yields to the
510+
# scheduler during the network wait, so the tile fetches overlap while the C code stays serialized.
511+
@sync for row = 1:mMo # Rows
512+
for col = 1:nMo # Cols
513+
@async img[(col-1)*256+1:col*256, (row-1)*256+1:row*256, :] =
510514
getImgTile(quadkey, quad_[row, col], tile_url[row, col], cache, cache_supp, ext, isZXY, verbose)
511515
end
512516
end
@@ -979,6 +983,27 @@ function getLonLat(pixelX, pixelY, zoomL)
979983
return lon, lat
980984
end
981985

986+
# -----------------------------------------------------------------------------------------
987+
"""
988+
Optional sink for per-tile fetch messages. When set to a `Function`, `mosaic`'s
989+
`"Downloading file ..."` / `"Retrieving file from cache: ..."` notes are routed to it (emitted at
990+
full verbosity, regardless of `verbose`) instead of being printed to stdout. Lets a host GUI show
991+
tile activity without an fd-level stdout redirect (which corrupts the C tile fetch). `nothing`
992+
(default) -> normal `verbose`-gated `println`.
993+
"""
994+
const TILE_LOGGER = Ref{Union{Nothing,Function}}(nothing)
995+
996+
# Emit a tile-fetch progress note: to TILE_LOGGER if set (always), else println when verbose>=level.
997+
function _tilemsg(verbose::Int, level::Int, msg::AbstractString)
998+
lg = TILE_LOGGER[]
999+
if lg !== nothing
1000+
try; lg(msg); catch; end
1001+
elseif verbose >= level
1002+
println(msg)
1003+
end
1004+
return
1005+
end
1006+
9821007
# -----------------------------------------------------------------------------------------
9831008
function getImgTile(quadkey, quadtree, url, cache, cache_supp, ext, isZXY, verbose)::Array{UInt8,3}
9841009
# Get the image either from a local cache or by url
@@ -992,7 +1017,7 @@ function getImgTile(quadkey, quadtree, url, cache, cache_supp, ext, isZXY, verbo
9921017
fname = string(cache, cache_supp, filesep, quadtree, ".", ext)
9931018

9941019
if isfile(fname)
995-
(verbose > 1) && println("Retrieving file from cache: ", fname)
1020+
_tilemsg(verbose, 2, string("Retrieving file from cache: ", fname))
9961021
_img = gdalread(fname)
9971022
else
9981023
_img = netFetchTile(url, string(cache, cache_supp), quadtree, ext, verbose)
@@ -1020,7 +1045,7 @@ end
10201045
# -----------------------------------------------------------------------------------------
10211046
function netFetchTile(url, cache, quadtree, ext, verbose)
10221047
# Fetch a file from the web either using gdal or Downloads (when gdal is not able to)
1023-
(verbose > 0) && println("Downloading file ", url)
1048+
_tilemsg(verbose, 1, string("Downloading file ", url))
10241049
try
10251050
dest_fiche = "lixogrr" # Don't recall anymore what this default, defaults for!
10261051
if !isempty(cache)

0 commit comments

Comments
 (0)