print the pixel coordinates of a specific point in the raster

Question:

To print the pixel coordinates of a specific point in the raster, i used the index() method assuming that The index() method takes the x and y coordinates of the point in geographic coordinates and returns the corresponding row and column indices of the point in the raster.

I want to double-check that.
Is this the best way to handle it?
I’m a beginner, and I’m still unsure about when and how to use the affine transformation.Is it necessary to perform the affine transformation before printing the pixel coordinate?

import rasterio

with rasterio.open("LC08_L2SP_190037_20190619_20200827_02_T1_ST_B10.TIF") as data:
    print(data.crs)
    longitude, latitude = 13.3886, 52.5174
    row, col = data.index(longitude, latitude)
    print("Pixel coordinates of point ({}, {}): ({}, {})".format(longitude, latitude, col, row))
Asked By: Salah Amani

||

Answers:

This code uses the rasterio package to read in a GeoTIFF file and extract information about its coordinate reference system (CRS) and the pixel coordinates of a specified latitude and longitude point.

The code first opens the GeoTIFF file using the rasterio.open() function and stores the resulting DatasetReader object in the variable data. It then prints out the CRS of the raster using the crs attribute of the DatasetReader object.

Next, the code specifies a longitude and latitude point (longitude, latitude = 13.3886, 52.5174) and uses the index() method of the DatasetReader object to convert the point’s coordinates to the corresponding row and column indices in the raster. The resulting pixel coordinates are then printed out using string formatting.

Note that the index() method assumes that the input coordinates are in the same CRS as the raster. If the input coordinates are in a different CRS, you may need to transform them using the rasterio.transform module’s transform() or reproject() functions before calling index().

Answered By: ho3ein
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.