Set region by extent?

Is there a way to set the region by the extent of a raster? I have a variety of TIF files that highlight different parts of a country, and would like to automatically resize region by the extent of the TIF file.

Not sure how it works in pyGMT, but in regular GMT you can do something like this to set the region:

region = $(gmt grdinfo <file.tif> -Ir)

Then instead of using an -R flag in your gmt grdimage or whatever it is, you use this “region” variable that has been defined from the tif extents.


gmt grdimage <tif> $reg ...

Hope that helps,

Or even simpler


but that assuming that file is a GeoTIFF

The guys above have the right idea. Translating that to PyGMT, following Joaquim’s example, you would do something like fig.grdimage(grid="file.tif", region="file.tif").

The method by Jay will be more flexible, especially if you want nicely rounded coordinates. What you do in Python is

region = pygmt.grdinfo("file.tif", spacing="r")  # get exact extent
region = pygmt.grdinfo("file.tif", spacing="100r")  # get extent rounded to the nearest 100
fig.grdimage(grid="file.tif", region=region)