This question is related, but somehow I still need some help to get this to work. 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
I have tried using cartopy, but this fails:
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
The documentation mentions that "The Copernicus DEM instances are available in Geographic Coordinates; the horizontal reference datum is the World Geodetic System 1984 (WGS84-G1150; EPSG 4326)", but I do not know how to use this information.