Plot a raster matrix with national borders from a shape file?

I have a matrix of raster data, the original NetCDF file with projection data etc, and a shape file.

Is there an easy way to accurately plot the matrix as a heatmap with a border supplied by the shapefile?

To answer my own question for anyone else needing this:

using NetCDF, Shapefile, Plots

matrx = your_matrix
shapepath = "path/to/file.shp"
ncdfpath = "path/to/file.nc"

shp = open(shapepath) do io
    read(io, Shapefile.Handle)
end

# Get lat and long from the original NetCDF
long = ncread(ncdfpath, "longitude")
lat = ncread(ncdfpath, "latitude")
# Use a `rotXXX()` function to fix the orientation if required, 
# then plot a heatmap with correct long and lat
plt = heatmap(long, lat, rotl90(matrx))
# Then overlay the shape, specifying the xlim and ylim or the matrix
plot!(plt, shp.shapes, xlim=(long[1], long[end]), ylim=(lat[end], lat[1]))
1 Like