diff --git a/src/st_bbox.jl b/src/st_bbox.jl index 083a3b2..ecc32b2 100644 --- a/src/st_bbox.jl +++ b/src/st_bbox.jl @@ -36,10 +36,9 @@ end # size: array size function bbox2cellsize(b::bbox, size) - range = bbox2range(b) nlon, nlat = size[1:2] - cellx = (range[2] - range[1]) / nlon - celly = (range[4] - range[3]) / nlat + cellx = (b.xmax - b.xmin) / nlon + celly = (b.ymax - b.ymin) / nlat cellx, celly end @@ -50,7 +49,6 @@ bbox2vec(b::bbox) = [b.xmin, b.ymin, b.xmax, b.ymax] - function st_bbox(lon, lat) cellx = abs(lon[2] - lon[1]) celly = abs(lat[2] - lat[1]) @@ -58,13 +56,7 @@ function st_bbox(lon, lat) maximum(lon) + cellx / 2, maximum(lat) + celly / 2) end - - - -function st_bbox(f::String) - gdalinfo(f)["bbox"] -end - +st_bbox(f::String) = gdalinfo(f)["bbox"] # get large bbox, also know as bbox_merge function st_bbox(bs::Vector{bbox}) @@ -83,8 +75,6 @@ function bbox_overlap(b::bbox, box::bbox; cellsize=1 / 240, reverse_lat=true) ilon = findall(b.xmin .< Lon .< b.xmax) ilat = findall(b.ymin .< Lat .< b.ymax) # 由于精度的原因,有一些会存在空值 - # ilon = indexin(lon, Lon) - # ilat = indexin(lat, Lat) ## 必定是所有的数据都在box中,不然是程序的错误 @assert length(lon) == length(ilon) @assert length(lat) == length(ilat) @@ -92,9 +82,6 @@ function bbox_overlap(b::bbox, box::bbox; cellsize=1 / 240, reverse_lat=true) end -# function st_mosaic() -# end - ## add support for Rasters using Rasters: Raster @@ -107,10 +94,46 @@ function st_bbox(ra::Raster) st_bbox(lon, lat) end +st_bbox(ras::Vector{<:Raster}) = st_bbox(st_bbox.(ras)) + + +function st_cellsize(r::Raster) + lon = r.dims[1] # X + lat = r.dims[2] # Y + + lon[2] - lon[1], lat[2] - lat[1] # cellx, celly +end + +function st_mosaic(ras::Vector{<:Raster}; missingval=NaN) + r = ras[1] + T = eltype(r) + missingval = T(missingval) + + cellsize = st_cellsize(r) + + box = st_bbox(ras) + lon2, lat2 = bbox2dims(box; cellsize) + + _size = length(lon2), length(lat2) + nd = ndim(r) + jcol = repeat([:], nd - 2) + + nd >= 3 && (_size=_size..., size(r)[3:end]) + A = fill(missingval, _size) + + for i in eachindex(ras) + r = ras[i] + b = st_bbox(r) + ilon, ilat = bbox_overlap(b, box; cellsize) + A[ilon, ilat, jcol...] = r.data + end + Raster(A, box; missingval) +end + export bbox, bbox2cellsize, bbox2range, bbox2vec, bbox2dims, bbox2ndim, bbox_overlap -export st_bbox, bbox_overlap +export st_bbox, st_mosaic