The problem
My georeferenced and projected Matplotlib map overlays with an offset on a basemap projected in QGIS with the same coordinate reference system.
What I did:
I created a map in Matplotlib and saved it in the usual way (as a tif) using savefig. This tif file has no georeferencing. I then used a system call from my Python script to GDAL translate to add georeferencing. The resulting GeoTIFF has georeferencing. Finally I used GDAL warp to project the data.
I started a clean project in QGIS, and loaded the Google satellite basemap. I then added my projected Matplotlib map as a raster layer. The screenshot of the overlay (linked below) shows that there is an offset between the basemap and the map overlay. This seems to be a scaling problem, but I'd like to be able to fix the problem. Any help would be great.
Here's a link to the files mentioned: tiff files and screenshot
Here's some of the relevant Python code:
Save the Matplotlib map
plt.savefig( "/mnt/data/dynamic_data/projects/projects2022/recreational_fishing_GHRSST/figures/plot_SST_fronts_"+region+".tif",
format="tif",
bbox_inches='tight',
dpi=300
)
Get the corner coordinates from the data
ds_raster = rasterio.open("/mnt/data/dynamic_data/projects/projects2022/recreational_fishing_GHRSST/data/"+region+"_sst.tif")
bounds = ds_raster.bounds
left= bounds.left
bottom = bounds.bottom
right = bounds.right
top = bounds.top
# construct ullr string to use in gdal_translate
ullr = [left, bottom, right, top]
ullr_string = ' '.join(str(e) for e in ullr)
Write the georeferencing to the Matplotlib map using GDAL translate
os.system("gdal_translate -of GTiff -a_ullr "+ullr_string+" -a_srs EPSG:4326 /mnt/data/dynamic_data/projects/projects2022/recreational_fishing_GHRSST/figures/plot_SST_fronts_"+region+".tif /mnt/data/dynamic_data/projects/projects2022/recreational_fishing_GHRSST/figures/plot_SST_fronts_"+region+"_trans.tif -co TILED=YES")
Project the georeferenced map using GDAL warp
os.system("gdalwarp -t_srs '+proj=merc +datum=WGS84' /mnt/data/dynamic_data/projects/projects2022/recreational_fishing_GHRSST/figures/plot_SST_fronts_"+region+"_trans.tif /mnt/data/dynamic_data/projects/projects2022/recreational_fishing_GHRSST/figures/plot_SST_fronts_"+region+"_proj.tif")