简体   繁体   中英

How to get values in the DataArray of XArray

I am trying to get the elevation of points from tif files. I only want the value provided when searching with latitude and longitude. How do I only get the 451.13458 in the array(451.13458, dtype=float32) of the DataArray.

xarr = xr.open_rasterio('/content/DEMs/dem1.tif')

# Slice one of the bands
img = xarr[0, :, :] 

#Use the .sel() method to retrieve the value of the nearest cell close to your POI
val = img.sel(x=lon1, y=lat1, method="nearest")    
print("This is val: ", val)

>>This is val:  <xarray.DataArray ()>
array(451.13458, dtype=float32)
Coordinates:
    band     int64 1
    y        float64 -32.32
    x        float64 18.82
Attributes:
    transform:      (6.615572650436456e-07, 0.0, 18.81609000062383, 0.0, -6.6...
    crs:            +init=epsg:4326
    res:            (6.615572650436456e-07, 6.615572650436456e-07)
    is_tiled:       0
    nodatavals:     (-32767.0,)
    scales:         (1.0,)
    offsets:        (0.0,)
    units:          ('metre',)
    AREA_OR_POINT:  Area

I was able to solve it.

val.data = 451.13458

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