Python Rasterio – How to Get Latitude and Longitude from TIF File

geotiff-tifflatitude longitudepythonrasterio

I refer to link and want to get latitude and longitude from .tif file using rasterio.

My code is the same as the answer:

with rasterio.open(path) as src:
        band1 = src.read(1)
        print('Band1 has shape', band1.shape)
        height = band1.shape[0]
        width = band1.shape[1]
        cols, rows = np.meshgrid(np.arange(width), np.arange(height))
        xs, ys = rasterio.transform.xy(src.transform, rows, cols)
        lons= np.array(xs)
        lats = np.array(ys)
        print('lons', lons)
        print('lats', lats)

And my tiff file is here

But the output of lats and lons are like this:
enter image description here

I think it must be wrong, the scale is obviously not latitude and lontitude. Could you tell me what is the problem?

Best Answer

Your data seems to be in EPSG:6933, so rasterio is giving you the relevant coordinates for that projection.

If you want lat/lon you can use gdalwarp to reproject your raster in EPSG:4326 as:

gdalwarp -t_srs EPSG:4326  input.tif output_4326.tif

and then use the code you provided.

You can also reproject using rasterio (see documentation here).

Related Question