简体   繁体   中英

Python | How do i get the pixel values from an geotiff map by coordinate

I'm trying to get pixel values (RGB) with coordinates from a geotiff map.

I have downloaded a raster map here: https://www.naturalearthdata.com/downloads/10m-raster-data/10m-natural-earth-2/

And I would like to get for example the pixel value of these coordinates lat: 41.902782 , lon: 12.496366 (this should be rome)

I have already tried rasterio and gdal but had no success. The download comes with a .tfw file but I couldn't include it so I wondered if the coordinates in my Python script were accurate.

So in the end I could get rgb values of every square meter in the world But a simple print of the rgb values for a coordinate would be a good start

I would be happy about every answer :)

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

this codes gives you the pixel rgb values of the coordinate on the map

now i only have to calculate how much a meter is in every lat & lon :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM