0

I am opening a raster with rasterio.open() after which I extract its bbox:

import rasterio

img = rasterio.open('myraster.tif') bbox = img.bounds print(bbox)

BoundingBox(left=450498.0, bottom=3509708.5, right=452789.0, top=3510922.5)

I want to plot it know on a folium map, for which I need the coordinates in lat/lon and not in UTM (which they are by default).

Is there a way to reproject the bbox directly within rasterio. My approach so far would be to convert that bbox to a shapely polygon, then reproject and plot. Would there be a more direct solution to avoid creating the shapely polygon?

GCGM
  • 1,118
  • 1
  • 16
  • 35

1 Answers1

2

You can use pyproj. Specifically the pyproj.transformer.Transformer class.

import rasterio as rio
from pyproj import Transformer

with rio.open('myraster.tif') as img: xmin, ymin, xmax, ymax = img.bounds transformer = Transformer.from_crs(img.crs, "EPSG:4326")

ul = transformer.transform(xmin,ymax)
lr = transformer.transform(xmax,ymin)

print(ul, lr)

user2856
  • 65,736
  • 6
  • 115
  • 196