Skip to content

Commit

Permalink
add st_mosaic
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Oct 30, 2023
1 parent 55d06e9 commit 146fec7
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions src/st_bbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -50,21 +49,14 @@ 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])
bbox(minimum(lon) - cellx / 2, minimum(lat) - celly / 2,
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})
Expand All @@ -83,18 +75,13 @@ 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)
ilon, ilat
end


# function st_mosaic()
# end


## add support for Rasters
using Rasters: Raster
Expand All @@ -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

0 comments on commit 146fec7

Please sign in to comment.