1

I am trying to extract UTM coordinates (in meters) of every pixel in a TIFF file using the following syntax in Python.

tifImage = rasterio.open(tif_file)
dx = tifImage.transform[0]
dy = tifImage.transform[4]
ulx = tifImage.transform[2]
uly = tifImage.transform[5]

Then I can calculate UTM of any pixel by using pixel location, width of each pixel and upper left pixel location.

In some files I get correct UTM values as a set of numbers in meters (EW: 789522.5815 NS: 2364415.745) but in some other files, I am getting numbers that seem to be decimal representations of GPS Lat/Longs like (18.8202568671686, 74.0741268192744) and not the UTM values.

How can I address this? I would like to get the coordinates for all the TIFF files. I could not quite figure out the CRS argument and may be that is something I need to do.

Would you recommend to use GDAL?

1

1 Answer 1

1

You can reproject your lon, lat coordinates to UTM. Here's an example of grabbing the x, y coords in raster CRS, then reprojecting to UTM.

import geopandas as gpd
import numpy as np
import rasterio as rio

# My UTM CRS is Zone 55 South,
# find yours on epsg.io or from an existing dataset in correct projection
utm_crs = 28355

with rio.open(f"raster.tif", "r") as ds:

    crs = ds.crs
    cols, rows = np.meshgrid(np.arange(ds.width), np.arange(ds.height))
    xs, ys = rio.transform.xy(ds.transform, rows, cols)

# Create reprojected point geometries
points = gpd.points_from_xy(xs[0], ys[0], crs=crs).to_crs(utm_crs)

# Make a simple list
xys = [[p.x, p.y] for p in points] 

print(xys)

Credit for using a numpy meshgrid to get coordinates using rasterio.transform.xy without looping from this answer by @intotecho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.