Reputation: 576
Suppose I have a GeoTIFF image img
with dimensions (1,3,3).
img = np.array([[[1.0, 2.3, 3.3],
[2.4, 2.6, 2.7],
[3.4, 4.2, 8.9]]])
I want to know the geographic coordinates(longitude and latitude) of the pixel whose value is 2.7 within the img
.
Expected output:
coordinates = (98.4567, 16.2888)
Upvotes: 1
Views: 1313
Reputation: 5443
Your question is having the tag rasterio
so i will consider you opened your geotiff with rasterio in that vein :
import rasterio as rio
import numpy as np
dataset = rio.open('file.tif', 'r')
img = dataset.read(1)
# array([[1. , 2.3, 3.3],
# [2.4, 2.6, 2.7],
# [3.4, 4.2, 8.9]])
You have to retrieve the indexes (row and column) that correspond to the value you are looking for:
cell_coords = np.where(img == 2.7)
# (array([1]), array([2]))
Then use the transform
attribute of your dataset (it contains the affine transformation matrix, using affine
python package, allowing to map pixel coordinates to real world coordinates) like this :
coordinates = rio.transform.xy(
dataset.transform,
cell_coords[0],
cell_coords[1],
offset='center',
)
# (98.4567, 16.2888) in your example
Upvotes: 2