1. SpatRaster

SpatRaster

SpatRaster is a simple spatial raster with WGS84 projection, abbreviated as rast.

Base.@kwdef mutable struct SpatRaster{T,N} <: AbstractSpatRaster{T,N}
  A::AbstractArray{T,N}
  b::bbox = bbox(-180.0, -90.0, 180.0, 90.0)
  cellsize::NTuple{2,Real}
  lon::AbstractVector{<:Real}
  lat::AbstractVector{<:Real}
  time::Union{AbstractVector,Nothing} = nothing
  bands::Union{AbstractVector{String},Nothing} = nothing
  name::String = "Raster"
  nodata::Union{AbstractVector{T},Nothing} = nothing
end
  • A : The raster data array where T is the element type and N is the number of dimensions
  • b : Bounding box defining the spatial extent, defaults to global extent bbox(-180.0, -90.0, 180.0, 90.0)
  • cellsize : Cell size in degrees as (longitude_size, latitude_size)
  • lon : Longitude coordinates for each column
  • lat : Latitude coordinates for each row
  • time : Time dimension values (optional)
  • bands : Band names for multi-band rasters (optional)
  • name : Name identifier for the raster, defaults to “Raster”
  • nodata : No-data values for each band (optional)

Construction

using SpatRasters
using Dates

dates = Date(2010, 1, 1):Day(1):Date(2010, 1, 10)
ntime = length(dates)

ra = rast(rand(180, 90, ntime); time=dates)
ra
SpatRaster{Float64}: Raster
  A        : Array{Float64, 3} | (180, 90, 10) | 1.24 Mb
  b        : bbox(-180.0, -90.0, 180.0, 90.0)
  cellsize : (2.0, 2.0)
  lon, lat : -179.0:2.0:179.0, 89.0:-2.0:-89.0
  time     : 2010-01-01 ~ 2010-01-10, ntime=10
  bands    : nothing
  nodata   : nothing
A = rand(70, 40)
b = bbox(70, 15, 140, 55) # China
rast(A, b)
SpatRaster{Float64}: Raster
  A        : Matrix{Float64} | (70, 40) | 0.02 Mb
  b        : bbox(70.0, 15.0, 140.0, 55.0)
  cellsize : (1.0, 1.0)
  lon, lat : 70.5:1.0:139.5, 54.5:-1.0:15.5
  time     : nothing
  bands    : nothing
  nodata   : nothing

Subset

  • ra[i, j] will automatically alter lon, lat and bbox. If both of i and j are integer, value of ra.A[i, j] will be retrieved.

  • ra[i, j, k] will additionally alter bands, and time

ra[1, 1]
ra[1:10, 1:10]
SpatRaster{Float64}: Raster
  A        : Array{Float64, 3} | (10, 10, 10) | 0.01 Mb
  b        : bbox(-180.0, 70.0, -160.0, 90.0)
  cellsize : (2.0, 2.0)
  lon, lat : -179.0:2.0:-161.0, 89.0:-2.0:71.0
  time     : 2010-01-01 ~ 2010-01-10, ntime=10
  bands    : nothing
  nodata   : nothing
ra[1:10, 1:10, 1:2]
SpatRaster{Float64}: Raster
  A        : Array{Float64, 3} | (10, 10, 2) | 0.0 Mb
  b        : bbox(-180.0, 70.0, -160.0, 90.0)
  cellsize : (2.0, 2.0)
  lon, lat : -179.0:2.0:-161.0, 89.0:-2.0:71.0
  time     : 2010-01-01 ~ 2010-01-02, ntime=2
  bands    : nothing
  nodata   : nothing
ra[1:10, 1:10, 1:1]
SpatRaster{Float64}: Raster
  A        : Array{Float64, 3} | (10, 10, 1) | 0.0 Mb
  b        : bbox(-180.0, 70.0, -160.0, 90.0)
  cellsize : (2.0, 2.0)
  lon, lat : -179.0:2.0:-161.0, 89.0:-2.0:71.0
  time     : 2010-01-01 ~ 2010-01-01, ntime=1
  bands    : nothing
  nodata   : nothing
ra[1:10, 1:10, 1]
SpatRaster{Float64}: Raster
  A        : Matrix{Float64} | (10, 10) | 0.0 Mb
  b        : bbox(-180.0, 70.0, -160.0, 90.0)
  cellsize : (2.0, 2.0)
  lon, lat : -179.0:2.0:-161.0, 89.0:-2.0:71.0
  time     : 2010-01-01 ~ 2010-01-01, ntime=1
  bands    : nothing
  nodata   : nothing

crop

截取部分范围