import rasterio
import geopandas as gpd
import matplotlib.pyplot as plt
from rasterio.warp import calculate_default_transform, reproject, Resampling
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
from matplotlib.font_manager import FontProperties
raster_path = "C:/Users/ASUS/Desktop/t2m/t2m_202001.tif"
shapefile_path = "C:/Users/ASUS/Desktop/t2m/World_countries.shp"
world = gpd.read_file(shapefile_path)
equal_earth_crs = ccrs.EqualEarth()
with rasterio.open(raster_path) as src:
transform, width, height = calculate_default_transform(
src.crs, equal_earth_crs, src.width, src.height, *src.bounds)
kwargs = src.meta.copy()
kwargs.update({
'crs': equal_earth_crs,
'transform': transform,
'width': width,
'height': height
})
with rasterio.open('reprojected.tif', 'w', **kwargs) as dst:
for i in range(1, src.count + 1):
reproject(
source=rasterio.band(src, i),
destination=rasterio.band(dst, i),
src_transform=src.transform,
src_crs=src.crs,
dst_transform=transform,
dst_crs=equal_earth_crs,
resampling=Resampling.nearest)
fig, ax = plt.subplots(figsize=(12, 8), subplot_kw={'projection': equal_earth_crs})
ax.set_global()
with rasterio.open('reprojected.tif') as src:
data = src.read(1)
extent = [src.bounds.left, src.bounds.right, src.bounds.bottom, src.bounds.top]
img = ax.imshow(data, cmap='RdPu', extent=extent, transform=equal_earth_crs, vmin=220, vmax=320)
world.boundary.plot(ax=ax, transform=ccrs.PlateCarree(), linewidth=0.5, edgecolor='black')