Copernicus Dem xarray select 最近 lat/lon 多维坐标

Copernicus Dem xarray select nearest lat/lon with multi-dimension coordinates

这个问题是相关的,但不知何故我仍然需要一些帮助才能让它工作。

import rioxarray
import numpy as np
import geopandas as gpd
import cartopy.crs as ccrs

# download and read elevation data (about 40MB)
xds = rioxarray.open_rasterio("https://elevationeuwest.blob.core.windows.net/copernicus-dem/COP30_hh/Copernicus_DSM_COG_10_N36_00_W113_00_DEM.tif")

# now I wish to find the elevation at the following coordinates:
this_lon = -112.23425
this_lat = 36.3566

# I can get elevation nearby by rounding the coordinates:
xds.loc[dict(x=-112.2, y=36.4)].values
# array([2708.229], dtype=float32)

# but since the data has a 30 meters grid, I should be able 
# to be more precise than rounding the coordinates
# If I use the exact coordinates I get an error since they are 
# not in the indexes:
xds.loc[dict(x=-112.23425, y=36.3566)].values
# KeyError: -112.23425

我尝试过使用 cartopy,但失败了:

data_crs = ccrs.LambertConformal(central_longitude=-100)
x, y = data_crs.transform_point(-112.23425, 36.3566, src_crs=ccrs.PlateCarree())
xds.sel(x=x, y=y)
# KeyError: -1090022.066606806

documentation提到“哥白尼DEM实例在地理坐标中可用;水平参考基准是世界大地测量系统1984(WGS84-G1150;EPSG 4326)”,但我不知道如何使用此信息。

实际上经度和纬度在 1/3600 度处是均匀的 space。 我们可以看到:

xds.x.values
array([-113.        , -112.99972222, -112.99944444, ..., -112.00083333,
       -112.00055556, -112.00027778])

(xds.x.values + 113)*3600
array([0.000e+00, 1.000e+00, 2.000e+00, ..., 3.597e+03, 3.598e+03,
   3.599e+03])

因此我们可以快速找到哥白尼DEM索引中最近的点如下:

i = round((-112.23425 + 113) * 3600)
j = round((36.3566 - 36) * 3600)

lon_nearest = xds.x.values[i]
lat_nearest = xds.y.values[3600-j]

xds.loc[dict(x=lon_nearest, y=lat_nearest)].values