Python |我如何通过坐标从 geotiff 地图中获取像素值
Python | How do i get the pixel values from an geotiff map by coordinate
我正在尝试使用 geotiff 地图的坐标获取像素值 (RGB)。
我在这里下载了栅格地图:
https://www.naturalearthdata.com/downloads/10m-raster-data/10m-natural-earth-2/
我想得到这些坐标的像素值
lat: 41.902782
, lon: 12.496366
(这应该是罗马)
我已经尝试过 rasterio 和 gdal 但没有成功。下载附带一个 .tfw 文件,但我无法包含它,所以我想知道我的 Python 脚本中的坐标是否准确。
所以最后我可以得到世界上每一平方米的rgb值
但是简单地打印坐标的 rgb 值将是一个好的开始
我会很高兴得到每一个答案:)
import rasterio
def getCoordinatePixel(map,lon,lat):
# open map
dataset = rasterio.open(map)
# get pixel x+y of the coordinate
py, px = dataset.index(lon, lat)
# create 1x1px window of the pixel
window = rasterio.windows.Window(px - 1//2, py - 1//2, 1, 1)
# read rgb values of the window
clip = dataset.read(window=window)
return(clip[0][0][0],clip[1][0][0],clip[2][0][0])
print(getCoordinatePixel("world.tif",0,0))
此代码为您提供地图坐标的像素 rgb 值
现在我只需要计算每个纬度和经度一米是多少:)
我正在尝试使用 geotiff 地图的坐标获取像素值 (RGB)。
我在这里下载了栅格地图: https://www.naturalearthdata.com/downloads/10m-raster-data/10m-natural-earth-2/
我想得到这些坐标的像素值
lat: 41.902782
, lon: 12.496366
(这应该是罗马)
我已经尝试过 rasterio 和 gdal 但没有成功。下载附带一个 .tfw 文件,但我无法包含它,所以我想知道我的 Python 脚本中的坐标是否准确。
所以最后我可以得到世界上每一平方米的rgb值 但是简单地打印坐标的 rgb 值将是一个好的开始
我会很高兴得到每一个答案:)
import rasterio
def getCoordinatePixel(map,lon,lat):
# open map
dataset = rasterio.open(map)
# get pixel x+y of the coordinate
py, px = dataset.index(lon, lat)
# create 1x1px window of the pixel
window = rasterio.windows.Window(px - 1//2, py - 1//2, 1, 1)
# read rgb values of the window
clip = dataset.read(window=window)
return(clip[0][0][0],clip[1][0][0],clip[2][0][0])
print(getCoordinatePixel("world.tif",0,0))
此代码为您提供地图坐标的像素 rgb 值
现在我只需要计算每个纬度和经度一米是多少:)