打印特定点在光栅中的像素坐标。

huangapple go评论105阅读模式
英文:

print the pixel coordinates of a specific point in the raster

问题

为了打印光栅中特定点的像素坐标,我使用了index()方法,假设该方法接受地理坐标点的x和y坐标,并返回该点在光栅中的行和列索引。

我想再次确认一下。
这是否是处理它的最佳方式?
我是个初学者,对何时以及如何使用仿射变换仍然不太确定。在打印像素坐标之前是否有必要执行仿射变换?

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("点 ({}, {}) 的像素坐标: ({}, {})".format(longitude, latitude, col, row))
英文:

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))

答案1

得分: 0

这段代码使用了rasterio包来读取一个GeoTIFF文件,并提取关于其坐标参考系统(CRS)以及指定纬度和经度点的像素坐标信息。

代码首先使用rasterio.open()函数打开GeoTIFF文件,并将结果存储在变量data中,这个结果是一个DatasetReader对象。然后,它使用DatasetReader对象的crs属性打印出了栅格的CRS。

接下来,代码指定了一个经度和纬度点(longitude, latitude = 13.3886, 52.5174),并使用DatasetReader对象的index()方法将该点的坐标转换为栅格中相应的行和列索引。然后,使用字符串格式化打印出了得到的像素坐标。

请注意,index()方法假定输入坐标与栅格具有相同的CRS。如果输入坐标使用不同的CRS,您可能需要在调用index()之前使用rasterio.transform模块的transform()或reproject()函数进行转换。

英文:

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().

huangapple
  • 本文由 发表于 2023年2月19日 17:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定