[英]get pixel coordinates of all pixels in a raster image
I have a raster image of shape 9000x10000 that has RGB bands. 我有一个具有RGB波段的形状为9000x10000的光栅图像。 I use the below code to get the XY coordinates of all pixels in the image. 我使用下面的代码来获取图像中所有像素的XY坐标。 But it is very slow. 但这很慢。 Is there a faster way to do it? 有更快的方法吗?
filename='file.dat'
inDs = gdal.Open(filename)
outDs = gdal.Translate('{}.xyz'.format(filename), inDs, format='XYZ', creationOptions=["ADD_HEADER_LINE=YES"])
I want to save the XY coordinates and the pixel values in a dataframe. 我想将XY坐标和像素值保存在数据框中。
If your raster file has a GeoTransform
attribute, you can try this: 如果您的栅格文件具有GeoTransform
属性,则可以尝试以下操作:
import gdal
import pandas as pd
def ix2xy(r,c,gt):
'''Gets x,y from row and column'''
x = gt[0] + r * gt[1]
y = gt[3] + c * gt[5]
return(x,y)
This little function gets the X/Y coordinates from the GeoTransform
attribute which is a tuple with (xorigin, xres, 0, yorigin, 0, yres)
. 这个小函数从GeoTransform
属性获取X / Y坐标,该属性是一个具有(xorigin, xres, 0, yorigin, 0, yres)
的元组。
ds = gdal.Open('file.dat')
gt = ds.GetGeoTransform()
df = pd.DataFrame.from_records(itertools.product(range(ds.RasterYSize),range(ds.RasterXSize)),columns=['Row','Column'])
ds = None
df['X'], df['Y'] = zip(*df.apply(lambda x: ix2xy(x['Column'],x['Row'],gt),axis=1))
This should give you a tidy dataframe with the columns Row
, Column
, X
and Y
. 这应该为您提供一个整齐的数据框,其中包含Row
, Column
, X
和Y
Column
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.